Comparison of functions rand and mt_rand in PHP

  • 2020-05-27 04:34:11
  • OfStack

The 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 properties and are slow. The rand() function of PHP USES libc random number generator by default. The mt_rand() function is used informally to replace it. This function USES the known properties of Mersenne Twister as a random number generator, and mt_rand() can generate random Numbers four times faster on average 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 properties and are slow. PHP's rand() function USES libc random number generator by default. The mt_rand() function is used informally to replace it. This function USES the known properties of Mersenne Twister(the horse twist) as a random number generator, which can generate random values at an average speed of four times faster than rand() provided by libc.

If the optional parameters min and max are not provided, mt_rand() is returned

A pseudorandom 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, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions. This is now done automatically.

rand - generates 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: on some platforms (e.g. Windows) RAND_MAX is only 32768. If the range required is greater than 32768, then specifying the min and max parameters will generate Numbers greater than RAND_MAX, or consider mt_rand() instead.

Note: since PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions. This is now done automatically.

Definition and usage of mt_rand

mt_rand() returns a random integer using the Mersenne Twister algorithm.

grammar
mt_rand (min max)
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 properties and are slow. PHP's rand() function USES libc random number generator by default. The mt_rand() function is used informally to replace it. This function USES the known properties of Mersenne Twister as a random number generator, which can produce random values at an average speed of four times faster than rand() provided by libc.
Hints and comments
Note: since PHP 4.2.0, it is no longer necessary to seed the random number generator with srand() or mt_srand() functions. This is now done automatically.

Note: in versions prior to 3.0.7, max meant range. To get the same 5 to 15 random Numbers in these versions as in the previous example, a short example is mt_rand (5, 11).
example

In this case, we will return 1 random number:
 
<?php 
echo(mt_rand()); 
echo(mt_rand()); 
echo(mt_rand(10,100)); 
?> 

The output is similar to:

3150906288
513289678
35

Related articles: