Php random number code/php random number generation example

Source: Internet
Author: User
Php random number code/php random generation number instance & lt ;? Php // seed user-defined functions use microseconds as the seed functionseed () {list ($ msec, $ sec) = explode ('', microtime (); return (float) $ sec;} php random number code/php random number generation instance

// Seed user-defined functions use microseconds as the seed
Function seed ()
{
List ($ msec, $ sec) = explode ('', microtime ());
Return (float) $ sec;
}
// Broadcast the seed of the random number generator and use the srand function to call the returned result of the seed function.
Srand (seed ());
// Random number generated by the output. the random number ranges from 10 to 100.
Echo rand (10,100 );
?>
Is the above and below no different? They all randomly output numbers between 10-. it's too easy for new people to learn.
Echo rand (10,100 );
?>

Mt_rand (10,100 );
Srand is the seed. if not set, the default value is 1.
Rand is a fixed operation of parameters using seeds.

You can give it a try. you don't need a seed or set a fixed seed to run rand.
Then turn off the browser, and then run rand.
You will find that the results are always the same
However, if a random number is used as the seed, the running result is similar to a random one.

Let's talk about the rand () function. rand ([int min], [int max]) gets a random number between min and max. If the maximum and minimum ranges of random numbers are not specified, this function automatically retrieves a random number from 0 to RAND_MAX.

However, if you only use the rand () function, the random number is messy. you 'd better use the srand () function before getting the random number each time to configure the new random number seed.

Explain the following usage of functions (these two functions are generally used in this way ):
Srand (double) microtime () * 1000000 );
$ Rand_number = rand ();

Microtime () returns two values: the current millisecond and timestamp. to extract a random number, we can only get one random number from the millisecond, (double) microtime () only the current millisecond value is returned.
Microtime () is the number of milliseconds in seconds. Therefore, the values are decimal places and multiplied by 1000000 to convert them into integers.

Their workflow is as follows:
(1): first, provide a "seed" to srand (), which is a value of the unsigned_int type.
(2): _ then, call rand (), which returns a random number (in the range of _ 0 ~) based on the value provided to srand ~ Between 32767)
(3): call rand () multiple times as needed to continuously obtain new random numbers.
(4): whenever srand () can be provided with a new "seed" to further "Randomize" the output results of rand.

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.