Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best seed for parallel process

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

Using a simple

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))

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)?

How to read 4 bytes from /dev/random?

f = open("/dev/random","rb")
f.read(4)

give me a string, I want an integer!

like image 917
Ruggero Turra Avatar asked Mar 07 '10 12:03

Ruggero Turra


1 Answers

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)

Either using struct:

import struct
rand_int = struct.unpack('I', rnd_string)[0]

Update Uppercase I is needed.

Or multiply and add:

rand_int = 0
for c in rnd_str:
    rand_int <<= 8
    rand_int += ord(c)
like image 127
ebo Avatar answered Sep 18 '22 19:09

ebo