np.random.rand Explained

In this tutorial, I’ll show you how to use the np.random.rand function (AKA, Numpy random rand) to create Numpy arrays filled with random uniform numbers.

I’ll explain exactly what this function does, how the syntax works, and I’ll show you step-by-step examples of how to use it.

If you need something specific, you can click on any of the following links to navigate to the correct section of the tutorial.

Table of Contents:

Ok. Let’s get to it.

A quick introduction to Numpy Random Rand

At a high level, the Numpy random rand function is pretty straight forward.

np.random.rand() generates random numbers from the standard uniform distribution (i.e., the uniform distribution from 0 to 1), and outputs those numbers as a Numpy array.

To help you understand that, let’s quickly review the relevant details about Numpy arrays, and about the uniform distribution.

Numpy Random Uniform Creates Numpy Arrays

First of all, let’s review Numpy and Numpy arrays.

The np.random.rand() produces random numbers, structured as a Numpy array.

A Numpy array is a Python data structure that we use for storing and manipulating numeric data.

Numpy arrays have a row-and-column structure, and they can come in a variety of shapes and sizes. They can be 1-dimensional, 2-dimensional, or multi dimensional. Here’s a quick example of a 2D Numpy array:

An example of a 2d Numpy array

Additionally, the Numbers inside of a Numpy array can have a variety of different properties.

We Use Numpy Functions to Create and Manipulate Numpy Arrays

The Numpy package has a variety of functions for working with Numpy arrays. For example, there are tools for summing the numbers in a Numpy array, calculating the mean of the numbers, calculating the standard deviation, and so on.

But there are also Numpy functions for creating arrays with specific properties.

For example, there are functions for creating arrays that contain only 1s, creating arrays that contain all zeros, and creating arrays that contain numbers from specific probability distributions. For example, the Numpy random normal function creates arrays with normally distributed numbers.

The np.random.rand() function is one of these types of functions.

np.random.rand creates arrays with numbers drawn from the standard uniform distribution

The Numpy random rand function creates Numpy arrays that are filled with Numbers from the standard uniform distribution.

If you’ve taken a class on probability, you might be familiar with the uniform distribution, but let’s quickly review.

The Uniform Distribution has a Constant Probability Within a Specific Range

The uniform distribution has a constant probability density function between a specific range.

Generally, the probability density function is f(x) = \frac{1}{high - low} between a specific range, low and high, and 0 everywhere else.

Essentially, when we use a uniform probability distribution, there’s a constant probability of selecting a number within a specific range, and 0 probability outside that range. Keep in mind that the Numpy random uniform function generates numbers from the general uniform distribution.

The standard uniform distribution is a special case of the uniform distribution

The standard uniform distribution is a special case of the uniform distribution. In the standard uniform distribution, the boundaries of the range are set to low = 0 and high = 1. So, Numpy random rand is like np.random.uniform with low = 0 and high = 1.

An image showing that for the standard uniform distribution, the probability density function is constant between 0 and 1, and 0 outside that range.

To restate and simplify: the standard uniform distribution selects numbers between 0 and 1. The numbers between 0 and 1 have a uniform probability of being selected. Outside of 0 and 1, the probability of selecting a number is 0.

So that’s essentially what np.random.rand does. The np.random.rand function creates Numpy arrays that contain values between 0 and 1. And the probability of selecting specific numbers between 0 and 1 is uniform.

A simple example that shows how to create a Numpy array with 3 numbers drawn from the standard uniform distribution.

But, there are a few details about how the function works. To understand those details, we need to look at the syntax.

The syntax of np.random.rand

Here, I’ll walk you through the syntax of np.random.rand.

A quick note

One quick note about the syntax.

Everything that I’m about to explain about the syntax assumes that you’ve imported Numpy.

In particular, I’m going to assume that you’ve imported Numpy with the alias ‘np‘.

You can do that with the code import numpy as np.

This is the common convention among Python data scientists. And it’s important, because how we import Numpy will slightly affect how we write the syntax.

np.random.rand syntax

Ok, assuming that we’ve imported Numpy with the alias np, we call the function as np.random.rand().

An image that explains the syntax of np.random.rand.

Inside of the parenthesis, there are a set of parameters that enable you to modify the size of the output. Let’s take a closer look at those.

The parameters of np.random.rand

The np.random.rand() function has a set of parameters that enable you to control the exact shape of the output.

Keep in mind, that these parameters are optional. In fact, it’s possible to call the function without any parameters at all. In this case, np.random.rand() will return a single random number between 0 and 1.

But if you use the parameters, you can specify the number of rows, columns, and the number of elements along additional dimensions.

Each of the following parameters controls an axis.

  • d0
  • d1
  • dn

Let’s take a look at these so you understand exactly what they do.

d0 (optional)

If you chose to use the d0 parameter, then the argument to this parameter controls the number of elements along axis-0.

So if you’re creating a 1 dimensional Numpy array, d0 controls the total number of elements in the array.

An image that shows how the d0 parameter works in Numpy random rand.

If you’re creating a 2 dimensional or n-dimensional array, then d0 controls the number of rows.

Remember: for 1D arrays, axis-0 is the only axis. But for 2D arrays, axis-0 points downwards against the rows. If you’re confused about this, you should review how Numpy axes work.

d1 (optional)

The second parameter, d1, controls the number of elements along axis-1.

Remember: in a 2-dimensional array, axis-1 points horizontally along the columns.

So if you create a 2D or multi-dimensional array, d1 controls the number of columns (and d0 will control the number of rows).

