In NumPy, we work with arrays, and you can use the two methods mentioned earlier to create random arrays.
The randint() method includes a size parameter that allows you to specify the shape of the array.
Create a 1-D array with 5 random integers ranging from 0 to 100:
from numpy import random x=random.randint(100, size=(5)) print(x) |
Create a 2-D array with 3 rows, where each row contains 5 random integers ranging from 0 to 100:
from numpy import random x = random.randint(100, size=(3, 5)) print(x) |
The rand()
method also lets you define the shape of the array.
Create a 1-D array with 5 random floats:
from numpy import random x = random.rand(5) print(x) |
Create a 2-D array with 3 rows, where each row contains 5 random numbers:
from numpy import random x = random.rand(3, 5) print(x) |