pythonnumpyrandomrandom-seed

Confusion regarding functions to generate random numbers in numpy


I am confused primarily on two points:

  1. Why are there different random number generator functions in numpy that seemingly produce the same thing? e.g. np.random.rand() vs np.random.uniform(); or np.random.randn(), np.random.normal(), np.random.standard_normal()- [I mean, why is the standard_normal() function even a thing? It is longer to type than specifying the locus =0 and scale =1 in np.random.normal() ???]

  2. What is the point of defining a random number generator at the beginning of a program, rng=np.default.random_rng() and then calling rng.normal(), rng.random() for each instance one wants to generate a random number, rather than using np.random.rand() and np.random.normal() etc in each case separately?

My idea is that it has something to do with how the random numbers are generated for both points, but I'm not at all sure.

Many thanks.


Solution

  • Regarding the first part of your question:

    Why are there different [...] functions [...] that seemingly produce the same thing?

    From the Zen of Python:

    Explicit is better than implicit.

    For example, the standard normal distribution is just a special case of normal distributions. Code is read more often than it is written, so being explicit is important in many cases.

    When it comes to the second part, I'd say it's a matter of what you need.

    The default_rng function returns a Generator that has some nice properties. For example, the ability to generate random arrays with arbitrary shapes and some specific performance optimizations. If you don't need that you might as well use the functions in numpy.random directly, I think.