【发布时间】:2020-12-15 04:21:13
【问题描述】:
NumPy 在Parallel Random Number Generation 上的文档展示了如何使用SeedSequence 产生孙子种子(见下文)。
from numpy.random import SeedSequence, default_rng ss = SeedSequence(12345) # Spawn off 10 child SeedSequences to pass to child processes. child_seeds = ss.spawn(10) streams = [default_rng(s) for s in child_seeds]子 SeedSequence 对象也可以生成孙子,并且 很快。每个 SeedSequence 在 spawned 树中都有自己的位置 SeedSequence 对象与用户提供的种子混合以生成 独立(概率很高)流。
grandchildren = child_seeds[0].spawn(4) grand_streams = [default_rng(s) for s in grandchildren]
我的问题:
要创建下一代种子,我应该使用:
great_grandchildren = grandchildren[0].spawn(4)
great_grand_streams = [default_rng(s) for s in great_grandchildren]
还是应该总是引用child_seeds[0]:
great_grandchildren = child_seeds[0].spawn(4)
great_grand_streams = [default_rng(s) for s in great_grandchildren]
我的问题的上下文涉及实现种子和一个由concurrent.futures.ProcessPoolExecutor 对象组成的函数,该对象在可能“无休止”的while循环场景中为每个进程使用种子。我想知道下面是否是从SeedSequence 产生种子的正确方法,假设我已经使用了 NumPy 示例中提到的grandchildren 和grand_streams 术语。例如:
from numpy.random import SeedSequence, default_rng
ss = SeedSequence(12345)
# Spawn off 10 child SeedSequences to pass to child processes.
child_seeds = ss.spawn(10)
streams = [default_rng(s) for s in child_seeds]
run_func1( streams ) #child_seeds is consummed
grandchildren = child_seeds[0].spawn(4)
grand_streams = [default_rng(s) for s in grandchildren]
while True:
run_concurrent_futures_ProcessPoolExecutor_func( grand_streams )
if condition_not_met:
grandchildren = grandchildren[0].spawn(4) #Do I use grandchildren[0] or child_seeds[0] to ensure randomness?
grand_streams = [default_rng(s) for s in grandchildren]
else:
break
【问题讨论】:
标签: python numpy random-seed concurrent.futures