Problem:
You want to generate a random number, it may have any length or within a range that you’ll specify.
Solution:
You can use mt_rand() function to generate one random number of any length or a random number between a certain range.
Generate a random number without any range
When you use mt_rand() without any parameter, the function returns a random number between 0 and mt_getrandmax(), inclusive (here, mt_getrandmax() shows the largest possible random value). See the following code-
<?php echo mt_rand() . "<br />"; echo mt_rand() . "<br />"; ?>
Output:
1669333830
32691779
Here, you’ll get different random numbers when you run the above code.
Generate one random number ina range
When you use two parameters in the mt_rand() function, the function returns a number between the two numbers, inclusive.The first one indicates the minimum and the second one indicates the maximum number. See the following code-
<?php echo mt_rand(10000, 99999); ?>
Output:
34424
Note: PHP has another random number generator function – rand(). But, mt_rand() function uses “Mersenne Twister” random number generator which works four time faster than rand() function does.