corey979
corey979

Reputation: 141

How to generate a different set of random numbers in each iteration of a prallelized For loop?

The following problem arised directly due to applying the answer to this question. In the minimal working example (MWE) there's a place in the myscript definition where I generate some random numbers, then perform some operations on them, and fnally write the output to a file. When this code is un-parallelized, it works correct. However, if it's parallel (I'm testing it on a 2-core machine, and have two threads at a time), when I want to perform 4 iterations (boot) I get twice the same output (i.e., among four outputs I get only two distinct numbers, not four as expected). How can this be fixed?

MWE:

import random
import math
import numpy as np
import multiprocessing as mp
from multiprocessing import Pool

boot = 4
RRpoints = 278

def myscript(iteration_number):
    RRfile_name = "outputRR%d.txt" % iteration_number
    with open(RRfile_name, "w") as RRf:

        col1 = np.random.uniform(0 , 1 , RRpoints)
        col2 = np.random.uniform(0 , 1 , RRpoints)
        sph1 = [i * 2 * math.pi for i in col1]
        sph2 = [math.asin(2 * i - 1) for i in col2]

        for k in xrange(0 , RRpoints):
            h = 0           
            mltp = sph1[k] * sph2[k]
            h += mltp

        RRf.write("%s\n" % h)

x = xrange(boot)
p = mp.Pool()
y = p.imap(myscript, x)
list(y)

Upvotes: 0

Views: 251

Answers (0)

Related Questions