How to generate Random Array in NumPy?

In this article, we will focus on the random module of NumPy, which allows us to generate random numbers and perform random operations in a variety of ways.

Generating NumPy Array of Random Integers

To generate random integers between a specified range, you can use the randint() function. Here’s an example of generating a single random integer between 0 and 50:

import numpy as np

# Generate a single random integer between 0 and 99
num = np.random.randint(0, 99)

print(num)

Output:

77

Reproducibility with Seed

When you need reproducible results, setting the seed is important. The np.random.seed function initializes the random number generator:

np.random.seed(42)
# Now the random numbers generated will be the same in any run.

For example, in the code below, we will try to fetch a random number four times. Each time, we will use the same seed value; therefore, we will get the same random number all four times.

import numpy as np

np.random.seed(42)
# Generate a single random integer between 0 and 99
num = np.random.randint(0, 99)
print(num)

np.random.seed(42)
# Generate a single random integer between 0 and 99
num = np.random.randint(0, 99)
print(num)


np.random.seed(42)
# Generate a single random integer between 0 and 99
num = np.random.randint(0, 99)
print(num)

np.random.seed(42)
# Generate a single random integer between 0 and 99
num = np.random.randint(0, 99)
print(num)

Output:

51
51
51
51

We got the same random number all four times because the seed value was the same for each random number. If you remove the seed() function call from the above code, you will get four different random numbers.

Creating NumPy Array of Random Integers

You can also generate a NumPy array of random integers by passing an extra argument size in the randint() function. The size argument denotes the shape of the array that you want to create, filled with random numbers. If you want to create a 1D NumPy array with some N random values, then set size as N. If you want to create a 2D NumPy array with 4 rows and 3 columns, then set size as (4,3). For example:

import numpy as np

np.random.seed(0)

# Create a NumPy Array of 10 random integers
arr = np.random.randint(0, 50, size=10)

print(arr)

Output:

[44 47  0  3  3 39  9 19 21 36]

Here, we created a NumPy Array of 10 random integers.

Creating NumPy Array with Uniform Distribution

Generating numbers that are uniformly distributed across a range is a common task in statistics, simulations, and various types of modeling. In NumPy, this can be efficiently done using the np.random.uniform function. This function generates samples from a uniform distribution over a specified interval.

A uniform distribution, in the simplest terms, refers to a probability distribution where every number within a certain range is equally likely to occur. If you imagine plotting the distribution, it would appear as a flat line, indicating that no value within the range is any more likely than another.

Here’s an example of how to use np.random.uniform to generate 10 random numbers between 0 and 1:

import numpy as np

# Generate 10 random numbers uniformly distributed between 0 and 1
arr = np.random.uniform(0, 1, size=10)

print(arr)

Output:

[0.37272705 0.09658273 0.98818703 0.7060652  0.52515426 0.32652724
 0.26448179 0.97768895 0.04380171 0.53666404]

The output will be different every time you run this code because it generates random numbers.

The function call np.random.uniform(0, 1, size=10) generates an array of 10 numbers. The parameters 0 and 1 specify the lower and upper limits of the range, and size=10 specifies the number of samples to generate.

Normal Distribution

For normally distributed numbers (Gaussian distribution), use the normal function:

import numpy as np

arr = np.random.normal(loc=0, scale=1, size=1000)

print(arr)

The loc parameter specifies the mean and the scale specifies the standard deviation.

Visualizing Distributions

To visualize these distributions, you might use matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# For uniform distribution
plt.hist(np.random.uniform(0, 1, size=10000), bins=50, alpha=0.7, label='Uniform')

# For normal distribution
plt.hist(np.random.normal(0, 1, size=10000), bins=50, alpha=0.7, label='Normal')

plt.legend()
plt.show()

IMAGE

Shuffling NumPy Arrays

The shuffle() function can be used to randomly shuffle elements in an array:

import numpy as np

# Create a numpy array of 10 
# integers from 1 to 9
arr = np.arange(10)

# Shuffle the elements in NumPy Array
np.random.shuffle(arr)

print(arr)

Output:

[9 6 5 2 7 4 8 3 1 0]

Summary

In this tutorial, we’ve covered how to generate random integers, use seeds for reproducibility, generate uniformly and normally distributed numbers, visualize these distributions, and shuffle arrays using NumPy’s random module.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top