A handwriting for generating pseudo-random numbers with random

Source: Internet
Author: User

I wonder if I should write any more words. Really don't need a lot of text to describe it.

Prelude inserted a small topic, before on the Internet to see such a cold joke (there is a picture), a program ape debugging procedures, the morning suspected of such a mistake in the afternoon, suspected such a place mistake, night suspected that such place may be wrong, sleep or tossing and turning hard to sleep. The next morning, look at the code again, accidentally found a bug, missing the comma "," haha cold jokes.

I was also negligent this time. In a Web site project, random numbers are used to generate the names of the uploaded files. Of course, in the need for users to upload files are not easy to encounter this bug. However, it is very likely that you will encounter this bug when you create a random number using this random in a crawler download file. Because we are going to seed in milliseconds of the current time. Here's a deadly myth: thinking that milliseconds are small can produce different seeds. But the millisecond is small, but the program is high-speed, high-speed to not Incredibles, in fact, the seed produced in the cycle is the same. I have overlooked this point.

End of Description:

Code features (Note the method of generating random numbers):

Results: The repetition rate of the random number produced is very high, basically can repeat three or four continuously. I guess it's because a millisecond program can go three or four times in a process.

Let's switch to another test method, the result is not repeated, in the case of repetition, into the loop to generate random numbers. The result is that after generating 999, it goes into the dead loop ...

Finally, we modified the method of generating random numbers to make sure that the seed for each random in the loop was not the same. It WORKS!!!

We then remove the loop and find that, in the case of no need for a loop, it can produce 10,000 different random numbers at once:

Original: Xiao Ming

Email:[email protected]

Date:2015/11/06

The original link: Xiao Ming's Blog Park-"about using random to generate pseudo-random numbers of a handwriting"

A handwriting for generating pseudo-random numbers with random

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.