mhsnk
mhsnk

Reputation: 222

Random number generation algorithm used in Numpy

What is algorithm that Python Numpy uses to generate random numbers? More specifically, what is the algorithm that is used when we invoke

np.random.uniform() 

Is Linear congruential generator random generation technique being used? What happens when we type

np.random.seed(42)

and why are some numbers like 42 are more popular with np.random.seed?

Upvotes: 1

Views: 1482

Answers (1)

Hans Musgrave
Hans Musgrave

Reputation: 7111

  1. Numpy uses the Mersenne Twister (as does the cpython random module).
  2. When you seed the random number generator you're choosing its current state (a PRNG chooses its next state based on its current state and chooses its current value as a function of its current state. Some other PRNG's simply use the identity function to generate a value from a state).
  3. The choice of seed doesn't matter much except that it be the same across multiple runs to ensure reproducibility. The number 42 is commonly chosen any time an arbitrary number needs to be picked, especially in nerdier social circles, because of its position as "the answer to life, the universe, and everything" according to a popular book. Other small, memorable numbers are often chosen for seeds as well.
  4. Edit: Reproducibility isn't the only goal of a seed choice (e.g., in cryptographic settings you often want something like the opposite of that -- that every machine is initialized with independent seeds, and the quality of some PRNG's depends on the seed choice), but in the context of a weak PRNG being used for scientific purposes -- like in numpy -- the primary goal is almost always reproducibility.

Upvotes: 4

Related Questions