How Random works

Source: Internet
Author: User

In zhihu, I saw Lao Zhao answer the question: what is the most stupid Bug you have ever seen during programming?

First, the Section JS Code:
var number = 10;var showNumber = function () {  alert(number);} (function () {
number = 20; showNumber();})()
Don't run it. Let's guess what alert will do with this code?
The answer is: 10. Okay, why not 20?

Another section of. NET:
Var numbers = new int [10]; // generates 10 random numbers for (var I = 0; I <numbers. length; I ++) {numbers [I] = new System. random (). next ();}
Generate 10 random numbers. Although the results are different each time, why are these 10 numbers the same?
Among them, the first js is because ";" is not added after showNumber (the fourth line); the second. NET's random number. I have never encountered this problem. I will use only one random number at a time and will not call multiple random numbers at a time, so I didn't pay attention to it. Let's take a look at what msdn says: the Random constructor Random () uses the default time-related seed value to initialize a new instance of the Random class. The default seed value is derived from the system clock and has finite resolution. as a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. this problem can be avoided by using a single Random object to generate all random numbers. you can als O work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random (Int32) constructor. (If the English language is not good, the original article will be posted.) The seeds of Random use the system clock seeds. The update frequency of the Windows system clock is about 10 ms (different versions), and the execution of this for loop is obviously much faster, so each time we use the same seed, the same seed, random generates the same Random number (even if the instance is different ). Therefore, the 10 values returned are the same. Solution: the first method is to use Thread. Sleep to Sleep the Thread for a period of time (the time is later than the update frequency of the Windows system clock). This is not recommended. The second type moves initialization out of the loop: var numbers = new int [10];   // Initialize Random System. random rd = new System. random (); for (var I = 0; I <numbers. length; I ++) {numbers [I] = rd. next () ;}reference: MSDN: How the Random class is implemented

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.