[Python] How does NumPy initialize its random seed? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    When I use multiprocessing to create some processes, it seems that processes have the same state. How does it work?

    I want each process has a different state, so that the data created by numpy.random is independent. One way is to use pid as seed. Is there a more elegant approach?

    from multiprocessing import Pool

    import numpy as np
    import os


    def f(x):
    print(f'{os.getpid()}: {np.random.get_state()}')
    print(f'{os.getpid()}: {np.random.rand(5)}')


    if __name__ == '__main__':
    with Pool(5) as p:
    p.map(f, [None] * 5)

    Continue reading...

Compartilhe esta Página