1

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?

1
  • 2
    1) Mersenne Twister 2) Seeding is PRNG-dependent and for MT there are scientific-papers available by the original authors on how to seed properly. 3) Because of A common book Commented Jul 25, 2019 at 18:48

1 Answer 1

4
  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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.