Comparison between functions rand and mt_rand in PHP

Source: Internet
Author: User
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. 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 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:
Copy codeThe code is as follows:
Echo (mt_rand ());
Echo (mt_rand ());
Echo (mt_rand (10,100 ));
?>

The output is similar:

3150906288
513289678
35

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.