问题描述
我知道有多次这个问题已经被提出,但没有这些解决方案为我工作。
I know there are multiple times this question has been put forth but none of those solutions worked for me.
首先,我做这在我的方法叫做 RandomNumGenerator(项目)
First I did this in my method called RandomNumGenerator(items)
List<int> randNum = new List<int>();
foreach (var item in items)
{
randNum.Add(new Random(1000).Next());
}
这总是给我同样的号码,然后细算这个答案我这样做:
This always gave me the same number, and then after looking at this answer I did this:
Random rnd = new Random(1000);
foreach (var item in items)
{
randNum.Add(rnd.Next());
}
这给我的号码如下
325467165
506683626
1623525913
2344573
1485571032
现在,而这是没有问题的每次迭代循环,这里的问题是,当我停下来,重新运行该应用程序,我得到我刚才得到了相同的数字一遍。
Now while that is fine for every iteration of the loop, the problem here is, when I stop and re-run the application, I get the same numbers I got earlier all over again.
325467165
506683626
1623525913
2344573
1485571032
只在调试这个行为,或每次我打电话将我有同样的问题在 RandomNumGenerator
?
推荐答案
您总是播种随机
实例使用相同的种子1000年在这里:
You are seeding the Random
instance always with the same seed 1000 here:
Random rnd = new Random(1000);
这将不会这样做的,因为当前的时间被用作种子:
this will not do that since the current time is used as seed:
Random rnd = new Random();
有一个看构造这需要一个 INT
。
提供了一个相同的种子值不同的随机对象的原因 每个实例产生的相同的序列随机数。
这篇关于随机数发生器产生相同的数字,每次应用程序然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!