随机数发生器产生相同的数字,每次应用程序然随机数、发生器、应用程序、数字

2023-09-03 02:32:55 作者:我们的年少不能重来?

我知道有多次这个问题已经被提出,但没有这些解决方案为我工作。

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

提供了一个相同的种子值不同的随机对象的原因   每个实例产生的相同的序列随机数。

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.