简体   繁体   English

在 Laravel 中生成随机数

[英]Generate random number in Laravel

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.请在 Laravel 上开展一个项目,我想以这种格式生成一个随机数:任何位置顺序的一个字符和其余的整数。 Example: C87465398745635, 87474M745436475, 98487464655378J8 etc. and this is my Controller:示例: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 我已经尽我所能但没有成功,请任何帮助解决方案将不胜感激,谢谢

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_random generates a random alpha-numeric string, and sometimes that function returned a numeric value ( see docs ).那是因为 Laravel 的str_random生成一个随机的字母数字字符串,有时该函数返回一个数值(请参阅文档)。

这应该给你 2 到 50 之间的随机数。

$randomId       =   rand(2,50);

use the function rand.使用函数rand。

Example:例子:

 echo rand(0, 99999);

Assuming that the $number you pass on generatePin is the length of pin:假设你传递给 generatePin 的$number是 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);
}

You can use the rand() function to generate a random number.您可以使用rand()函数生成随机数。 You can also create some characters and prefix the rand() with it.您还可以创建一些字符并用它作为rand()前缀。

Generate random number生成随机数

echo generateRandomString(6) // Ex j4nDqQRiYafD

Implementation执行

function generateRandomString(int $n=0)
{
  $al = ['a','b','c','d','e','f','g','h','i','j','k'
  , 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
  'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
  'F','G','H','I','J','K', 'L', 'M', 'N', 'O', 'P',
  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  '0', '2', '3', '4', '5', '6', '7', '8', '9'];

  $len = !$n ? random_int(7, 12) : $n; // Chose length randomly in 7 to 12

  $ddd = array_map(function($a) use ($al){
    $key = random_int(0, 60);
    return $al[$key];
  }, array_fill(0,$len,0));
  return implode('', $ddd);
}

Using range function使用范围功能

range — Create an array containing a range of elements Create an array containing a range of elements. range — 创建一个包含一系列元素的数组 创建一个包含一系列元素的数组。

Using array_rand function使用array_rand函数

Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.从数组中挑选一个或多个随机条目,并返回随机条目的键(或多个键)。 It uses a pseudo random number generator that is not suitable for cryptographic purposes.它使用不适合加密目的的伪随机数生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM