×

NumPy Random Module

A random number is a number from a sequence or a distribution, whose future occurrence cannot be predicted based on any past or present data over a defined interval or set. A pseudo-random number is a set of values or elements that is statistically random, but it is derived from a known starting point and is typically repeated over and over.  

In Computer Science and Statistics we deal with the pseudo-random number. It is called “pseudo” because the algorithm can repeat the sequence, and then the numbers are thus not entirely random.

The NumPy package has numpy.random module which has functions to produce pseudo-random numbers and arrays.

  • randint()
  • rand()
  • choice()
  • random_sample()
  • random()
  • bytes()
  • shuffle()
  • permutation()

randint():

The randint() function of numpy.random returns a random integer value or a sequence of integers within a specified range.

Syntax:

random.randint(low, high=None, size=None)

The low and high parameters are the given range of random number generation(low is inclusive and high is exclusive). The size parameter is the number of elements in the output array(if an integer value is given) or can be the shape of output array( if a tuple of integer values is given).

import numpy as np
print(np.random.randint(2, 12))
#Output:
9

print(np.random.randint(2, 12, size = (2, 3)))
#Output:
[[5 9 9]
 [4 3 9]]

rand():

The rand() function of numpy.random creates a random sequence of float values of a specified array shape from a uniform distribution over the range(0, 1).

Syntax:

random.rand(d0, d1, ..., dn)

The d0, d1, …, dn parameters are the shape or dimensions of the output array(takes integer values and should not be negative).

float_arr = np.random.rand(3, 2)
print(float_arr)

#Output:
[[0.11161499 0.34470339]
 [0.17216142 0.47919114]
 [0.17894259 0.02541886]]

choice():

The choice() function returns a random value or sequence of random values from an array of values.

Syntax:

random.choice(input_array, size=None, replace=True, p=None)

The input_array parameter is the input array, and if a single integer is given then it implies the same as calling np.arange() function for that integer value. The size parameter is the shape of the output array.

The replace parameter is True(default) implies that the values of a can be selected multiple times. The p parameter is the probabilities of each element in the input array(the sum of probabilities should be 1).

input_array = np.array([2, 3, 5, 6, 7, 11, 12, 13, 15])

print(np.random.choice(input_array, size=(2, 2), replace = False, 
                p = [0.1, 0.2, 0.3, 0.1, 0.05, 0.05, 0.025, 0.025, 0.15]))
#Output:
[[13 15]
 [ 3  5]]

print(np.random.choice(5, 3))
#Output:
[2 2 4]

random_sample() and random():

  • random_sample(): The random_sample() function returns a random sequence of float values from a continuous uniform distribution over (0, 1).
  • random(): The random() function returns a random float value or sequence of float values between (0, 1). The random() function is similar to random_sample() function.

For both functions, the size parameter is the shape of the output array.

print(np.random.random_sample((2, 5)))
#Output:
[[0.3280204  0.53512278 0.0287408  0.79980645 0.22939151]
 [0.9978283  0.26280173 0.08264888 0.05430897 0.50705565]]

#sample between the interval (-3,0)
print(3 * np.random.random_sample(4) - 3)
#Output:
[-1.89155089 -2.80983072 -1.96911831 -0.09584475]

print(np.random.random(3))
#Output:
[0.31730069 0.85362228 0.62390946]

bytes():

The bytes() function returns a string of random bytes of the specified length.

Syntax:

random.bytes(length)

The length parameter is the number of random bytes.

print(np.random.bytes(5))

#Output:
b'\xbc\xb45\xcaI'

shuffle():

The shuffle() function modifies an array in-place by shuffling the input array contents. The shuffle() function shuffles the array along the first axis of a multi-dimensional array, only the order of array elements is changed but the contents of the array remain the same.

Syntax:

random.shuffle(x)

The x parameter is the input array to be shuffled.

input_array = np.arange(1, 9)
print("Original array:", input_array)
#Output:
Original array: [1 2 3 4 5 6 7 8]

np.random.shuffle(input_array)

print("Shuffled array:", input_array)
#Output:
Shuffled array: [5 8 4 3 7 2 6 1]

permutation():

The permutation() function returns a randomly permuted range(shuffles) of the input array contents. If the input array is multi-dimensional, then the permutation() function only shuffles along with its first index.

Syntax:

random.permutation(x)

The x parameter is the input array. If x is an integer then it implies np.arange(x) and if x is an array then the permutation() function makes a copy of x and shuffles the elements of x randomly.

input_array = np.arange(9).reshape(3, 3)
print("Original array:\n", input_array)
#Output:
Original array:
 [[0 1 2]
 [3 4 5]
 [6 7 8]]

print("Shuffled array:\n", np.random.permutation(input_array))
#Output:
Shuffled array:
 [[3 4 5]
 [0 1 2]
 [6 7 8]]