Home >Backend Development >PHP Tutorial >Comparison of the differences between functions rand and mt_rand in PHP_PHP Tutorial
PHP functions rand and mt_rand
mt_rand() is four times faster than rand()
Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of Mersenne Twister as a random number generator. mt_rand() can generate random values on average four times faster than rand() provided by libc.
mt_rand() is four times faster than rand()
mt_rand — generates better random numbers
(PHP 3 >= 3.0.6, PHP 4, PHP 5)
int mt_rand ( [int min, int max] )
Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known properties of Mersenne Twister as a random number generator, which can generate random values on average four times faster than rand() provided by libc.
If the optional parameters min and max are not provided, mt_rand() returns
A pseudo-random number between 0 and RAND_MAX.
For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).
Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() function, it is now done automatically.
rand — Generate a random integer
(PHP 3, PHP 4, PHP 5)
int rand ( [int min, int max] )
If the optional parameters min and max are not provided, rand() returns a pseudo-random integer between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use rand(5, 15).
Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you need a range greater than 32768, specify the min and max parameters to generate a number greater than RAND_MAX, or consider using mt_rand() instead.
Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() function, it is now done automatically.
mt_rand definition and usage
mt_rand() returns a random integer using the Mersenne Twister algorithm.
Syntax
mt_rand(min,max) Description
If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).
Many old libc random number generators have some uncertain and unknown properties and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of Mersenne Twister as a random number generator, which can generate random values on average four times faster than rand() provided by libc.
Tips and Notes
Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions, it is now done automatically.
Note: In versions before 3.0.7, max means range. To get the same random numbers from 5 to 15 as in the above example in these versions, the short example is mt_rand (5, 11).
Example
In this example, we will return some random numbers: