The difference between functions Rand and Mt_rand in PHP _php tips

Source: Internet
Author: User
Tags generator rand
PHP Functions Rand and Mt_rand
  
Mt_rand () four times times faster than Rand ()
  
Many old libc random number generators have some uncertainties and unknown properties and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is used informally to replace it. The function uses the known characteristics of the Mersenne twister as a random number generator, and mt_rand () can produce a random numerical velocity of four times times faster than the rand () provided by LIBC.
  
Mt_rand () four times times faster than Rand ()
  
mt_rand-to 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 uncertainties and unknown properties and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is used informally to replace it. The function, known as a random number generator in the Mersenne twister (horse stopper rotation), can produce a random numerical speed of four times times faster than the libc provided by Rand ().
  
If you do not provide optional parameters 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, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.
  
Rand-produces a random integer
  
(PHP 3, PHP 4, PHP 5)
  
int rand ([int min, int max])
  
If the optional parameter min and Max,rand () are not 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 (for example, Windows) Rand_max is only 32768. If the desired range is greater than 32768, then specifying the min and Max parameters can generate a number greater than Rand_max, or consider using Mt_rand () instead.
  
Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

mt_rand Definitions and usage

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

Grammar
Mt_rand (Min,max) description
If the optional parameter min and Max,mt_rand () are not 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 old libc random number generators have some uncertainties and unknown properties and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is used informally to replace it. The function uses the known characteristics of the Mersenne twister as a random number generator, and it can produce a random numerical velocity of four times times faster than the rand () provided by LIBC.
Tips and comments
Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

Note: In the previous version of 3.0.7, Max means range. To get a random number in these versions and the same 5 to 15 as in the previous example, the short example is Mt_rand (5, 11).
Example

In this case, we'll return some random numbers:
Copy Code code as follows:

<?php
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.