2

Here is the code using multiple cores to print random values:

import multiprocessing
import numpy as np
import random

def print_map(_):
    print(np.random.rand(1))
    # print (random.uniform(0, 1))
num_data = 8
cores = 8
pool = multiprocessing.Pool(processes=cores)
print_random = pool.map(print_map, np.arange(num_data))

The random.uniform can produce different random values. But some of the values numpy.random.rand produces are always the same.

For example, the numpy module produces:

[ 0.13633318]
[ 0.13633318]
[ 0.13633318]
[ 0.21356165]
[ 0.13633318]
[ 0.13633318]
[ 0.13633318]
[ 0.13633318]

And the random module produces:

0.608248187123
0.624808314139
0.578712513812
0.184301478758
0.297702915307
0.886168638762
0.236475271032
0.302152315241

Has anybody had this problem? I really want to know why this happens.

Pek
  • 166
  • 1
  • 15
ChangGao
  • 21
  • 2
  • Possible duplicate of [Same output in different workers in multiprocessing](https://stackoverflow.com/questions/12915177/same-output-in-different-workers-in-multiprocessing) – MB-F Jan 08 '18 at 07:40
  • Yeah, this makes sense. Thanks a lot !! – ChangGao Jan 09 '18 at 01:02

0 Answers0