Generate Random Numbers in Python

This tutorial shows how you can use Numpy to generate random numbers in Python. The following is the basic syntax summarizing 3 functions.

1. Integers:

np.random.randint()

2. Normal distribution:

np.random.randn()

3. Uniform distribution:

np.random.rand()


Example 1: Integer

np.random.randint(low, high=None, size=None, dtype=int)

np.random.randint() will return integer numbers. Given that there are quite a few parameters in randint(), it is better to specify low, high, and size in the statement.

import numpy as np

# Without size information, it will just generate a random integer
Array_1 = np.random.randint(low=2, high=90)
print("First Array: \n", Array_1)

# The following has the size of 10 numbers
Array_2 = np.random.randint(low=2, high=90, size=10) 
print("Second Array: \n", Array_2)

# The following has the shape mentioned
Array_3 = np.random.randint(low=2,high=90,size=(3,4)) 
print("Third Array: \n", Array_3)

The following is the output showing Array_1, Array_2, and Array_3 from randint() in Numpy.

First Array: 
 53

Second Array: 
 [56 79 71 15 27 15 88 32 32 14]

Third Array: 
 [[67 33 59 38]
 [29 20 79 24]
 [25 13 30 76]]

Example 2: Standard Normal Distribution

np.random.randn(d0, d1, …, dn)

np.random.randn() will return data of a standard normal distribution (i.e., mean=0 and variation = 1.).

d0d1dn denotes the size in each dimension. If no d0d1dn is given, a single Python float is returned.

import numpy as np

# Without argument, it will return a single floating point number
Array_1 = np.random.randn()
print("First Array: \n", Array_1)

# d0=10, which returns an array with a size of 10
Array_2 = np.random.randn(10) 
print("Second Array: \n", Array_3)

# d0=3, d1=4, size of 3x4 array
Array_3 = np.random.randn(3,4) 
print("Third Array: \n", Array_3)

The following are the outputs of Array_1, Array_2, and Array_3 from randn() in Numpy.

First Array: 
 0.06768179188386116

Second Array: 
 [-1.39432412e+00 -4.31202971e-01 -2.69721318e+00  6.02813442e-01
 -3.71431990e-01  1.49630972e+00  5.38674312e-04  2.17072606e-01
  2.39233874e-01 -5.64958800e-01]

Third Array: 
 [[ 1.17880526 -0.14422117 -0.09097434  0.22683969]
 [-0.10747543  0.68855861 -0.91183705  2.06881669]
 [ 1.23845381 -0.87994308  0.41059549 -0.55673435]]

Example 3: Uniform Distribution

np.random.rand(d0d1dn)

np.random.rand() will return data following uniform distribution (i.e., in the range of [0,1)).

d0d1dn denotes the size in each dimension. If no d0d1dn is given, a single Python float is returned.

import numpy as np

# without any argument, it will return a single floating point number in the range of [0, 1)
Array_1 = np.random.rand() 
print("First Array: \n", Array_1 )

# d0=5; generate 5 numbers in the range of [0, 1)
Array_2 = np.random.rand(5) 
print("Second Array: \n", Array_2)

# d0=3, d1=4, size of 3x4 array; generate 12 numbers in the range of [0, 1)
Array_3 = np.random.rand(3,4) 
print("Third Array: \n", Array_3)

The following are the outputs of Array_1, Array_2, and Array_3 from rand() in Numpy.

First Array: 
 0.11727791343839256

Second Array: 
 [0.71103727 0.63259931 0.54372395 0.59553258 0.62353549]

Third Array: 
 [[0.18877663 0.26831789 0.2800152  0.09025151]
 [0.63440239 0.09823381 0.2045029  0.94778125]
 [0.23219263 0.10362962 0.86029194 0.89346368]]

Further Reading

The following tutorial is specifically about generating a random sample of normal distribution in Python.

How to generate a sample of normal distribution

Leave a Comment