Php generates the random number mt_rand () rand () mt_srand () function.

Source: Internet
Author: User
Tags random seed
Mt_rand () returns a random integer using the mersennetwister algorithm. The syntax mt_rand (min, max) indicates that if the optional parameters min and max are not provided, mt_rand () returns 0...

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 ).

*/
Echo mt_rand (); // generates a random number.
Echo"
";
Echo mt_rand (); // generates a random number.
Echo"
";
Echo mt_rand (10,100); // Generate 10 ~ Random number between 00

/*
Mt_srand () seeding mersenne twister random number generator.

Syntax
Mt_srand (seed) parameter description
Seed is required. Seed is used to seeding the random number generator.

Description
Starting from php version 4.2.0, the seed parameter is changed to an optional parameter. If this parameter is left blank, it is set to the number at any time.

*/

Function make_seed () // generates a random seed.
{
List ($ usec, $ sec) = explode ('', microtime (); // splits the current number of milliseconds
Return (float) $ sec + (float) $ usec * 100000); // return value
}
Mt_srand (make_seed (); // seeding for the random number generator
$ Randval = mt_rand (); // generates a random number.
Echo $ randval; // output result

/*
The rand () function returns a random integer.

Syntax
Rand (min, max) parameter description
Min, max (optional. Specifies the range of random numbers.

Description
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 ).

*/

Echo rand (); // generates a random number.
Echo"
";
Echo rand (); // generates a random number.
Echo"
";
Echo rand (5, 15); // generates 5 ~ Random number between 15

/*
Note: in some platforms (such as windows), rand_max only has 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 () to replace it.
*/

Address:

Reprinted at will, but please attach the article address :-)

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.