Random numbers in NumPy

What are random numbers?

Random numbers do not mean that the numbers are different each time. Random means something that cannot be logically predicted.

Pseudo-random and true random

Computers work on programs, and programs are authoritative instruction sets. Therefore, this means that there must be some algorithm to generate random numbers.

If there is a program to generate random numbers, it can be predicted, so it is not a true random number.

Random numbers generated by algorithms are called pseudo-random numbers.

Can we generate true random numbers?

Yes. To generate a true random number on our computer, we need to obtain random data from some external source. External sources are usually our keystrokes, mouse movements, network data, etc.

We do not need true random numbers unless they are related to security (such as encryption keys) or the basis of the application is randomness (such as digital roulette wheels).

In this tutorial, we will use pseudo-random numbers.

Generate random numbers

NumPy provides the random module to handle random numbers.

Example

Generate a random integer between 0 and 100:

from numpy import random
x = random.randint(100)
print(x)

Run Instance

Generate random floating-point numbers

The random module rand() The method returns a random floating-point number between 0 and 1.

Example

Generate a random floating-point number between 0 and 100:

from numpy import random
x = random.rand()
print(x)

Run Instance

Generate random arrays

In NumPy, we can use the two methods from the above example to create random arrays.

integer

randint() The method accepts size Parameter, in which you can specify the shape of the array.

Example

Generate a 1-D array containing 5 random integers between 0 and 100:

from numpy import random
x=random.randint(100, size=(5))
print(x)

Run Instance

Example

Generate a 2-D array with 3 rows, each containing 5 random integers between 0 and 100:

from numpy import random
x = random.randint(100, size=(3, 5))
print(x)

Run Instance

Floating-point number

rand() The method also allows you to specify the shape of the array.

Example

Generate a 1-D array containing 5 random floating-point numbers:

from numpy import random
x = random.rand(5)
print(x)

Run Instance

Example

Generate a 2-D array with 3 rows, each containing 5 random numbers:

from numpy import random
x = random.rand(3, 5)
print(x)

Run Instance

Generate Random Numbers from an Array

choice() The method allows you to generate random values based on an array of values.

choice() The method takes an array as a parameter and randomly returns one of the values.

Example

Return one of the values in the array:

from numpy import random
x = random.choice([3, 5, 7, 9])
print(x)

Run Instance

choice() The method also allows you to return an array of values.

Please add a size Parameters specify the shape of the array.

Example

Generate a two-dimensional array composed of values from the array parameters (3, 5, 7, and 9):

from numpy import random
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)

Run Instance