Comparison between functions rand and mt_rand in PHP. PHP functions rand and mt_randmt_rand () are four times faster than rand (). The old libc random number generator has some uncertain and unknown features and is very slow. PHP rand () functions use PHP functions rand and mt_rand by default.
Mt_rand () is four times faster than rand ()
Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator. mt_rand () can generate random values at an average speed four times faster than the rand () provided by libc.
Mt_rand () is four times faster than rand ()
Mt_rand-generate 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 features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses the known features in Mersenne Twister (horse plug rotation) as a random number generator, which can generate an average speed of random values four times faster than the rand () provided by libc.
If the optional parameters min and max are not provided, mt_rand () are returned.
A pseudo-random number between 0 and RAND_MAX.
For example, if you want a random number between 5 and 15 (including 5 and 15), use mt_rand (5, 15 ).
Note: Since PHP 4.2.0, the random number generator is no longer required to be seeding by using the srand () or mt_srand () function, which is now automatically completed.
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 (including 5 and 15), use rand (5, 15 ).
Note: in some platforms (such as Windows), RAND_MAX is only 32768. If the required range is greater than 32768, specify the min and max parameters to generate a number greater than RAND_MAX, or use mt_rand () instead.
Note: Since PHP 4.2.0, the random number generator is no longer required to be seeding by using the srand () or mt_srand () function, which is now automatically completed.
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 (including 5 and 15), use mt_rand (5, 15 ).
Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc.
Tips and comments
Note: Since PHP 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which is now automatically completed.
Note: In versions earlier than 3.0.7, max indicates range. In these versions, we need to obtain a random number from 5 to 15, which is the same as the preceding example. The short example is mt_rand (5, 11 ).
Example
In this example, we will return some random numbers:
The code is as follows:
Echo (mt_rand ());
Echo (mt_rand ());
Echo (mt_rand (10,100 ));
?>
The output is similar:
3150906288
513289678
35
Mt_rand () is four times faster than rand (). The random number generator of the old libc has some uncertain and unknown features and is very slow. The rand () function of PHP is used by default...