Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Numpy: Random number in a loop

I have such code and use Jupyter-Notebook

for j in range(timesteps):    
    a_int = np.random.randint(largest_number/2) # int version

and i get random numbers, but when i try to move part of code to the functions, i start to receive same number in each iteration

def create_train_data():        
    np.random.seed(seed=int(time.time()))     
    a_int = np.random.randint(largest_number/2) # int version
    return a

for j in range(timesteps):    
    c = create_train_data()  

Why it's happend and how to fix it? i think maybe it because of processes in Jupyter-Notebook

like image 358
Vladimir Shebuniayeu Avatar asked Sep 17 '25 16:09

Vladimir Shebuniayeu


1 Answers

The offending line of code is

np.random.seed(seed=int(time.time()))

Since you're executing in a loop that completes fairly quickly, calling int() on the time reduces your random seed to the same number for the entire loop. If you really want to manually set the seed, the following is a more robust approach.

def create_train_data():   
    a_int = np.random.randint(largest_number/2) # int version
    return a

np.random.seed(seed=int(time.time()))
for j in range(timesteps):
    c = create_train_data()

Note how the seed is being created once and then used for the entire loop, so that every time a random integer is called the seed changes without being reset.

Note that numpy already takes care of a pseudo-random seed. You're not gaining more random results by using it. A common reason for manually setting the seed is to ensure reproducibility. You set the seed at the start of your program (top of your notebook) to some fixed integer (I see 42 in a lot of tutorials), and then all the calculations follow from that seed. If somebody wants to verify your results, the stochasticity of the algorithms can't be a confounding factor.

like image 99
Hans Musgrave Avatar answered Sep 20 '25 06:09

Hans Musgrave