PHP random number code PHP generates random numbers method _php Tutorial

Source: Internet
Author: User
Today, a friend to inquire about the fire PHP random number of questions, this is the basic problem of PHP, we can refer to the fire provided PHP manual, because of time, small series also lazy writing, in the Internet to find some examples, integrated together, I hope to help you, if there are problems please say below.

Seed user-defined functions with microseconds as seeds
function seed ()
{
List ($msec, $sec) = explode (' ', Microtime ());
return (float) $sec;
}
Seeding the random number generator and invoking the return result of the seed function with the Srand function
Srand (Seed ());
The output generates random numbers, and the range of random numbers is 10-100.
echo rand (10,100);
?>
Isn't this the same as the one below? are random output 10-100 between the number of new students learning, may ask too simple OH
echo rand (10,100);
?>

Mt_rand (10,100);
Srand is a seed, if not set, the default is 1.
Rand is generally a fixed operation that uses seed to make parameters.

You'll find out if you try. Run Rand without seeding or setting a fixed seed
Then close the browser and open it again, and then run Rand.
You'll find the results are always the same.
But if a random number is used to seed, then the result of each run is approximate to the random

First say the rand () function, rand ([int min], [int max]) This function takes a random number between Min and Max. Without specifying the maximum and minimum range of random numbers, this function automatically takes a random number from 0 to Rand_max.

However, if only rand () is used, the random number is very chaotic and it is best to use the Srand () function to configure a new random number seed each time before taking a random number.

Explain the following usage (which is generally the case with these two functions):
Srand (Double) microtime () *1000000);
$rand _number= rand ();

Microtime () returns two values: the current millisecond and timestamp, we want to extract the random number, only a random number from the millisecond, (double) microtime () only returns the current millisecond value.
Microtime () is the number of milliseconds in seconds, and therefore the value is a decimal, multiplied by 1000000 to convert it to an integer.

; their workflow is as follows:
(1): First, give Srand () a "seed"; it is a value of type Unsigned_int.
(2): _ Then, call Rand (), which returns a random number (range between _0~32767) based on the value provided to Srand ()
(3): Call rand () as many times as needed, and get new random numbers continuously.
(4): Whenever you can provide a new "seed" for Srand (), further "Randomize" rand ()
Output results.

http://www.bkjia.com/PHPjc/364638.html www.bkjia.com true http://www.bkjia.com/PHPjc/364638.html techarticle today, a friend to inquire about the fire PHP random number of questions, this is the basic problem of PHP, we can refer to the fire provided PHP manual, due to the time relationship, small series also lazy writing, in the net ...

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