Random Numbers

Uniform Distribution

Random Integer

randint() Return random integers from low (inclusive) to high (exclusive)

np.random.randint( low )                  # generate an integer, i, which         i < low
np.random.randint( low, high )            # generate an integer, i, which  low <= i < high
np.random.randint( low, high, size=1)     # generate an ndarray of integer, single dimension
np.random.randint( low, high, size=(r,c)) # generate an ndarray of integer, two dimensions

In [101]:

np.random.randint( 10 )

Out[101]:

4

In [102]:

np.random.randint( 10, 20 )

Out[102]:

18

In [103]:

np.random.randint( 10, high=20, size=5)   # single dimension

Out[103]:

array([19, 14, 17, 14, 10])

In [104]:

np.random.randint( 10, 20, (3,5) )        # two dimensions

Out[104]:

array([[16, 12, 11, 16, 14],
       [17, 14, 13, 18, 10],
       [11, 13, 16, 12, 19]])

Random Float

randf() Generate float numbers in between 0.0 and 1.0

np.random.ranf(size=None)

In [105]:

np.random.ranf(4)

Out[105]:

array([ 0.33704004,  0.09189608,  0.25174427,  0.75178433])

uniform() Return random float from low (inclusive) to high (exclusive)

np.random.uniform( low )                  # generate an float, i, which         f < low
np.random.uniform( low, high )            # generate an float, i, which  low <= f < high
np.random.uniform( low, high, size=1)     # generate an array of float, single dimension
np.random.uniform( low, high, size=(r,c)) # generate an array of float, two dimensions

In [106]:

np.random.uniform( 2 )

Out[106]:

1.3915417901060765

In [107]:

np.random.uniform( 2,5, size=(4,4) )

Out[107]:

array([[ 4.84258254,  2.49007695,  4.64296637,  3.46953041],
       [ 4.38261131,  2.03138784,  3.34808228,  4.89474556],
       [ 3.78168068,  4.16705326,  2.1949934 ,  4.5061449 ],
       [ 3.38789493,  3.68116316,  3.30409811,  3.44727418]])

Normal Distribution

random.standard_normal( size=None )                #default to mean = 0, stdev = 1
random.normal         ( loc=0, scale=1, size=None) # loc = mean, scale = stdev, size = dimension

Standard Normal Distribution

In [108]:

np.random.standard_normal((3,3))

Out[108]:

array([[ 1.58957408, -0.07234321,  0.98613347],
       [-1.13732018,  0.65923266,  1.71813249],
       [ 0.58190391, -0.75181137,  0.8793038 ]])

Normal Distribution

In [109]:

np.random.seed(125)
np.random.normal(size=5)  # standard normal, mean = 0, stdev = 1

Out[109]:

array([-0.69883694,  0.01062308, -0.94678644,  0.32872998,  0.31506457])

In [110]:

np.random.normal( loc = 12, scale=1.25, size=(3,3))

Out[110]:

array([[ 11.49647195,   8.70837035,  12.25246312],
       [ 11.49084235,  15.35870969,  10.89512663],
       [ 11.49541384,  12.12740461,  13.49497678]])

Observe: standard_normal() and normal() are the same when there is no parameters in normal()

In [111]:

np.random.seed(15)
print (np.random.normal ( size = 5 ))
np.random.seed(15)
print (np.random.standard_normal (size=5))
[-0.31232848  0.33928471 -0.15590853 -0.50178967  0.23556889]
[-0.31232848  0.33928471 -0.15590853 -0.50178967  0.23556889]

results matching ""

    No results matching ""