An image that shows how the d0 parameter controls the rows and d1 controls the columns.

Bear in mind that you’ll only use d1 if you’re creating a 2D or multi dimensional array. And if you use d1, you’ll also have to use d0.

dn (optional)

The previous two parameters (d0 and d1) generalize for higher dimensional arrays.

If you want to create an n-dimensional Numpy array, dn controls the number of elements along axis-n.

The Output of np.random.rand

The numpy.random.rand() function outputs a Numpy array that contains numbers drawn from the standard uniform distribution. That is, the array will contain numbers from the range [0, 1).

Having said that, the exact shape of the output depends on the parameters you use.

If you call np.random.rand() without any parameters, then it will output a single number.

If you call the function as np.random.rand(d0), and use only the d0 parameter, it will output a 1-dimensional array with the number of elements specified by the argument to d0.

And if you use the higher order parameters d0, d1, … dn, then np.random.rand will output a Numpy array of size (d0, d1, ..., dn).

So essentially, the parameters d0, d1, and dn control the size and shape of the output array.

Examples: How to Use np.random.rand to Generate Random Uniform Numbers

Now that we’ve looked at the syntax, let’s look at some examples.

It will probably be easier to understand how this works if you see some concrete examples.

Examples:

Run this code first

One quick note.

As I mentioned above, before we use Numpy, we need to import the Numpy package. You can do that with the following code:

import numpy as np

Once you do that, you’ll be ready to run the example code.

EXAMPLE 1: Generate a single number with np.random.rand

Ok. First, we’ll generate a single number with Numpy random rand.

Let’s take a look.

np.random.seed(0)
np.random.rand()

OUT:

0.5488135039273248
Explanation

This is really simple.

When we call np.random.rand() without any parameters, it outputs a single number, drawn randomly from the standard uniform distribution (i.e., the uniform distribution between 0 and 1).

Here, we also used Numpy random seed to make our code reproducible. That being the case, if you run this code, you should get the same output.

EXAMPLE 2: Create a 1D Numpy array with Numpy Random Rand

Next, we’re going to create a 1-dimensional Numpy array, filled with random uniform numbers.

np.random.seed(0)
np.random.rand(3)

OUT:

array([0.5488135 , 0.71518937, 0.60276338])
Explanation

Here, we’ve called Numpy random rand and we provided a single number, 3, as an argument.

This argument value is being passed to the d0 parameter.

Effectively, when we call np.random.rand() with a single numeric argument, the function creates a 1-dimensional array, with the number of elements specified by the input value.

So the input value is 3, and the output array has 3 elements.

(Also note that we’ve used Numpy random seed to make the output reproducible.)

EXAMPLE 3: Create a 2D Numpy array with Numpy Random Rand

Finally, let’s create a 2-dimensional array with Numpy random rand.

To do this, we’re going to call the function with two integer arguments:

np.random.seed(0)
np.random.rand(2, 3)

OUT:

array([[0.5488135 , 0.71518937, 0.60276338],
       [0.54488318, 0.4236548 , 0.64589411]])
Explanation

Here, we’ve called np.random.rand with 2 arguments … the values 2 and 3.

The value 2 is being passed to the d0 argument, and the value 3 is being passed to the d1 argument.

Remember, when we create a 2D array, d0 controls the number of rows and d1 controls the number of columns.

An image that shows how the d0 parameter controls the rows and d1 controls the columns.

So here, when we call the function as np.random.rand(2, 3), Numpy random rand produces a Numpy array with 2 rows and 3 columns. The values are drawn randomly from the standard uniform distribution.

Note also that once again, we’ve used Numpy random seed to make our code reproducible.

Frequently Asked Questions about Numpy Random Rand

Now that you’ve learned about the syntax of Numpy Random Rand, and seen some examples, let’s look at a common question about the function.

Frequently asked questions:

What’s the difference between np.random.rand and np.random.uniform?

numpy.random.rand() and numpy.random.uniform() are very similar functions.

Both functions generate random numbers drawn from the uniform distribution.

Both functions output Numpy arrays.

The difference is that np.random.rand() is like a special case of np.random.uniform().

Let me explain.

numpy.random.uniform generates random numbers from the uniform distribution, but it allows you to specify the low end of the range and the high end of the range for the uniform distribution.

numpy.random.rand also generates random numbers from the uniform distribution, but the low end of the range is set 0 and the high end of the range is set to 1.

So the np.random.rand function is like a special case of np.random.uniform.

To see this, try running the following code:

np.random.seed(0)
np.random.rand(3)
np.random.seed(0)
np.random.uniform(size = 3, low = 0, high = 1)

You’ll notice that these produce the same output.

Beyond that, Numpy random uniform has a slightly different syntax. To learn more about this function, check out our tutorial about Numpy random uniform.

Leave your other questions in the comments below

Do you have other questions about the Numpy random rand function?

If so, just leave your question in the comments section at the bottom of the page.

Join our course to learn more about Numpy

Here in this tutorial, I’ve explained how to use the np.random.rand function.

This should help you understand this particular function, but if you really want to learn Numpy, there’s a lot more to learn.

If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery.

Numpy Mastery will teach you everything you need to know about Numpy, including:

  • How to create Numpy arrays
  • How to use the Numpy random functions
  • What the “Numpy random seed” function does
  • How to reshape, split, and combine your Numpy arrays
  • How to perform mathematical operations on Numpy arrays
  • and more …

Moreover, it will help you completely master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.

Find out more here:

Learn More About Numpy Mastery

Leave a Comment