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