在知乎上看到老赵回答了 你在编程时见过最愚蠢的 Bug 是什么?
首先是段JS代码:
var number = 10;
var showNumber = function () {
alert(number);
}
(function () {
number = 20;
showNumber();
})()
不要运行,猜猜看这段代码会alert出什么结果来?答案是:10。好吧,为什么不是20?再来一段.NET的:
var numbers = new int[10];
// 生成10个随机数
for (var i = 0; i < numbers.Length; i++) {
numbers[i] = new System.Random().Next();
}
生成10个随机数,尽管每次运行结果都不同,但为什么每次这10个数字都是相同的?
其中第一js是因为没在showNumber后面(第四行)加“;”;
第二.NET的随机数这个问题我倒是没遇到过,几乎每次只会用一个不会一次调用多个,所以没注意。
先看看msdn怎么说:
Random
构造函数Random()
使用与时间相关的默认
种子值,初始化 Random 类的新实例。
system clock and has finite resolution."> The default seed value is derived from the system clock and has finite resolution. class="mtpsTagOuterHtml" ><span>Random</span></span></span> 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.">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.can be avoided by using a single <span><span class="mtpsTagOuterHtml" ><span>Random</span></span></span> object to generate all random numbers.">This problem can be avoided by using a single Random object to generate all random numbers. modifying the seed value returned by the system clock and then explicitly providing this new seed value to the <span><span class="mtpsTagOuterHtml" ><span>Random(Int32)</span></span></span> constructor.">You can also 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. (英语不好,就贴原文了)
Random的种子利用的是系统时钟种子。而Windows系统时钟的更新频率大概在10ms左右(各个
版本不一样),而这个for
循环的执行显然要快得多,那每次利用的就是同一个种子,种子相同,Random产生相同的随机数(即使实例不同也一样)。所以返回的10个值都是一样的了。
解决方法:
第一种利用Thread.Sleep让
线程休眠一段时间(时间大于Windows系统时钟的更新频率),这个不推荐使用。
第二种将初始化移出循环:
var numbers = new int[10];
// 初始化Random
System.Random rd = new System.Random();
for (var i = 0; i < numbers.Length; i++)
{
numbers[i] = rd.Next();
}
参考文档:
MSDN:Random 类
Random的实现原理