Shiv
Shiv

Reputation: 369

np.random.seed need to be called every time

Following two version code should have given the same output. Looks like np.random.seed need to be called every time. Is it correct?

Is there any way to set seed once so that same random number generated everytime

np.random.seed(0)
for _ in range(10):
    print(np.random.randint(low=1, high=100))

Output: 45 48 65 68 68 10 84 22 37 88

for _ in range(10):
    np.random.seed(0)
    print(np.random.randint(low=1, high=100))

Output: 45 45 45 45 45 45 45 45 45 45

Upvotes: 1

Views: 1322

Answers (1)

Sefe
Sefe

Reputation: 14007

Unless you are using a cryptographically secure random number generator, random numbers are not really random. You are using a pseudo random number generator, which means you are iterating over a fixed table of predefined numbers. Seeding the random number means choosing the entry in that table you want to use first. Seeding it every time before you create a random number to the same position means you will always get the same number.

Upvotes: 2

Related Questions