Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Generate Random Number From Array

The choice() method enables you to generate a random value from an array of values.

It takes an array as a parameter and randomly returns one of its values.

Example

Select one of the values from an array:

from numpy import random

x = random.choice([3579])

print(x)

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

You can add a size parameter to define the shape of the array.

Example

Create a 2-D array containing the values from the array parameter (3, 5, 7, and 9):

from numpy import random

x = random.choice([3579], size=(35))

print(x)