ipython - The random number generator in numpy -
i using numpy.random.randnand numpy.random.randto generate random numbers. confusing difference between random.randn , random.rand?
the main difference between 2 mentioned in docs. links doc rand , doc randn
for numpy.rand, random values generated uniform distribution within 0 - 1
but numpy.randn random values generated normal distribution, with mean 0 , variance 1.
just small example.
>>> import numpy 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 can see rand gives me values within 0-1,
whereas randn gives me values mean == 0 , variance == 1
to explain further, let me generate large enough sample:
>>> = np.random.rand(100) >>> b = np.random.randn(100) >>> np.mean(a) 0.50570149531258946 >>> np.mean(b) -0.010864958465191673 >>> you can see mean of a close 0.50, generated using rand. mean of b on other hand close 0.0, generated using randn
Comments
Post a Comment