In this tutorial I’ll show you how to use the np.random.uniform function (AKA, Numpy random uniform).
I’ll explain what the function does, explain the syntax, and show you clear examples of how the function works.
If you need something specific, you can click on any of the following links and it will take you to the correct location in the tutorial.
Table of Contents:
Having said that, if you really want to understand how this function works, you should probably read the whole thing.
Ok. Let’s get to it.
An introduction to Numpy Random Uniform
So what does Numpy Random Uniform do?
The np.random.uniform function creates Numpy arrays filled with values that are drawn from a uniform probability distribution.
Does that make sense?
Unless you know a lot about Numpy and probability, it might not make sense
… so let me dive in and explain it a little.
Numpy Random Uniform Creates Numpy Arrays
The first thing you need to understand is that the Numpy random uniform function creates Numpy arrays.
Specifically, Numpy has functions for creating and “wrangling” a data structure called a Numpy array.
A Numpy array is just a data structure that contains numbers inside of a row-and-column structure. A simple Numpy array filled with integers looks something like this:
If you take a look, you can see that a Numpy array is just a row-and-column structure that contains numeric data.
The Numpy Package has many Functions for Creating Numpy Arrays
The Numpy package has a variety of functions for creating Numpy arrays with different types of properties.
For example, the Numpy random normal function creates Numpy arrays that are filled with normally distributed numbers.
The Numpy full function creates arrays where every cell in the array contains the exact same number.
Numpy zeroes creates arrays that are filled with zeros.
So Numpy has a variety of functions for creating Numpy arrays with different types of numbers.
Numpy Random Uniform Creates Arrays Drawn From a Uniform Distribution
And with that in mind, let’s return to numpy.random.uniform.
Like some of the other Numpy functions that I just mentioned – like np.random.normal and np.zeroes – the Numpy random uniform function creates Numpy arrays.
But it creates Numpy arrays with very specific types of numbers.
In particular, the np.random.uniform function creates Numpy arrays that contain numbers drawn from a uniform distribution.
What does that mean?
Let’s quickly review the uniform distribution to find out.
A Quick Review of the Uniform Distribution
The uniform distribution is a type of probability distribution.
Remember, a probability distribution is a function that gives the probability of different outcomes.
When we’re talking about probability distributions in Numpy, we’re talking about the probability of choosing a specific number.
Now, let’s tie that to the uniform distribution.
The Uniform Distribution Draws Numbers From a Range, with a Constant Probability
How does the uniform distribution work?
In the uniform distribution, there is a range that we can define by
Inside of that range, the probability of drawing a particular number is given by .
But the probability of drawing a number outside of that range is 0.
That’s why we call it the “uniform” distribution. The probability of a particular outcome inside the range is constant and uniform.
np.random.uniform creates arrays filled with values drawn from the uniform distribution
Let’s bring this back to the np.random.uniform function.
When we use Numpy random uniform, it creates a Numpy array that’s filled with numeric values. Those numeric values are drawn from within the specified range, specified by
high. The function will randomly select N values from that range, where N is given by the
So in the simple example above,
size is set to 3, so the function creates a Numpy array with 3 values. The values are randomly selected from the range from 0 to 1 (the
high values, respectively).
That’s really it at a high level.
But just to make sure that everything is clear, we’ll look carefully at the syntax. And later, I’ll show you some examples.
The syntax of np.random.unifirm
Let’s take a look at the syntax.
A quick note
One quick note: whenever we use a package like Numpy, we need to import that package.
Exactly how we import the package will impact the exact syntax that we use.
The common convention among data scientists is to import Numpy with the alias ‘
You can do that with the code:
import numpy as np
When we do this, we can call our Numpy functions with the prefix
Ok, back to the syntax.
When you call Numpy random uniform, you start by simply calling the function as
Then, inside the parenthesis, we have 3 major parameters that control how the function works:
Let’s take a look at those.
The parameters of numpy.random.uniform
Each parameter controls some aspect of how the function works.
size parameter controls the number of elements in the output array.
So if you set
size = 3, the output Numpy array will have 3 elements. If you set
size = 5, the output will have 5 elements.
You can also set the size with a tuple of values. In this case, the function will create a multi dimensional array. So if you set
size = (2,3), np.random.uniform will create a Numpy array with 2 rows and 3 columns.
Keep in mind that the
size parameter is optional. If you do not provide a value to the
size parameter, the function will output a single value between
low parameter sets the lower boundary of the possible output values.
Keep in mind that the range of possible outputs includes
low. So if you set
low = 0, the value 0 is a possible output value.
Also keep in mind that this parameter is optional. If you don’t explicitly provide an argument to this parameter, it will default to 0.
high parameter sets the upper boundary of possible output values.
Note that the value of the
high parameter is excluded. So if you set
high = 1, the exact value 1 will not be possible.
Also keep in mind that you need to provide an argument to the high parameter. Having said that, you do not need to explicitly use the parameter name itself. Said differently, you can pass an argument to this parameter by position.
The Output of np.random.normal
The output of Numpy random normal is a Numpy array of size
size, where the values are drawn from the uniform distribution with the low value
low and high value
So now that you know how the syntax works, let’s look at some examples.
Examples of how to Use Numpy Random Uniform
Ok. Here, I’ll show you several different examples of how to use the np.random.uniform function.
You can click on any of the links below, and it will take you to the specific example.
- Draw 3 values from the range
- Create a 2-dimensional array of uniformly distributed numbers
- Create an array of values from a specific range
Run this code first
One quick thing before you run the examples.
You’ll need to import Numpy.
You can do that by running the following code:
import numpy as np
By doing this, we’ll be able to call our Numpy functions with the prefix ‘
EXAMPLE 1: Draw 3 values from the range
First, we’ll create a Numpy array with 3 values drawn from the range
np.random.seed(55) np.random.uniform(size = 3, low = 0, high = 1)
array([0.09310829, 0.97165592, 0.48385998])
So what’s going on here?
Here, we’re setting the size parameter to
size = 3. This causes the function to create a Numpy array with 3 values.
We’re also setting
low = 0 and
high = 1. Those settings define the range of possible values.
The output is simply 3 numbers between 0 and 1.
(Note that we’re also using the Numpy random seed function here in order to set the seed of the random number generator. If you don’t understand that, you should read our blog post about Numpy random seed.)
EXAMPLE 2: Create a 2-dimensional array of uniformly distributed numbers
Next, let’s create a 2-dimensional array of uniformly distributed numbers.
This will work in a way that’s very similar to example 1.
The main difference is the argument to the
Here, we’ll set
size = (2,3).
np.random.seed(0) np.random.uniform(size = (2,3), low = 0, high = 1)
array([[0.5488135 , 0.71518937, 0.60276338], [0.54488318, 0.4236548 , 0.64589411]])
Look carefully at the output.
The output is a Numpy array with 2 rows and columns. That’s because we set the
size parameter to
size = (2,3). When we pass a tuple of values to the
size parameter, it’s telling Numpy to create a multi-dimensional array … in this case, an array with 2 rows and 3 columns.
Then, because we set
low = 0 and
high = 1, all of the values in the array are between 0 and 1.
(Note again: we’re also using the Numpy random seed function here in order to set the seed of the random number generator. If you don’t understand that, you should read our tutorial post about Numpy random seed.)
EXAMPLE 3: Create an array of values from a specific range
Finally, let’s create an array of values drawn uniformly from a specific range.
Here, we’ll create a Numpy array with 3 values.
But the values will be drawn from the range
We can set the low end and high end of the range with the
np.random.seed(0) np.random.uniform(size = 3, low = 50, high = 60)
array([55.48813504, 57.15189366, 56.02763376])
Here, we’ve set the
size parameter to
size = 3. Because of this, the output has 3 values.
We’ve also set
low = 50 and
high = 60. Because of this, all of the values are between 50 and 60. That is, all of the values were randomly selected from the range
[50,60) with equal probability.
Leave your other questions in the comments below
Do you have other questions about Numpy random uniform?
If so, just leave your questions in the comments section below.
Join our course to learn more about Numpy
In this blog post, I’ve shown you how to use np.random.uniform.
This function is good for some tasks when you’re working with numeric data. But if you really want to master data manipulation with numeric data, you need to know a lot more about Numpy.
If you’re serious about learning Numpy, 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 Numpy axes work
- How to reshape, split, and combine your Numpy arrays
- Applying mathematical operations on Numpy arrays
- and more …
Moreover, when you join the course, you’ll get access to our specialized practice system. This practice system will enable you to memorize all of the Numpy syntax that you learn. You’ll master it all within a few short weeks.
If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here: