我在使用
concurrent.futures
和
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()
我的模拟结果如下:
python teste.py
0.590820857053
0.590820857053
0.590820857053
0.590820857053
0.890384312465
0.890384312465
0.890384312465
0.890384312465
0.391709923204
0.391709923204
如果我删除
sleep
函数中的函数
calc_g()
,结果似乎有点随机:
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从主程序生成fork,并将相同的种子复制到子进程。由于生成种子后,随机数的生成过程是确定的,因此
np.random.uniform()
都是一样的。
有人能用例子更好地解释这一点吗?
我应该如何使用
NP随机的
在平行任务中模拟硬币投掷的随机性?