vendredi 24 avril 2020

Why does RandomState with a set seed generate new numbers each time?

I'm using numpy to set a random state for my RandomState object, but finding that when calling the randn() method on the object I sometimes get new "random" numbers each time I call randn(). By setting the random seed I'd expect to get the same results each time, as the numpy documentation says the following:

A fixed seed and a fixed series of calls to ‘RandomState’ methods using the same parameters will always produce the same results.

I've noticed that if i initialize rng in the same cell as the cell where I create random_y, the results remain consistent (same arrays generated each time). However, if I initialize rng in a different cell, the values of random_y change. For example:

##cell 1:
import numpy as np
rng = np.random.RandomState(2)

#cell2:
x = np.linspace(0,10,500) #linspace: generate numbers a to b in steps of c, inclusive of both
random_y = rng.randn(500,6)
random_y

this will result in new random_y values each time i run the two cells. however, if i were to combine these two cells into one and re-run, random_y will remain static. why is this?




Aucun commentaire:

Enregistrer un commentaire