Mencia
Mencia

Reputation: 898

How to use random.seed with random.randn?

I can seed an intenteger random number generator by doing:

import random    
random.seed(9002)   
random.randint(1, 10)  

and the same integer number is generated every time.

On the other hand, when I try to do the same to generate real numbers, the seed is not fixing the generated number to the same value every time.

import random   
random.seed(9001)
np.random.randn(1)

How can I seed np.random.randn ?

Upvotes: 0

Views: 3545

Answers (2)

Zack Amin
Zack Amin

Reputation: 536

import numpy as np
from numpy.random import randn

You need to import numpy first, then randn

To call seed -

np.random.seed(101) # This can be any number.

This allows you to create an array from random numbers with the same number each time. This allows you to test outputs or check your work against a tutorials output.

Upvotes: 0

matt
matt

Reputation: 12347

Seed numpy's random number generator.

np.random.seed(0)
np.random.randn(1)

That should always produce the same array.

Upvotes: 2

Related Questions