PHP Random number code

Source: Internet
Author: User
Tags min rand range

Today, a friend consulted the fire php random number problem, this is the basic problem of PHP, you can refer to the fire provided by the PHP manual, due to the time relationship, small knitting also lazy write, found some examples on the Internet, together, hope to help you, if you have any questions please say it out below.

  

Seed user-defined functions in microseconds as seeds

function seed ()

{

List ($msec, $sec) = Explode (', microtime ());

return (float) $sec;

}

Sowing the seed of random number generator, using the Srand function to call the return result of the seed function

Srand (Seed ());

The random number produced by the output, the range of the random number is 10-100

echo rand (10,100);

?>

Does this have nothing to do with the bottom one? are random output between 10-100 of the number of new 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 usually a fixed operation that uses seeds as a parameter.

If you try, you'll know, no seed or set a fixed seed, run Rand

Then turn off the browser and run the rand again.

You'll find the results are always the same.

But if you make a seed with a random number, the results of each run are approximate to the random

Let's start with the rand () function, rand ([int min], [int max]) This function takes a random number from min to 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 the rand () function is used, the random number is a large disorder, and it is best to use the Srand () function to configure the new random number seed each time before taking the random number.

Explain the following usage (which is generally the case with these two functions):

Srand (Double) microtime () *1000000);

$rand _number= rand ();

Microtime () returned two values: the current millisecond and timestamp, we want to extract the random number, we can only take a random number from the millisecond, (double) microtime () only returns the current millisecond value.

Microtime () is the number of milliseconds in seconds, so the values are decimal, multiplied by 1000000 to convert them to integers.

; They work in the following processes:

(1): First, provide a "seed" to Srand (), which is a unsigned_int type value.

(2): _ Then, invoke rand (), which returns a random number based on the value provided to Srand () (range between _0~32767)

(3): To call rand () multiple times as needed, to get new random numbers.

(4): Whenever the Srand () can be given a new "seed", thereby further "randomization" of Rand ()

Output results.

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.