Home>Article>Backend Development> Comparison of the differences between rand() function and mt_rand() function in php

Comparison of the differences between rand() function and mt_rand() function in php

怪我咯
怪我咯 Original
2017-07-04 13:54:12 1462browse

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()Functionuses the libc random number generator by default. The mt_rand() function is informally used to replace it.

PHP functionsrand and mt_rand

mt_rand() is four times faster than rand()

Many old libc random number generators have some Uncertain and unknown properties and 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 in 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: Since PHP 4.2.0, it is no longer necessary to use the srand() or mt_srand() function to seed the random numbergenerator, it is nowcompleted automatically.

 rand — Generate a randominteger
  (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 use the srand() or mt_srand() function to seed the random number generator, 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 characteristics 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
NotesNote: As of PHP 4.2.0, seeding the random number generator with the srand() or mt_srand() functions is no longer necessary, 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:

The code is as follows:

The output is similar:


3150906288
513289678
35

The above is the detailed content of Comparison of the differences between rand() function and mt_rand() function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn