Numpy Random | Random Module

Python NumPy random module

The NumPy random is a module help to generate random numbers.

Import NumPy random module

1

2

import numpy as np # import numpy package

import random # import random module

np.random.random()

This function generates float value between 0.0 to 1.0 and returns ndarray if you will give shape.

1

2

3

4

rd_num = np.random.random(1)

rd_2D_array =  np.random.random((3,3))

print(rd_num)

print(rd_2D_array)

1

2

3

4

5

6

Output >>>

[0.4698348]

 

[[0.17440905, 0.66151053, 0.66339827],

 [0.88763943, 0.8709484 , 0.06250261],

 [0.09760232, 0.05503074, 0.55680254]]

Click here to jump on Python NumPy tutorial

np.random.randint()

The random integer function generates single random integer number from given range and if the shape will give then return ndarray.

1

2

3

4

5

6

7

rd_no = np.random.randint(1,4)

rd_2D_arr = np.random.randint(1,4, (4,4))

rd_3D_arr = np.random.randint(1,4, (2,4,4))

 

print(rd_no)

print(rd_2D_arr)

print(rd_3D_arr)

np.random.seed()

The random module generates random number but next time you want to generate the same number then seed() will help.

1

2

3

np.random.seed(10)

rd_3D_arr = np.random.randint(1,4, (2,4,4))

print(rd_3D_arr)

1

2

3

4

5

6

7

8

9

10

Output >>>

      [[[2, 2, 1, 1],

        [2, 1, 2, 2],

        [1, 2, 2, 3],

        [1, 2, 1, 3]],

 

       [[1, 3, 1, 1],

        [1, 3, 1, 3],

        [3, 2, 1, 1],

        [3, 2, 3, 2]]]

Nex time generate the same 3D array using the same seed value (10).

1

2

3

np.random.seed(10)

rd_3D_arr = np.random.randint(1,4, (2,4,4))

print(rd_3D_arr)

1

2

3

4

5

6

7

8

9

10

Output >>>

      [[[2, 2, 1, 1],

        [2, 1, 2, 2],

        [1, 2, 2, 3],

        [1, 2, 1, 3]],

 

       [[1, 3, 1, 1],

        [1, 3, 1, 3],

        [3, 2, 1, 1],

        [3, 2, 3, 2]]]

Note: The seed function accepts value up to  2^32 -1 (4294967295).

np.random.rand()

The rand() function work like random() but it accept shape and return ndarray which contain random values between 0.0 to 1.0.

1

2

arr_2D = np.random.rand(3,3) # return  3 x 3 matrix

print(arr_2D)

1

2

3

4

Output >>>

      [[0.58390137, 0.18263144, 0.82608225],

       [0.10540183, 0.28357668, 0.06556327],

       [0.05644419, 0.76545582, 0.01178803]]

np.random.randn()

The randn() function work like rand() function but it reurn samples of  standerd normalise distribution value.

1

2

arr_2D = np.random.randn(3,3)

print(arr_2D)

1

2

3

4

Output >>>

      [[-1.58494101,  1.05535316, -1.92657911],

       [ 0.69858388, -0.74620143, -0.15662666],

       [-0.19363594,  1.13912535,  0.36221796]]

np.random.choice()

If you have sequence values and want to get random single value then the random choice() function is the best choice.

1

2

3

x = [1,2,3,4] # list

choice_from_x = np.random.choice(x) # retun random single item from sequence

print(choice_from_x )

1

2

Output >>>

1

Let’s try to get the number of choice from sequence x using for loop.

1

2

for i in range(20):

    print(np.random.choice(x))

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Output >>>

2

1

4

1

1

3

1

3

3

1

4

2

1

4

2

3

3

2

3

3

np.random.permutation()

If you want to generate some permutation of sequence then use random permutation() function.

1

2

x_permute = np.random.permutation(x)

print(x_permute)

1

2

Output >>>

[2, 3, 4, 1]

Learn more about random sampling then click here