当我在 C# 的循环中声明 Random 时,它给了我非随机数——在循环外声明它给了我随机数.为什么? [英] When I declare Random inside my loop in C#, it gives me nonrandom numbers - declaring it outside my loop gives me random numbers. Why?
问题描述
我正在制作一副纸牌.当我尝试洗牌时,我变得有些奇怪.
I'm creating a deck of cards. When I try to shuffle, I am getting some weirdness.
调用我的程序之前的构造函数经过如下循环(伪代码,包含)-
The constructor before calling my program goes through the following loop (pseudo code,inclusive)-
for i in (0,13):
for j in (0,3):
new Card(i,j)
无论如何,这是简化形式.基本上会生成一张带有数字和花色的卡片.现在问题代码(C#):
This is the simplified form,anyway. Basically a card with a number and a suit is generated. Now the problem code (C#):
private void Shuffle()
{
List<Card> newList = new List<Card>();
while (cards.Count > 0)
{
Random r = new Random();
int next = r.Next(0,cards.Count);
Console.WriteLine(next);
Card cur = cards.ElementAt(next);
newList.Add(cur);
cards.Remove(cur);
}
this.cards = newList;
}
这给了我半可预测的输出 - 即以下内容:
This gives me semi-predictable output - i.e. the following:
18181817171716161515151414141313131212121111111010109998887776665554433315432210
18 18 18 17 17 17 16 16 15 15 15 14 14 14 13 13 13 12 12 12 11 11 11 10 10 10 9 9 9 8 8 8 7 7 7 6 6 6 5 5 5 4 4 3 3 3 1 5 4 3 2 2 1 0
接近尾声时似乎打破了规律,但我不知道为什么.再次运行它会给我不同但非随机的输出.
Near the end it seems to break the pattern, but I'm not sure why. Running it again gives me different, but non-random, outputs.
但是,如果我删除循环外的随机声明 -
However, if I remove the random declaration to OUTSIDE the loop -
private void Shuffle()
{
List<Card> newList = new List<Card>();
Random r = new Random(); /** THE ONLY DIFFERENT LINE **/
while (cards.Count > 0)
{
int next = r.Next(0,cards.Count);
Console.WriteLine(next);
Card cur = cards.ElementAt(next);
newList.Add(cur);
cards.Remove(cur);
}
this.cards = newList;
}
我得到了更多随机的数字,在这种情况下 -
I get much more random seeming numbers, in this case -
19,28,21,2,16,20,33,26,7,36,31,33,33,26,34,4,18,20,13,27,16,11,18,22,18,21,21,8,22,12,6,17,2,17,0,11,2,14,9,0,8,10,1,7,4,1,0,0,2,1,0,0
19,28,21,2,16,20,33,26,7,36,31,33,33,26,34,4,18,20,13,27,16,11,18,22,18,21,21,8,22,12,6,17,2,17,0,11,2,14,9,0,8,10,1,7,4,1,0,0,2,1,0,0
当我从 Visual Studio 发布代码并运行输出的程序时,这种差异似乎消失了.我对发生了什么感到困惑.由于我有 4 个内核,这是否在同一毫秒内将进程分配给 4 个内核,从而使用与其种子相同的数字?但是,当我发布代码时它为什么会起作用,这没有意义......
This difference seems to disappear when I publish the code from Visual Studio and run the outputted program. I'm confused as to what's going on. Since I have 4 cores, is this distributing the process to 4 cores at the same millisecond, thereby using the same number as its seed? That wouldn't make sense as to why it's working when I publish the code, though...
推荐答案
随机数生成从种子值开始.如果相同重复使用种子,生成相同的数字序列.一产生不同序列的方法是使种子值时间相关的,从而产生不同的系列,每个新的随机实例.默认情况下,无参数构造函数Random 类使用系统时钟生成其种子值,
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value,
通过在循环中进行声明,您可以有效地一遍又一遍地使用相同的值调用构造函数 - 因此您会得到相同的数字.
By having the declaration in the loop you are effectively calling the constructor with the same value over and over again - hence you are getting the same numbers out.
这篇关于当我在 C# 的循环中声明 Random 时,它给了我非随机数——在循环外声明它给了我随机数.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!