来自单个种子的多个独立随机数流 [英] Multiple independent random number streams from single seed
问题描述
我分别使用m_i
伪随机数流进行了n
类似的分析(m_i
在分析之间可能有所不同).每个分析都有其自己的随机数种子,因此每个分析之间的随机数不相关.
I have n
similar analyses each using m_i
pseudo-random number streams (m_i
may vary between analyses). Each analysis has its own random number seed so that the random numbers are uncorrelated between analyses.
我的问题是我需要从单个种子创建m_i
流.该分析当前使用Numpy编写,因此其Mersenne Twister解决方案非常理想,但我欢迎其他成熟库中的解决方案.我考虑了以下可能性:
My problem is that I need to create the m_i
streams from the single seed. The analysis is currently written in Numpy, so solutions its Mersenne Twister are ideal, but I am open to solutions in other mature libraries. I considered these possibilities:
-
使用种子创建随机数流,绘制
m_i
整数,并将这些整数用作m_i
随机流的种子. 生日悖论.有 2 ^ 32 (〜4十亿)个种子,但是如果我在2 ^ 16(〜60000)之后遇到碰撞(两个流从同一个种子开始).
Use the seed to create a random number stream, draw
m_i
integers, and use those integers as the seeds form_i
random streams. This is not good because of the birthday paradox. There are 2^32 (~4 billion) seeds, but if I would end up with a collision (two streams started with the same seed) after 2^16 (~60000).
将种子与每个流索引的某个常数m_max
乘以1,以获取该流的种子.(例如,对于seed=2
和m_max=10000
,分析将使用种子20001、20002、20003等).这是不希望的,因为在发生冲突之前,所有分析都将仅限于m_max
流,并且如果m_max
太大,则分析数量将仅限于2^32/m_max
.
Multiply the seed by some constant m_max
for each stream index by 1 to get that stream's seed. (e.g. with seed=2
and m_max=10000
, the analysis will use seeds 20001, 20002, 20003, etc.). This is undesirable because all analyses will be limited to m_max
streams before there will be collisions, and if m_max
is too big, the number of analyses is limited to 2^32/m_max
.
使用种子创建随机数流,绘制 624 每个流所需的32位 integers ,并设置每个流的状态 这似乎是完美的,除了我不知道624个随机整数是否实际上是梅森·Twister的有效内部状态(可以是任意位吗?).我也不知道这些整数之间是否存在任何隐藏的相关性(也许它们只是在624处移动了相同的流).
Use the seed to create a random number stream, draw 624 32-bit integers per stream needed, and set the state of each stream to the 624 integers drawn for it. This seems to be perfect except that I have no idea if 624 random integers is actually a valid internal state for the Mersenne Twister (can it be arbitrary bits?). I also don't know if there is any hidden correlation between the integers (maybe they are the same stream just shifted by 624).
是否有标准的方法?
推荐答案
方法3可以正常工作,因为任何PRNG的种子可以与PRNG的状态一样长(例如,梅森·Twister的状态长度为19968位,或624 * 32位,因此最多可以接受那么多位的种子-不限于32位或64位,这是许多实现Mersenne Twister的API的做法.但是,您应该使用与Mersenne Twister无关的PRNG(例如PCG)来播种该PRNG,然后根据您的建议绘制624整数种子. (或者,如果您不需要可重复的结果,或者如果您将保存以这种方式生成的624个整数种子,则可以使用加密的RNG(例如os.urandom()
或secrets.SystemRandom
)来绘制这些种子.) a href ="https://peteroupc.github.io/random.html#Examples_and_Non_Examples" rel ="nofollow noreferrer">我在RNG上的文章提出了几种不同设计的PRNG.
Approach 3 can work, as the seed for any PRNG can be as long as that PRNG's state (for example, Mersenne Twister has a state length of 19968 bits, or 624 * 32 bits, so can accept a seed of up to that many bits — it's not limited to 32 or 64 bits, as is the practice with many APIs that implement Mersenne Twister). However you should use a PRNG of an unrelated design to Mersenne Twister, such as PCG, to seed that PRNG, then draw the 624-integer seed as you suggest. (Or, if you don't require reproducible results or if you will save the 624-integer seeds generated this way, you can use a cryptographic RNG, such as os.urandom()
or secrets.SystemRandom
, to draw those seeds instead.) My article on RNGs suggests several PRNGs with different designs.
更新(2019年12月1日):
UPDATE (Dec. 1, 2019):
如果您使用的是NumPy,请注意,与此同时,NumPy 1.17引入了新的随机数生成系统;它使用所谓的位生成器(例如PCG)和随机生成器(例如新的numpy.random.Generator
).这是建议更改RNG政策. NumPy文档现在具有有关并行播种RNG 的详细信息. ,以及多线程RNG .在"非加密PRNG的种子生成中,我也有关于播种多个进程(不是特定于NumPy)的常规信息. ."
If you're using NumPy, please note that in the meantime, NumPy 1.17 introduces a new random number generation system; it uses so-called bit generators, such as PCG, and random generators, such as the new numpy.random.Generator
. It was the result of a proposal to change the RNG policy. The NumPy documentation now has detailed information on seeding RNGs in parallel, and on multithreading RNGs. I also have general information on seeding multiple processes (not NumPy-specific) in "Seed Generation for Noncryptographic PRNGs."
这篇关于来自单个种子的多个独立随机数流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!