Why does "numpy.random.rand " produce the same values in different cores?

ChangGao: <p>Hi guys, I’m asking a question about using multipleprocessing module to print random numbers.</p>
<p>When I was using the “numpy.random.rand” produce random numbers, I found that some of the produced values from different cores are the same. But the random.uniform can work just ok.</p>
<p>Here is the code.</p>
<pre><code>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))
</code></pre>
<p>The “random” module produces<br>
0.608248187123<br>
0.624808314139<br>
0.578712513812<br>
0.184301478758<br>
0.297702915307<br>
0.886168638762<br>
0.236475271032<br>
0.302152315241</p>
<p>But the “numpy” module produces<br>
[ 0.13633318]<br>
[ 0.13633318]<br>
[ 0.13633318]<br>
[ 0.21356165]<br>
[ 0.13633318]<br>
[ 0.13633318]<br>
[ 0.13633318]<br>
[ 0.13633318]</p>
<p>I really want to know how this could happen.<br>
Thanks a lot !</p>

jpeg729: <p>Presumably numpy duplicates its random number generator once per process, if so then each process uses the same random seed. At a guess the fourth result is different because that thread’s setup stage was delayed.</p>
<p>If you really want different random numbers every time, you could call <code>numpy.random.seed(random.randint(some_large_integer))</code> in <code>print_map</code></p>

ChangGao: <p>Thanks ! In your way the “numpy” module can produce different numbers.</p>
<p>But I still couldn’t find where the random number generator was duplicated. Actually the data I produced several days ago using “numpy” seemed to have no such problems.</p>
<p>I don’t know if this has a relationship with the recent update of my Ubuntu. T_T</p>

jpeg729: <p>I can only guess that maybe a recent update of numpy changed the handling of the random state when running multiprocess stuff.</p>

ChangGao: <p>Yeah … Thanks again !</p>

smth: <p>See this comment: <a href="https://discuss.pytorch.org/t/does-getitem-of-dataloader-reset-random-seed/8097/7?u=smth">Does __getitem__ of dataloader reset random seed?</a></p>

ChangGao: <p>Yeah! That really helps a lot! Thank you very much!!</p>

jia_lee: <p>If we use random rather than np.random, everything will be ok without setting seeds every multi-process?</p>

ChangGao: <p>I think so. You can have a try like this:<br>
The main reason is resulted from the fact that no seed is input to the <code>numpy.random.seed()</code>. Providing a seed for each process can also be a solution.</p>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/29854398/seeding-random-number-generators-in-parallel-programs" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/1702972/overcomer" target="_blank" rel="nofollow noopener">
    <img alt="overcomer" src="https://i.stack.imgur.com/0nMgL.png?s=128&amp;g=1" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/29854398/seeding-random-number-generators-in-parallel-programs" target="_blank" rel="nofollow noopener">Seeding random number generators in parallel programs</a>
</h4>

<div class="tags">
  <strong>python, numpy, random, multiprocessing</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/1702972/overcomer" target="_blank" rel="nofollow noopener">
    overcomer
  </a>
  on <a href="https://stackoverflow.com/questions/29854398/seeding-random-number-generators-in-parallel-programs" target="_blank" rel="nofollow noopener">05:59PM - 24 Apr 15 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>