4

I wonder why my program doesn't creates different random number, if I use Pipe and Process to create them parallel. My code

import numpy as np
from multiprocessing import Process, Pipe

def createNumbers(conn):
    result = np.random.randint(0,2, size=(4))
    conn.send(result)

parent_conn, child_conn = Pipe()
for i in range(3):
    process = Process(target=createNumbers, args=(child_conn,))
    process.start()
    process.join()
    result = parent_conn.recv()

    print(result)

creates three identically vectors

(0, 1, 1, 1)
(0, 1, 1, 1)
(0, 1, 1, 1)

If I use ThreadPool instead, it works fine. The code

from multiprocessing.pool import ThreadPool
import numpy as np

def createNumbers():
    return np.random.randint(0,2, size=4)

for i in range(3):
    pool = ThreadPool(processes=1)
    process = pool.apply_async(createNumbers)
    result = process.get()
    print(result)

creates up to three identically vectors

(1, 0, 1, 0)
(0, 0, 1, 0)
(1, 0, 0, 0)

Has someone an idea why the program with Pipe and Process creates the same numbers?

SebastianH
  • 734
  • 5
  • 23
  • 4
    Possible duplicate of [Seeding random number generators in parallel programs](https://stackoverflow.com/questions/29854398/seeding-random-number-generators-in-parallel-programs) – MB-F Jan 25 '18 at 10:56
  • 1
    np.random.seed() is missing in your code. Refer https://stackoverflow.com/questions/12915177/same-output-in-different-workers-in-multiprocessing – Kris Jan 25 '18 at 11:09
  • Thanks for both advices. The np.random.seed() does work. I think this is a little weird that Process need to reseed and Threadpool work without it. – SebastianH Jan 25 '18 at 11:53
  • 1
    @SebastianH I guess the reason is that processes, when forking, get a copy of the same random state. While threads, in contrast, share a single random state. – MB-F Jan 25 '18 at 12:42

0 Answers0