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.
Select one of the values from an array:
from numpy import random x = random.choice([3, 5, 7, 9]) 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.
Create a 2-D array containing the values from the array parameter (3, 5, 7, and 9):
from numpy import random x = random.choice([3, 5, 7, 9], size=(3, 5)) print(x) |