A pitfall about PhP random numbers...

Source: Internet
Author: User
Tags random seed

The method for getting random numbers in PHP is simple. You can use the rand function.

 

 
Int rand (INT $ min, int $ max)

A random number within the specified range can be obtained by a single call. But we all know that the random number used in the computer is actually
Pseudo-random number. In general, to increase randomness, we will also get used to setting a Random Seed before the call:

 

 

 
Void srand ([int $ seed])

According to the customs of other languages
A time value is passed in the srand parameter. Generally, the millisecond value or microsecond value of the current time is passed in. Although srand is automatically called when you call Rand from php4.2, srand is not a required operation.

 

In PHP, you can use the microtime () function to obtain random numbers. Therefore, in a general random number scenario, we can use the followingCodeThe random number is obtained.

 

 
<? PHP srand (microtime (); echo rand (1, 25). php_eol; echo rand (1, 25). php_eol;?>

Run the code and we get two random numbers. It looks good, but when we execute it again, we find that the random number is exactly the same as the previous one.

What is the situation? We have set the srand seed to the millisecond value of the current time.

The problem was discovered only after reading the documentation. Originally, without parameters, the mircotime function would return a space-separated string in the format of "msec sec" and undergo automatic type conversion, the actual srand parameter value is 0. When the Random Seed is fixed, a fixed random sequence is obtained. Therefore, the same random number is obtained for each script execution.

Since PhP5, microtime adds$ Get_as_floatParameter. If true is passed, a float value in the current millisecond can be returned for microtime. Because the returned value is the current second value before the decimal point, the result is multiplied by 1000 to extend the decimal point to the millisecond level, in this way, the random number can be safely obtained:

 

 
<? PHP srand (microtime (true) * 1000); echo rand (1, 25). php_eol; echo rand (1, 25). php_eol;?>

 

After two consecutive accesses, different random numbers are obtained, and another bash script is written:

 

 
For I in $ (SEQ 1 1 100) Do curl http: // 127.0.0.1/RND. php echo done

It runs for 100 consecutive times and passes the test.

 

Actually...

The code in worker will report

 

PHP notice: A non well formed numeric value encountered

 

Warning. However, if the script is running in a service or background process, it may not be easy to find the problem.

Or...

PHP already has an mt_rand () function to replace the old Rand, which can be automatically seeding and is four times more efficient than Rand... Well, it seems that there is no less research on old problems and learning new knowledge...

 

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.