# 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)>>> aarray([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)>>> aarray([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)>>> aarray([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)>>> aarray([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`
`>>> 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

`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

`==================== =========================================================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.`
`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

--

--

## More from Jimmy (xiaoke) Shen

Data Scientist/MLE/SWE @takemobi

Love podcasts or audiobooks? Learn on the go with our new app.