Search code examples
pythonnumpyrandomnormal-distributionrandom-seed

How to keep the sequence of random numbers (normal distributions) the same? I tried random.seed(), but it didn't work


I'm trying to write some replicable Monte Carlo simulation, and need to fix the seed for the random number generator (so that when other people run it, they get exactly the same result). I tried the following codes

import numpy as np
import random
random.seed(1)
N=10
mu=[0]
sig=[[1]]
a=np.random.multivariate_normal(mu, sig, N)
print(a)

But each time I run the code, it prints a different sequence. How could this be fixed? Thanks!


Solution

  • random and np.random aren't the same. If you use np.random then use np.random.seed.