About using random to generate pseudo-random numbers, random

Source: Internet
Author: User
Tags repetition

About using random to generate pseudo-random numbers, random

I was wondering whether to write any text. It does not require too many text descriptions.

The prelude inserts a small topic. I saw such a joke on the Internet (as shown in the figure). A programmer debugs a program and doubts a mistake in a place in the morning and a mistake in a place in the afternoon, in the evening, I suspect that a certain location may be wrong. It is difficult to sleep after I go to bed. The next morning, when I checked the code again, I accidentally found the bug and missed the comma ",", haha.

I am also negligent this time. In a website project, use random to generate a random number as the name of the uploaded file. Of course, this bug is not easily encountered in files that need to be uploaded by users. However, if you use this random to generate random numbers for Crawler downloading files, you may encounter this bug. Because we use the current time to seed in milliseconds. Here is a fatal misunderstanding: it is thought that milliseconds are very small and different seeds can be generated. However, although the milliseconds are small, the program runs at high speed and cannot be renewed at high speed. In fact, the seeds generated in the loop are the same. I ignored this.

Description ended ,:

 

 

Code close-up (note the method for generating random numbers ):

 

 

Result: the random number generated has a high repetition rate, which can be repeated three or four consecutive times. I guess it is because a millisecond program can go through three or four times.

 

 

In another test method, we do not need to repeat the results. In the case of repetition, a random number is generated cyclically. The result is that after 999 are generated, it enters the endless loop ......

 

 

Finally, we modified the random number generation method to ensure that the seeds of each random in the loop are different. It works !!!

 

 

We removed the loop and found that 10000 different random numbers can be generated at one time without loop:

 

 

Original: James

Email: weimingyou@126.com

Date: 2015/11/06

Link to the original article: Xiao Ming blog-A Manual on generating pseudo-random numbers using random

Related Article

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.