并行处理的最佳种子 [英] Best seed for parallel process
问题描述
我需要在不同的机器上并行运行MonteCarlo仿真.该代码是用c ++编写的,但是该程序是使用python脚本设置和启动的,该脚本设置了很多东西,尤其是随机种子. setseed函数可以获取4个字节的无符号整数
I need to run a MonteCarlo simulations in parallel on different machines. The code is in c++, but the program is set up and launched with a python script that set a lot of things, in particular the random seed. The function setseed thake a 4 bytes unsigned integer
使用简单
import time
setseed(int(time.time()))
不太好,因为我将作业提交到集群中的队列中,它们保持待命状态几分钟,然后开始运行,但是启动时间是难以置信的,可能是两个作业同时启动(秒) ,所以我切换到:
is not very good because I submit the jobs to a queue on a cluster, they remain pending for some minutes then they starts, but the start time is impredicible, it can be that two jobs start at the same time (seconds), so I switch to:
setseet(int(time.time()*100))
但是我不高兴.最好的解决方案是什么?也许我可以结合以下信息:时间,机器ID,进程ID.还是最好的解决方案是从/dev/random(Linux机器)中读取?
but I'm not happy. What is the best solution? Maybe I can combine information from: time, machine id, process id. Or maybe the best solution is to read from /dev/random (linux machines)?
如何从/dev/random中读取4个字节?
How to read 4 bytes from /dev/random?
f = open("/dev/random","rb")
f.read(4)
给我一个字符串,我想要一个整数!
give me a string, I want an integer!
推荐答案
从/dev/random
读取是一个好主意.只需将4个字节的字符串转换为整数:
Reading from /dev/random
is a good idea. Just convert the 4 byte string into an Integer:
f = open("/dev/random","rb")
rnd_str = f.read(4)
使用struct:
import struct
rand_int = struct.unpack('I', rnd_string)[0]
更新需要大写字母I.
或相乘并加:
rand_int = 0
for c in rnd_str:
rand_int <<= 8
rand_int += ord(c)
这篇关于并行处理的最佳种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!