Preserve 'randomness' in parallel programs

I've always drawn random numbers like,

srand(time(NULL))                      // seed with current time
...
double rnum = 1.0*rand()/RAND_MAX;     // convert to [0.0,1.0]

And I've never had any issues. I'm currently running parallel code with up to hundreds of cores, and they seem to have the same 'random' series of numbers, presumably because they're all starting at the same time (or very similar times).

What's a good way to randomize between processors?

I can access each processor's ID, so I was thinking about using something like,

srand(time(NULL)*(pG->my_id+1))        // +1 incase my_id == 0

But I don't know exactly how seeding works - so I was a little worried about an integer multiplier (especially a factor of 2?) not being 'random enough' (note: I have no specific criteria for sufficiently random).


All pseudo-random number generators yield sequences of values that eventually cycle. One abstract viewpoint is that the seed is equivalent to an entry point into the cycle. Any two different seeds represent different entry points, and should be fine as long as the cycle length is sufficiently long that the subsequences don't overlap. You'd do about as well to pick 10 sequential seed values for 10 processors as to do anything extremely fancy. For instance, here are 10 values apiece from seeding Ruby's Mersenne twister with 0, 1, and 2. As you can see, they appear to have nothing to do with each other even though the seeds were picked sequentially.

seed = 0: 0.5488135039273248 0.7151893663724195 0.6027633760716439 0.5448831829968969 0.4236547993389047 0.6458941130666561 0.4375872112626925 0.8917730007820798 0.9636627605010293 0.3834415188257777

seed = 1: 0.417022004702574 0.7203244934421581 0.00011437481734488664 0.30233257263183977 0.14675589081711304 0.0923385947687978 0.1862602113776709 0.34556072704304774 0.39676747423066994 0.538816734003357

seed = 2: 0.43599490214200376 0.025926231827891333 0.5496624778787091 0.4353223926182769 0.42036780208748903 0.3303348210038741 0.2046486340378425 0.6192709663506637 0.29965467367452314 0.26682727510286663

链接地址: http://www.djcxy.com/p/68032.html

上一篇: MaybeT m的应用实例假定Monad m

下一篇: 在并行程序中保留“随机性”