I want to generate a 6 digit random number using the PHP mt_rand()
function.
I know the PHP mt_rand()
function only takes 2 parameters: a minimum and a maximum value.
How can I do that?
randint() to generate a random number of length N. Call random. randint(a, b) to return a random number between a and b , inclusive.
An n digit number is a positive number with exactly n digits. Nine hundred distinct n digit numbers are to be formed using only the three digits 2, 5, and 7. The smallest value of n for which this is possible is. (a) 6. (b) 7.
Just generate a int value = random. nextInt(100000) so that you will obtain a value in [0,99999] . Now your definition of 5 digits pin is not precise, 40 could be interpreted as 00040 so it's still 5 digits if you pad it.
Something like this ?
<?php $a = mt_rand(100000,999999); ?>
Or this, then the first digit can be 0 in first example can it only be 1 to 9
for ($i = 0; $i<6; $i++) { $a .= mt_rand(0,9); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With