php generates a random number mt_rand () rand () mt_srand () function

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags echo example function network network programming php php tutorial programming

mt_rand () Returns a random integer using the mersenne twister algorithm.

grammar
mt_rand (min, max) Description mt_rand () returns a pseudo-random number from 0 to rand_max if the optional parameters min and max are not provided. For example, want a random number between 5 and 15 (including 5 and 15), using mt_rand (5, 15).

* /
echo mt_rand (); // Generate a random number
echo "";
echo mt_rand (); // Generate a random number
echo "";
echo mt_rand (10,100); // Generate a random number between 10 and 00

/ *
mt_srand () Seeds mersenne twister random number generator.

grammar
mt_srand (seed) Parameter Description
seed required Use seed to seed the random number generator.

Description From the php tutorial version 4.2.0 onwards, the seed parameter becomes optional, when the item is empty, it will be set to the number of hours.

* /

function make_seed () // Generate a random number seed
{
list ($ usec, $ sec) = explode ('', microtime ()); // Divide the current number of milliseconds
return (float) $ sec + ((float) $ usec * 100000); // return value
}
mt_srand (make_seed ()); // Seeds the random number generator
$ randval = mt_rand (); // Generate a random number
Echo $ randval; / / output the results

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

grammar
rand (min, max) Parameter description
min, max optional. Specify the range of random numbers.

Description If no optional arguments min and max are 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 (); / / generate random numbers
echo "";
Echo rand (); / / generate random numbers
echo "";
echo rand (5,15); // Generate a random number between 5 and 15

/ *
Note: In some platforms (for example, windows) rand_max only 32768. If you need a range greater than 32768, specifying the min and max parameters yields a number greater than rand_max, or consider replacing it with mt_rand ()
* /

Related Article

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.