php random x digit number

前端 未结 21 2137
耶瑟儿~
耶瑟儿~ 2020-12-04 15:00

I need to create a random number with x amount of digits.

So lets say x is 5, I need a number to be eg. 35562 If x is 3, then it would throw back something like; 463

相关标签:
21条回答
  • 2020-12-04 15:43

    Following is simple method to generate specific length verification code. Length can be specified, by default, it generates 4 digit code.

    function get_sms_token($length = 4) {
    
        return rand(
            ((int) str_pad(1, $length, 0, STR_PAD_RIGHT)),
            ((int) str_pad(9, $length, 9, STR_PAD_RIGHT))
        );
    }
    
    echo get_sms_token(6);
    
    0 讨论(0)
  • 2020-12-04 15:45

    Here is a simple solution without any loops or any hassle which will allow you to create random string with characters, numbers or even with special symbols.

    $randomNum = substr(str_shuffle("0123456789"), 0, $x);

    where $x can be number of digits

    Eg. substr(str_shuffle("0123456789"), 0, 5);

    Results after a couple of executions

    98450
    79324
    23017
    04317
    26479
    

    You can use the same code to generate random string also, like this

    $randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, $x);
    

    Results with $x = 11

    FgHmqpTR3Ox
    O9BsNgcPJDb
    1v8Aw5b6H7f
    haH40dmAxZf
    0EpvHL5lTKr
    
    0 讨论(0)
  • 2020-12-04 15:45

    Please not that rand() does not generate a cryptographically secure value according to the docs:

    http://php.net/manual/en/function.rand.php

    This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.

    Instead it is better to use random_int(), available on PHP 7 (See: http://php.net/manual/en/function.random-int.php).

    So to extend @Marcus's answer, you should use:

    function generateSecureRandomNumber($digits): int {
       return random_int(pow(10, $digits - 1), pow(10, $digits) - 1);
    }
    
    function generateSecureRandomNumberWithPadding($digits): string {
       $randomNumber = random_int(0, pow(10, $digits) - 1);
       return str_pad($randomNumber, $digits, '0', STR_PAD_LEFT);
    }
    

    Note that using rand() is fine if you don't need a secure random number.

    0 讨论(0)
  • 2020-12-04 15:46

    You can use rand($min, $max) for that exact purpose.

    In order to limit the values to values with x digits you can use the following:

    $x = 3; // Amount of digits
    $min = pow(10,$x);
    $max = pow(10,$x+1)-1);
    $value = rand($min, $max);
    
    0 讨论(0)
  • 2020-12-04 15:47

    You can use rand() together with pow() to make this happen:

    $digits = 3;
    echo rand(pow(10, $digits-1), pow(10, $digits)-1);
    

    This will output a number between 100 and 999. This because 10^2 = 100 and 10^3 = 1000 and then you need to subtract it with one to get it in the desired range.

    If 005 also is a valid example you'd use the following code to pad it with leading zeros:

    $digits = 3;
    echo str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    
    0 讨论(0)
  • 2020-12-04 15:47
    function random_number($size = 5)
    {
        $random_number='';
        $count=0;
        while ($count < $size ) 
            {
                $random_digit = mt_rand(0, 9);
                $random_number .= $random_digit;
                $count++;
            }
        return $random_number;  
    }
    
    0 讨论(0)
提交回复
热议问题