Explanation of PHP Random Number Functions rand of and mt_rand of
- 2021-12-04 18:19:15
- OfStack
rand () and mt_rand () in PHP are both functions used to generate a single random number within a specified range. If you need to generate multiple non-repeating random numbers, please refer to: PHP generates N non-repeating random numbers within a specified range.
Since they are all used to generate a random number, what is the difference between them?
rand()
Function default to use libc random number generator, many old libc random number generator has 1 some uncertain and unknown characteristics and efficiency is very low; mt_rand () uses the characteristics known in Mersenne Twister as a random number generator, and its average speed of generating random values is four times faster than rand () provided by libc. So the mt_rand () function is used informally to replace rand () in PHP.
Syntax:
rand(min,max)
mt_rand(min,max)
min and max are optional and specify the range of random numbers.
If no optional parameters min and max are supplied, a pseudo-random integer from 0 to RAND_MAX is returned. For example, for random numbers between 1 and 100 (including 1 and 100), use rand (1, 100) or mt_rand (1, 100).
Note: Since PHP 4.2. 0, PHP generation of random numbers no longer requires srand () or mt_srand () functions to generate random seeds, but has been done automatically.
Summarize