Michael Ji
Michael Ji

Reputation: 167

The random number generator in numpy

I am using the numpy.random.randnand numpy.random.randto generate random numbers. I am confusing about the difference between random.randn and random.rand?

Upvotes: 3

Views: 5136

Answers (2)

Srivatsan
Srivatsan

Reputation: 9363

The main difference between the two is mentioned in the docs. Links to Doc rand and Doc randn

For numpy.rand, you get random values generated from a uniform distribution within 0 - 1

But for numpy.randn you get random values generated from a normal distribution, with mean 0 and variance 1.

Just a small example.

>>> import numpy as np
>>> np.random.rand(10)
array([ 0.63067838,  0.61371053,  0.62025104,  0.42751699,  0.22862483,
        0.75287427,  0.90339087,  0.06643259,  0.17352284,  0.58213108])
>>> np.random.randn(10)
array([ 0.19972981, -0.35193746, -0.62164336,  2.22596365,  0.88984545,
       -0.28463902,  1.00123501,  1.76429108, -2.5511792 ,  0.09671888])
>>> 

As you can see that rand gives me values within 0-1,

whereas randn gives me values with mean == 0 and variance == 1

To explain further, let me generate a large enough sample:

>>> a = np.random.rand(100)
>>> b = np.random.randn(100)
>>> np.mean(a)
0.50570149531258946
>>> np.mean(b)
-0.010864958465191673
>>>

you can see that the mean of a is close to 0.50, which was generated using rand. The mean of b on the other hand is close to 0.0, which was generated using randn

Upvotes: 3

Pawel
Pawel

Reputation: 41

You can also get a conversion from rand numbers to randn numbers in Python by the application of percent point function (ppf) for the Normal Distribution with random variables distributed ~ N(0,1). It is a well-known method of projecting any uniform random variables (0,1) onto ppf in order to get random variables for a desired cumulative distribution.

In Python we can visualize that process as follows:

from numpy.random import rand
import matplotlib.pyplot as plt
from scipy.stats import norm

u = rand(100000)   # uniformly distributed rvs
z = norm.ppf(u)    # ~ N(0,1) rvs

plt.hist(z,bins=100)
plt.show()

enter image description here

Upvotes: 0

Related Questions