在 .NET 中播种随机数生成器 [英] Seeding a random number generator in .NET
问题描述
我有一个 伪随机数生成器 (PRNG),它使用六个 UInt32
s 作为状态.我需要想出一个合理的方法来播种它.两个明显的可能性是:1) 使用 System.Random
生成六个随机数并将它们用作种子;2) 使用 Guid.NewGuid()
生成两个 GUID
.哪个更好?
I have a pseudorandom number generator (PRNG) with nice properties which uses six UInt32
s as state. I need to come up with a reasonable way to seed it. Two obvious possibilities are: 1) generate six random numbers using System.Random
and use them as seeds; 2) generate two GUID
s with Guid.NewGuid()
. Which would be better?
我不需要加密安全性.
推荐答案
如果需要UInt32
,那Random
更方便吗?只是 Next()
、Next()
、Next()
等等(和转换)...(使用相同的 Random
实例 - 不要每次都创建 new Random()
.
If it needs UInt32
, then Random
is more convenient? just Next()
, Next()
, Next()
etc (and cast)... (use the same Random
instance however - don't create new Random()
each time).
这取决于是否提供足够的随机性的意图.既然这只是种子,应该没问题...
It depends on what the intent is as to whether this offers enough randomness. Since this is just the seed, it should be OK...
这篇关于在 .NET 中播种随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!