Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Generate Random Array

In NumPy, we work with arrays, and you can use the two methods mentioned earlier to create random arrays.

Integers

The randint() method includes a size parameter that allows you to specify the shape of the array.

Example

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)

Example

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=(35))

print(x)

Floats

The rand() method also lets you define the shape of the array.

Example

Create a 1-D array with 5 random floats:

from numpy import random

x = random.rand(5)

print(x)

Example

Create a 2-D array with 3 rows, where each row contains 5 random numbers:

from numpy import random

x = random.rand(35)

print(x)