This tutorial will explain how to use the np.random.randint function from Numpy (AKA, Numpy random randint). Ultimately, you’ll learn how to use Numpy to generate random integers drawn from a uniform distribution.
The tutorial will explain the syntax of np.random.randint. It will also show you clear, step-by-step examples of how to use np.random.randint.
The tutorial is fairly comprehensive. That said, if you’re looking for something specific, you can click on one of the links in the following Table of Contents to go directly to a specific location in the tutorial.
Table of Contents:
- A quick review of Numpy
- The syntax of np.random.randint
- np.random.randint examples
- np.random.randint FAQ
Having said that, if you’re new to Numpy, it’s probably best if you read the whole tutorial. You’ll probably learn a lot more that way.
Ok. Let’s just start out with a quick review of Numpy.
A quick review of Numpy
Numpy is an add-on package for the Python programming language.
Specifically, Numpy is a package that enables you to create and work with arrays of numeric data.
Numpy enables you to work with numeric data
So in essence, Numpy is a toolkit for working with Numeric data.
We often use it for performing numeric computations.
For example, you can use Numpy to perform statistical computations. So can use it to compute things like the median, mean, maxiumum, or standard deviation of a group of numbers.
You can also use it to compute more general mathematical functions like the natural logarithm, or the exponential.
But before we perform any of those operations, we often need to create a Numpy array first.
Numpy provides functions for creating numpy arrays
So in addition to providing functions for performing computations on numeric data, Numpy also has a set of functions for creating what we call Numpy arrays.
A quick review of Numpy arrays
Put simply, a Numpy array is a Python object that holds numeric data in a row-and-column format.
Here’s an example of a simple Numpy array:
Numpy arrays can come in a variety of shapes and sizes. There are 2-dimentional Numpy arrays (like the example above), but also 1-dimentional arrays, 3-dimensional arrays, and n-dimentional arrays.
What’s important to understand is that Numpy arrays hold numeric data. (Note that the numbers in a Numpy array must all have the same data type.)
Numpy provides functions for creating Numpy arrays
As I mentioned earlier, Numpy provides a variety of functions for creating these Numpy arrays.
The simplest of these is the Numpy array function, which creates Numpy arrays from Python lists and list-like objects.
But there’s also a large group of other functions for creating arrays with various properties.
For example, you can:
- Create an “empty” numpy array
- Make an array with all zeros
- Make an array with all ones
- Create an array that’s filled with a particular value
- Create an array with a range of values
- … etc
Numpy can create arrays with specific properties
Numpy can also create arrays with specific properties.
For example, it’s possible to create arrays with normally distributed values.
And it’s possible to create arrays with “random” or semi-random values.
And that’s what Numpy random randint essentially does.
Numpy random randint creates arrays with random integers
Put very simply, the Numpy random randint function creates Numpy arrays with random integers.
So as opposed to some of the other tools for creating Numpy arrays mentioned above, np.random.randint creates an array that contains random numbers … specifically, integers.
It’s fairly simple, but there are some important details.
We can control the size of the array (the total number of items), and we can control the range from which the random integers are drawn.
Exactly how the function works depends on how you use the syntax.
That being the case, let’s take a closer look at the syntax of the function.
The syntax of np.random.randint
Here, we’ll take a close look at the syntax of np.random.randint.
Briefly though, I need to remind you of one thing that’s important to how the syntax works.
A quick note on importing Numpy
How to call Numpy functions depends on how you import Numpy.
If you import Numpy with the code import numpy
, then you can call Numpy functions with the prefix ‘numpy.
‘ before the name of the function. For example, if you import Numpy with the code import numpy
, then you would call Numpy random randint as numpy.random.randint()
.
Having said that, Numpy users very commonly use a different import syntax.
Most of the time, Numpy users import Numpy with the code import numpy as np
. This enables you to use the alias ‘np
‘ when you call your Numpy functions. So if you do it this way, you would call Numpy random randint as np.random.randint()
.
Again: how you import Numpy changes how you call the function!
In the rest of this tutorial, we’ll be using the prefix np
.
So, you’ll need to import Numpy with the following code:
import numpy as np
Once you do that, you’ll be ready to learn about the syntax, and you’ll be ready for the examples later on.
np.random.randint syntax
The syntax of np.random.randint is fairly simple.
Assuming that you’ve imported Numpy with the code in the previous section, you will call the function as np.random.randint()
.
However, there are a set of parameters that you use inside of the parenthesis that control exactly how the function will work.
That being said, some of these parameters can operate in ways that are a little counter-intuitive, so they require a little explanation.
The parameters of np.random.randint
As shown in the syntax explanation image above, there are four parameters that you need to know about:
low
high
size
dtype
Let’s look at each of them individually.
low
(required)
The low
parameter enables you to specify the lowest possible integer for your array of random integers.
Having said that, that’s not the full story.
If you use both the high
and the low
parameter in your syntax, the output array will contain random integers within the range [low, high)
. That means that np.random.randint will draw integers from the range starting at low
, up to but excluding high
.
However, if you do not use the high
parameter, then np.random.randint will actually behave differently. If you use the low
parameter but not the high
parameter, np.random.randint will draw integers from the range [0, low)
.
I know, this is a little confusing. You just have to remember that the low
parameter is a little quirky.
high
The high
parameter enables you to specify the highest possible integer in the output.
This parameter is optional.
If you do not use the high
parameter, then np.random.randint will draw random integers from the range [0,low)
.
size
The size
parameter enables you to specify the shape of the output array.
This is a little bit confusing.
Since the parameter is called “size
“, you’d think that this strictly controls the size of the array. But it actually controls the “shape”. (Remember: size and shape are both distinct properties of a Numpy array.)
This parameter is optional. If you don’t provide an argument to the size
parameter, then np.random.randint will return a single integer.
If you provide a single value n
as the argument, it will return n
integers from the appropriate range.
But you can also provide a tuple or list of values in the form (m, n, k)
. If you do this, then np.random.randint will return a Numpy array with m
rows, n
columns, etc. And the total size of the array (the total number of items) will be m * n * k
.
dtype
The dtype
parameter enables you to specify the exact datatype of the output.
The default datatype is ‘np.int
‘.
Having said that, you can change this to other options, including ‘int64
‘ and ‘int
.’
Examples of how to use np.random.randint
Now that we’ve looked at the syntax, we’ll work through a few examples.
Examples:
- Draw a single random integer using randint
- Select an integer from a specific range
- Create an array of random integers with a specific size
- Create a 2D array of random integers
Run this code first
Before we run any of the examples, we need to properly import Numpy.
So, please run this code:
import numpy as np
This will enable us to use the alias ‘np
‘ when we call our Numpy functions.
EXAMPLE 1: Draw a single random integer using randint
Ok.
First, let’s just draw a single integer using Numpy random randint.
To do this, we’ll call np.random.randint with an integer inside the parenthesis.
np.random.seed(22) np.random.randint(10)
OUT:
5
Explanation
So what happened here?
We called np.random.randint with the number ’10’ inside the parenthesis.
That number 10 is the high end of the integer range (but keep in mind, the range will exclude 10).
Effectively, this code is causing np.random.randint to select a random integer between 0 and 10 (but excluding 10!). That’s how it works when you only provide one argument … it treats the argument as the maximum integer for the range of possible output values.
Note that we’re using the np.random.seed function to set the random seed for Numpy. This will give us the same “random” integer every time we use the same seed value, which makes the code repeatable.
One quick note: we could alternatively write the syntax for this example as:
np.random.seed(22) np.random.randint(low = 0, high = 10)
They will produce the same result.
This syntax actually brings us to our next example.
EXAMPLE 2: Select an integer from a specific range
For this example, we’re going to select an integer from a specific range.
To do this, we’re going to use both the low
and high
parameters.
Let’s take a look.
Here, we’re going to select a single integer from the range 10 and 99.
np.random.seed(22) np.random.randint(low = 10, high = 100)
OUT:
14
Explanation
Here, we’re using np.random.randint to select a number between 10 and 99.
To do this, we’re setting low = 10
and high = 100
. Remember: when we use the high
parameter, the maximum of the range will be high - 1
. The range is always up to but excluding high
.
Again note that we’re using the np.random.seed function to set the random seed for Numpy. This will give us the same “random” integer every time we use the same seed value, which makes the code repeatable.
EXAMPLE 3: Create a 1D array of random integers with a specific size
Now, let’s create a 1-dimentional array of a specific size.
Here, we’re going to create a Numpy array with 6 integers between 0 and 9.
np.random.seed(22) np.random.randint(low = 0, high = 10, size = 6)
OUT:
array([5, 4, 0, 4, 6, 6])
Explanation
Here, we’re setting low = 0
and high = 10
.
This means that Numpy will randomly select integers from the range 0 to 9.
Setting size = 6
causes Numpy to create an array with 6 random integers from that range.
The result in this case is array([5, 4, 0, 4, 6, 6])
.
Note that we’ve set np.random.seed(22)
. This will make the code reproduceable and will create the exact same output every time, as long as you use the same random seed.
EXAMPLE 4: Create a 2D array of random integers
Finally, let’s create a 2-dimensional array.
Here, we’re going to use code that’s almost the same as example 3.
The only difference is that we’re going to change the argument to the size
parameter.
We’re going to set size = (3,4)
.
Let’s take a look:
np.random.seed(22) np.random.randint(low = 0, high = 10, size = (3,4))
OUT:
array([[5, 4, 0, 4], [6, 6, 4, 8], [4, 2, 8, 7]])
Explanation
Here, we’ve set size = (3,4)
. That effectively creates a 2-dimentional Numpy array with 3 rows and 4 columns.
Remember, we can set the size
like this to create multi-dimentional arrays. To do this, we simply pass a Python list or a list-like object as the argument to size
.
Frequently asked questions about np.random.randint
Now that you’ve learned about np.random.randint and seen some examples, let’s review some frequently asked questions about the function.
Frequently asked questions:
Question 1: How do you create an array of binary numbers?
To create an array of binary numbers, you can use np.random.randint with low = 0
and high = 2
.
np.random.seed(22) np.random.randint(low = 0, high = 2, size = 10)
OUT:
array([1, 0, 0, 0, 0, 0, 0, 1, 0, 0])
Naturally, you can change the size and shape by using the size
parameter.
Question 2: Why do we need to use numpy.random.seed?
You may have noticed that in the above examples, I used numpy.random.seed before using numpy.random.randint.
Why?
When we use Numpy to generate “random” numbers, the numbers are not completely random. Technically, they are “pseudo random,” which means that they appear to be random, but are actually generated by a deterministic process.
When we generate pseudo random numbers on a computer, we need to initialize that process with what we call a “seed.”
The seed acts as the starting point of the pseudo random generation process, and the algorithm generates numbers from there.
When we use the same seed, the pseudo random number generator will generate the same output. (Remember: the numbers look random, but are 100% determined by a repeatable process.)
By using the same seed, we’ll always get the same pseudo random numbers.
This ultimately makes our code repeatable, which is good for teaching purposes.
Still a little confused? I recommend that you read our tutorial about Numpy random seed. It explains everything in much greater detail.
Leave your other questions in the comments below
Do you have other questions about Numpy random randint?
Leave your questions in the comments section below.
Join our course to learn more about Numpy
The examples you’ve seen in this tutorial should be enough to get you started with Numpy random randint, but if you’re serious about learning Numpy, you should enroll in our premium course called Numpy Mastery.
There’s a lot more to learn about Numpy, and Numpy Mastery will teach you everything, 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
- and more …
Moreover, it will help you completely master the syntax within a few weeks. You’ll discover how to become “fluent” in writing Numpy code.
Find out more here:
I love your methods of explanation.
You’re really doing a lot.
Thanks Emmanuel … much appreciated.
Good job with your explanations on the random.randint function.
How do l create a dataframe with 7 columns and 20 rows, but with each column having a differently low, and high random integer range?
Try this:
Note: you’ll need to reset the values in
low_high_values
to the values you want.Alternatively, you can do this with numpy.hstack instead of transpose.
To do this, you need to use np.random.randint with size = (number_of_elements,1).
What happens when we won’t provide low and high values but provide size which range does it takes
An argument to the
low
parameter is required. Thehigh
parameter is optional.How the function behaves is explained right in he blog post:
“If you use both the
high
and thelow
parameter in your syntax, the output array will contain random integers within the range[low, high)
. That means that np.random.randint will draw integers from the range starting atlow
, up to but excludinghigh
.However, if you do not use the
high
parameter, then np.random.randint will actually behave differently. If you use thelow
parameter but not thehigh
parameter, np.random.randint will draw integers from the range[0, low)
.I know, this is a little confusing. You just have to remember that the
low
parameter is a little quirky.The
high
parameter enables you to specify the highest possible integer in the output.This parameter is optional.
If you do not use the
high
parameter, then np.random.randint will draw random integers from the range[0,low)
.”