用于随机生成器的 64 位种子 [英] 64 bits Seeds for random generators
问题描述
我目前正在运行具有 8 个以上管道(线程)的多线程模拟应用程序.这些管道运行非常复杂的代码,该代码依赖于由种子生成的随机序列.然后将序列归结为单个 0/1.
I am currently running a multithreading simulation application with 8+ pipes (threads). These pipes run a very complex code that depends on a random sequence generated by a seed. The sequence is then boiled down to a single 0/1.
在将种子从主线程传递到处理管道后,我希望这种随机处理"具有 100% 确定性.因此,我可以在第二次运行中复制结果.
I want this "random processing" to be 100% deterministic after passing a seed to the processing pipe from the main thread. So, I can replicate the results in a second run.
所以,例如:(我有这个编码并且它有效)
So, for example: (I have this coded and it works)
Pipe 1 -> Seed: 123 -> Result: 0
Pipe 2 -> Seed: 123 -> Result: 0
Pipe 3 -> Seed: 589 -> Result: 1
当我需要运行 100M 或更多这些进程,然后对结果求平均值时,就会出现问题.可能是100M中只有1个是1,其余都是0.很明显,我无法使用 32 位种子输入 srand()
对 100M 随机值进行采样.
The problem arises when I need to run 100M or more of these processes and then average the results. It may be the case only 1 of the 100M is a 1, and the rest are 0.
As it is obvious, I cannot sample 100M random values with 32bit seeds feeding to srand()
.
是否可以在 VS2010 中使用 64 位种子种子到 srand(),或者使用等效的方法?
rand() 是在 2^32 之后重复还是不重复(有一些内部隐藏状态)?
谢谢
推荐答案
你可以使用 C++11 的 random 工具来生成给定大小和种子大小的随机数,尽管这个过程有点复杂,无法在这里总结.
You can use C++11's random facilities to generate random numbers of a given size and seed size, though the process is a bit too complicated to summarize here.
例如,您可以构造一个 std::mersenne_twister<uint64_t, ...>
并用 64 位整数为其种子,然后获取指定分布内的随机数,这似乎成为您正在寻找的东西.
For example, you can construct an std::mersenne_twister<uint64_t, ...>
and seed it with a 64-bit integer, then acquire random numbers within a specified distribution, which seems to be what you're looking for.
这篇关于用于随机生成器的 64 位种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!