子进程生成相同的

子进程生成相同的

本文介绍了子进程生成相同的“随机"消息.数字作为父进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用concurrent.futuresnp.random进行仿真时遇到一些问题.

I'm having some problems with simulations using concurrent.futures and np.random.

示例:

import numpy as np
from concurrent.futures import ProcessPoolExecutor, as_completed
from time import sleep

def calc_g():
    sleep(1)
    u = np.random.uniform()
    print u

futures = {}
with ProcessPoolExecutor() as executor:

    for i in range(0,10):
        job = executor.submit(calc_g)
        futures[job] = i

    for job in as_completed(futures):
        job.result()

我在此模拟中的结果是:

My results in this simulations are:

python teste.py
0.590820857053
0.590820857053
0.590820857053
0.590820857053
0.890384312465
0.890384312465
0.890384312465
0.890384312465
0.391709923204
0.391709923204

如果我在函数calc_g()中删除了sleep函数,则结果似乎更加随机:

If I remove the sleep function in the function calc_g(), results seem to be a little more random:

python teste.py
0.116725033305
0.919465043075
0.116725033305
0.116725033305
0.608303685887
0.59397039096
0.608862016487
0.800008484487
0.689917804793
0.116725033305

我认为这与numpy使用的种子的生成有关. Python从主程序生成派生,并将相同的种子复制到子进程.由于种子生成后随机数的生成过程是确定的,因此np.random.uniform()中的值相同.

I think that it has to do with the generation of seeds that numpy uses. Python generates forks from the main program and the same seed is copied to child processes. As generation process of random numbers is deterministic after the generation of seeds, values from np.random.uniform() are the same.

有人可以举例说明吗?

我应该如何在并行任务中使用np.random来模拟抛硬币时的随机性?

How should I use np.random in parallel tasks to simulate randomness as coin tossing?

推荐答案

对于多处理中的独立PRNG流,请为每个进程提供自己的 RandomState .最简单的解决方法是,更改此行:

For independent streams of PRNGs in multiprocessing, give each process its own RandomState. The simplest fix, change this line:

u = np.random.uniform()

对此:

u = np.random.RandomState().uniform()

这篇关于子进程生成相同的“随机"消息.数字作为父进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:33