C# Random.Next 总是返回相同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1654887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random.Next returns always the same values
提问by Thomas
This is really weird, and I cannot see why this is happening. In the foreach cycle, I am iterating through a class A collection, and for each class, I call the Count()
method, where r1
and r2
numbers are generated from range [-1,1]. The problem is that Random.Next
returns the same "random" numbers for each instance. When the results for the first instance are 0 and -1, the same ones will be returned from following instances. Please, could you tell me why this is happening? Also, I cannot get different results in each class A instance. This is the code:
这真的很奇怪,我不明白为什么会这样。在foreach循环,我迭代通过类的集合,并且对于每个类,我调用Count()
方法,其中,r1
和r2
数据是从范围[-1,1]生成。问题是 Random.Next
为每个实例返回相同的“随机”数字。当第一个实例的结果为 0 和 -1 时,后续实例将返回相同的结果。拜托,你能告诉我为什么会这样吗?此外,我无法在每个 A 类实例中获得不同的结果。这是代码:
class a
{
Random rnd = new Random();
private void Count()
{
int r1 = rnd.Next(-1, 1);
int r2 = rnd.Next(-1, 1);
}
}
class b
{
List<a> listofA=new list<a>();
foreach (a ACLASS in listofA)
{
ACLASS.Count();
}
}
采纳答案by Guffa
The problem is that you are creating instances of the Random
class too close in time.
问题是您创建的Random
类的实例时间太近了。
When you create a Random
object, it's seeded with a value from the system clock. If you create Random
instances too close in time, they will all be seeded with the same random sequence.
当您创建一个Random
对象时,它会从系统时钟中获得一个值。如果您创建的Random
实例时间太近,它们将全部以相同的随机序列进行播种。
Create a single Random
object and pass its reference to the constructor when you create instances of the "a" class, instead of creating one Random
object for each "a" instance.
在创建Random
“a”类的实例时,创建一个对象并将其引用传递给构造函数,而不是Random
为每个“a”实例创建一个对象。
回答by Jherico
You include a random instance for each A instance. It sounds like they're all getting the same default seed value. You probably want to make a static random for all A instances and use it repeatedly, or alternatively provide a seed value to the Random() instance in the A constructor.
您为每个 A 实例包含一个随机实例。听起来他们都获得了相同的默认种子值。您可能希望为所有 A 实例创建一个静态随机数并重复使用它,或者为 A 构造函数中的 Random() 实例提供一个种子值。
回答by ChrisF
You're creating a new instance of Random
very close together (your loop is very tight) so each instance is effectively using the same seed value.
您正在创建一个Random
非常接近的新实例(您的循环非常紧密),因此每个实例都有效地使用了相同的种子值。
A better approach would be to create one instance and pass that to your Count
method.
更好的方法是创建一个实例并将其传递给您的Count
方法。
You problably know this next bit, but I'll include it here for completeness:
您可能知道下一点,但为了完整起见,我将其包括在此处:
The MSDNhas the details on this, but basically your problem is the Random.Nextmethod you're using generates:
在MSDN对这个细节,但基本上你的问题是Random.Next你使用生成方法:
A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
大于或等于 minValue 且小于 maxValue 的 32 位有符号整数;即返回值的范围包括minValue,但不包括maxValue。如果 minValue 等于 maxValue,则返回 minValue。
because of this your calls will return -1 or 0.
因此,您的调用将返回 -1 或 0。
回答by tvanfosson
Use a single, static Random number generator for all instances of the class.
对类的所有实例使用单个静态随机数生成器。
class a
{
private static Random rnd;
static a() {
rnd = new Random();
}
private void Count()
{
int r1 = rnd.Next(-1, 2);
int r2 = rnd.Next(-1, 2);
}
}
Note the change to give you numbers in the range -1,1 rather than -1,0
请注意为您提供 -1,1 而不是 -1,0 范围内的数字的更改