php 在 Laravel 中生成随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24884174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:31:34  来源:igfitidea点击:

Generate random number in Laravel

phpstringrandomlaravel

提问by kacyblack

Please am working on a Project on Laravel and I wanted to Generate a Random Number in this format: one character in any position order and the rest integers. Example: C87465398745635, 87474M745436475, 98487464655378J8 etc. and this is my Controller:

请在 Laravel 上开展一个项目,我想以这种格式生成一个随机数:任何位置顺序的一个字符和其余的整数。示例:C87465398745635、87474M745436475、98487464655378J8 等,这是我的控制器:

    function generatePin( $number ) {
    $pins = array();
    for ($j=0; $j < $number; $j++) { 
        $string = str_random(15);
        $pin = Pin::where('pin', '=', $string)->first();
        if($pin){
            $j--;
        }else{
            $pins[$j] = $string;
        }
    }



    return $pins;
}

But it seems to be Generating something else like this: ZsbpEKw9lRHqGbv, i7LjvSiHgeGrNN8, pyJEcjhjd3zu9Su I have tried all I could but no success, Please any helping solution will be appreciated, Thanks

但它似乎正在生成其他类似的东西:ZsbpEKw9lRHqGbv、i7LjvSiHgeGrNN8、pyJEcjhjd3zu9Su 我已经尽我所能但没有成功,请任何帮助解决方案将不胜感激,谢谢

回答by Luís Cruz

If you want to generate the random string like you said, replace:

如果你想像你说的那样生成随机字符串,请替换:

$string = str_random(15);

with

// Available alpha caracters
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// generate a pin based on 2 * 7 digits + a random character
$pin = mt_rand(1000000, 9999999)
    . mt_rand(1000000, 9999999)
    . $characters[rand(0, strlen($characters) - 1)];

// shuffle the result
$string = str_shuffle($pin);

Edit:

编辑:

Before, the code wasn't generating a random alpha character all the time. Thats because Laravel's str_randomgenerates a random alpha-numeric string, and sometimes that function returned a numeric value (see docs).

之前,代码并不是一直生成随机的字母字符。那是因为 Laravel 会str_random生成一个随机的字母数字字符串,有时该函数会返回一个数值(请参阅文档)。

回答by Vainglory07

Assuming that the $numberyou pass on generatePin is the length of pin:

假设$number你通过 generatePin 是 pin 的长度:

function generatePin( $number ) {
    // Generate set of alpha characters
    $alpha = array();
    for ($u = 65; $u <= 90; $u++) {
        // Uppercase Char
        array_push($alpha, chr($u));
    }

    // Just in case you need lower case
    // for ($l = 97; $l <= 122; $l++) {
    //    // Lowercase Char
    //    array_push($alpha, chr($l));
    // }

    // Get random alpha character
    $rand_alpha_key = array_rand($alpha);
    $rand_alpha = $alpha[$rand_alpha_key];

    // Add the other missing integers
    $rand = array($rand_alpha);
    for ($c = 0; $c < $number - 1; $c++) {
        array_push($rand, mt_rand(0, 9));
        shuffle($rand);
    }

    return implode('', $rand);
}