Comparison between the rand () function and the Mt_rand () function in PHP

Source: Internet
Author: User
Mt_rand () is four times times faster than Rand (), and many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it.

PHP Functions Rand and Mt_rand
  
Mt_rand () four times times faster than Rand ()
  
Many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it. The function uses the known characteristics of the Mersenne Twister as a random number generator, and the average speed of mt_rand () can produce random values four times times faster than the libc provided by Rand ().
  
Mt_rand () four times times faster than Rand ()
  
mt_rand-generate a better random number
  
(PHP 3 >= 3.0.6, PHP 4, PHP 5)
  
int Mt_rand ([int min, int max])
  
Many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it. The function uses the known characteristics of the Mersenne Twister (horse-plug rotation) as a random number generator, which can produce random values four times times faster than Rand () provided by LIBC.
  
If no optional parameters are provided min and Max,mt_rand () return
  
Pseudo-random number between 0 and Rand_max.
  
For example, want a random number between 5 and 15 (including 5 and 15), with Mt_rand (5, 15).
  
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now done automatically.
  
Rand-produces a random integer
  
(PHP 3, PHP 4, PHP 5)
  
int rand ([int min, int max])
  
If no optional parameter min and Max,rand () are provided, the pseudo-random integer between 0 and Rand_max is returned. For example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).
  
Note: Under some platforms (e.g. Windows) Rand_max is only 32768. If the required range is greater than 32768, then specifying the min and Max parameters can generate a number greater than Rand_max, or consider replacing it with Mt_rand ().
  
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now done automatically.

mt_rand definition and usage

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

Grammar
Mt_rand (Min,max) description
If no optional parameter min and Max,mt_rand () are provided, the pseudo-random number between 0 and Rand_max is returned. For example, want a random number between 5 and 15 (including 5 and 15), with Mt_rand (5, 15).

Many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it. The function uses the known characteristics of the Mersenne Twister as a random number generator, which can produce random values four times times faster than Rand () provided by LIBC.
Hints and Notes
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now done automatically.

Note: In the previous version of 3.0.7, Max means range. To get the same random number from the previous example of 5 to 15 in these versions, the short example is Mt_rand (5, 11).
Example
In this case, we will return some random numbers:

The code is as follows:

<?php Echo (Mt_rand ()); Echo (Mt_rand ()); Echo (Mt_rand (10,100));?>

The output is similar to:

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.