Example of batch random number of time seed in PHP

Source: Internet
Author: User
Tags rand

PHP functions Mt_rand () and Rand () will have a chance of repeating random numbers when they are generated in batches. Srand () and Mt_srand () are not explicitly invoked at the beginning of PHP4.1, and seeds are automatically generated at Mt_rand and Rand. Because, in batches of random, we use the explicit bar to generate seeds, we can avoid duplication. Why, then? Because the seeds are different. Why is the seed different? Because he is the seed of time.

Storing the random number of lives
$RANDARR = Array ();
Generate 100,000.
For ($i =0 $i <100000; $i + +) {
Generate seeds
$date = Explode (', microtime ());
$seed = $date [0];
Seed generator
Mt_srand ($seed);
Generate random numbers
$RANDARR [] = Mt_rand ();

Random numbers are generated. And it won't be repeated oh. The advantage of time is to omit the procedure of do{to generate random number Code}while (!isset (a random number of freshmen) when a common pseudo-random number is duplicated.

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.