C# 我如何播种一个随机类以避免获得重复的随机值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1785744/
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
How do I seed a random class to avoid getting duplicate random values
提问by leora
I have the following code inside a static method in a static class:
我在静态类的静态方法中有以下代码:
Random r = new Random();
int randomNumber = r.Next(1,100);
I have this inside a loop and I keep getting the same randomNumber
!
我在一个循环内有这个,而且我一直得到相同的结果randomNumber
!
Any suggestions here?
这里有什么建议吗?
采纳答案by Mehrdad Afshari
You should not create a new Random
instance in a loop. Try something like:
您不应Random
在循环中创建新实例。尝试类似:
var rnd = new Random();
for(int i = 0; i < 100; ++i)
Console.WriteLine(rnd.Next(1, 100));
The sequence of random numbers generated by a single Random
instance is supposed to be uniformly distributed. By creating a new Random
instance for every random number in quick successions, you are likely to seed them with identical values and have them generate identical random numbers. Of course, in this case, the generated sequence will be far from uniform distribution.
单个Random
实例生成的随机数序列应该是均匀分布的。通过Random
快速连续为每个随机数创建一个新实例,您可能会为它们设置相同的值并让它们生成相同的随机数。当然,在这种情况下,生成的序列将远非均匀分布。
For the sake of completeness, if you really need to reseed a Random
, you'll create a new instance of Random
with the new seed:
为了完整起见,如果您确实需要重新播种a Random
,您将Random
使用新种子创建一个新实例:
rnd = new Random(newSeed);
回答by McKay
public static Random rand = new Random(); // this happens once, and will be great at preventing duplicates
Note, this is not to be used for cryptographic purposes.
请注意,这不能用于加密目的。
回答by PPC
In case you can't for some reason use the same Random
again and again, try initializing it with something that changes all the time, like the time itself.
如果由于某种原因您不能Random
一次又一次地使用相同的内容,请尝试使用一直在变化的内容对其进行初始化,例如时间本身。
new Random(new System.DateTime().Millisecond).Next();
Remember this is bad practice though.
请记住,这是不好的做法。
EDIT: The default constructor already takes its seed from the clock, and probably better than we would. Quoting from MSDN:
编辑:默认构造函数已经从时钟中获取种子,并且可能比我们更好。引用自 MSDN:
Random() : Initializes a new instance of the Random class, using a time-dependent default seed value.
Random() :使用与时间相关的默认种子值初始化 Random 类的新实例。
The code below is probably your best option:
下面的代码可能是你最好的选择:
new Random().Next();
回答by Omidoo
this workes for me:
这对我有用:
private int GetaRandom()
{
Thread.Sleep(1);
return new Random(DateTime.Now.Millisecond).Next();
}
回答by ZedZed
A good seed initialisation can be done like this
一个好的种子初始化可以这样完成
Random rnd = new Random((int)DateTime.Now.Ticks);
The ticks will be unique and the cast into a int with probably a loose of value will be OK.
刻度将是唯一的,并且转换为可能具有松散值的 int 就可以了。
回答by joppiesaus
A good seed generation for me is:
对我来说,好的种子代是:
Random rand = new Random(Guid.NewGuid().GetHashCode());
It is very random. The seed is always different because the seed is also random generated.
这是非常随机的。种子总是不同的,因为种子也是随机生成的。
回答by Gary Davies
I use this for most situations, keep the seed if there is a need to repeat the sequence
我在大多数情况下使用它,如果需要重复序列,请保留种子
var seed = (int) DateTime.Now.Ticks;
var random = new Random(seed);
or
或者
var random = new Random((int)DateTime.Now.Ticks);
回答by Orphid
Bit late, but the implementationused by System.Random is Environment.TickCount
:
有点晚了,但System.Random 使用的实现是Environment.TickCount
:
public Random()
: this(Environment.TickCount) {
}
This avoids having to cast DateTime.UtcNow.Ticks
from a long, which is risky anyway as it doesn't represent ticks since system start, but "the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar)".
这避免了必须DateTime.UtcNow.Ticks
从 long 进行转换,这无论如何都是有风险的,因为它不代表自系统启动以来的滴答声,而是“自 0001 年 1 月 1 日午夜 12:00:00 以来经过的 100 纳秒间隔的数量(0 :00:00 UTC 于 0001 年 1 月 1 日,在公历中)”。
Was looking for a good integer seed for the TestApi's StringFactory.GenerateRandomString
正在为 TestApi 寻找一个好的整数种子 StringFactory.GenerateRandomString