How np.random.seed and np.random.randn works

Jimmy (xiaoke) Shen
3 min readMar 10, 2022

--

Base on [1]:

numpy.random.seed(seed=None)

This method is called when RandomState is initialized. It can be called again to re-seed the generator. For details, see RandomState.

Parameters:seed : int or 1-d array_like, optional

Seed for RandomState. Must be convertible to 32 bit unsigned integers.

How does it work under the hood?

From [2]

How to use it?

For bellow, if we generate a random numbers which has the shape of 20, it will always give us the same outputs if use a fixed random seed.

Not use fixed random seed

>>> import numpy as np
>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])>>> a = np.random.rand(20)
>>> a
array([0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998,0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041,0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])

Use fixed random seed

>>> import numpy as np
>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])Reference

rand and randn

Based on the source code from here:

rand                 Uniformly distributed values.
randn Normally distributed values

If we quick check the hist of randn by using the following code, we can get the above plot.

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.hist(np.random.randn(10000), bins=100)
>>> plt.show()

Source code of randn

Source code of randn can be found from here

Here is what the function will return

Returns        -------        Z : ndarray or float            A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from            the standard normal distribution, or a single such float if            no parameters were supplied.

Source code of np.random

You can type

>>> help(np.random)

to check the source code directly, and you will see actually the randn will be removed in the future.

==================== =========================================================Compatibilityfunctions - removedin the new API-------------------- ---------------------------------------------------------rand                 Uniformly distributed values.randn                Normally distributed values.ranf                 Uniformly distributed floating point numbers.random_integers      Uniformly distributed integers in a given range.(deprecated, use ``integers(..., closed=True)`` instead)random_sample        Alias for `random_sample`randint              Uniformly distributed integers in a given rangeseed                 Seed the legacy random number generator.

For the new API, it should be

plt.hist(np.random.normal(0, 1, 10000), bins=100)

Application

We can use a random number generator to select part of the np array.

>>> np.random.seed(42)>>> X = np.random.rand(10, 3)>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.59865848, 0.15601864, 0.15599452],[0.05808361, 0.86617615, 0.60111501],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.43194502, 0.29122914, 0.61185289],[0.13949386, 0.29214465, 0.36636184],[0.45606998, 0.78517596, 0.19967378],[0.51423444, 0.59241457, 0.04645041]])>>> mask = np.random.rand(X.shape[0])>>> maskarray([0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])>>> np.random.seed(42)>>> X = np.random.rand(10, 3)>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.59865848, 0.15601864, 0.15599452],[0.05808361, 0.86617615, 0.60111501],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.43194502, 0.29122914, 0.61185289],[0.13949386, 0.29214465, 0.36636184],[0.45606998, 0.78517596, 0.19967378],[0.51423444, 0.59241457, 0.04645041]])>>> mask = np.random.rand(X.shape[0])>>> maskarray([0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])>>> mask = mask > 0.5>>> sum(mask)5>>> X = X[mask]>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.45606998, 0.78517596, 0.19967378]])>>> maskarray([ True, False, False,  True,  True,  True, False, False,  True,False])

Reference

[1]https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.seed.html

[2]https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.RandomState.html#numpy.random.RandomState

--

--

No responses yet