Menu

Numpy.random.randint() in python

Written by Selva Prabhakaran | 3 min read

numpy.random.randint function is used to get random integers from low to high values. The low value is included while the high value is excluded in the calculations. The output values are taken from the discrete uniform distribution of the range values.

random.randint(low, high=None, size=None, dtype=int)

  • Purpose: The numpy random randint function used for creating a numpy array with random integers from low to high interval.

  • Parameteres:

    • low: int or array-like of ints: Lowest integers are drawn from the random values.
    • high: int or array-like of ints, optional: Larger or highest integers are drawn from the random values.
    • size: int or tuple of ints, optional: The shape of the array you want to create.
    • dtype: dtype, optional The type of the output you want.
  • Returns: out:int or ndarray of ints Returns an array with specified shape along with random integer values in it.

python
# Import Packages
import numpy as np
import warnings

warnings.filterwarnings("ignore")

1.Construct 1-D array using randint() function

In this example you will understand how to construct a 1-D array using np.random.randint() function

Example 1: Create a 1-D array using randint()

python
# create a 1-D array
a = np.random.randint(low = 1, size = 5)
python
# print the newly created array
print(a)
python
[0 0 0 0 0]
  • when low = 1 then randint() takes integer as 0.
  • The size = 5 which creates a 1-D array
  • Now a 1-D array is created with integer as 0.

Example 2: Create a 1-D array using randint()

python
# create a 1-D array
a = np.random.randint(low = 1, high = 6, size = 8)
python
# print the newly created array
print(a)
python
[2 1 1 1 3 3 3 1]
  • when low = 1 and high = 6 randint() takes integers between 1 to 5.
  • The size = 8 which creates a 1-D array
  • Now a 1-D array is created with integers between 1-5.

2.Construct 2-D array using randint() function

In this example you will understand how to construct a 2-D array using np.random.randint() function

Example 1: Create a 2-D array using randint()

python
# create a 2-D array
a = np.random.randint(low = 2, size = (2,3))
python
# print newly created array
print(a)
python
[[0 1 0]
 [0 0 0]]
  • when low = 2 then the randint() takes integers between 0 to 1.
  • As you set the size as (2,3), which creates a 2-D array with 2 rows and 3 columns
  • Now the 2-D array is created with integers between 0-1

Example 2: Create a 2-D array using randint()

python
# create a 2-D array
a = np.random.randint(low = 0, high = 6, size = (3,4))
python
# print newly created array
print(a)
python
[[3 2 3 5]
 [1 4 5 2]
 [3 5 1 2]]
  • The low = 0 and high = 6 then the randint() takes integers between 0 to 5.
  • As you set the size as (3,4), which creates a 2-D array with 3 rows and 4 columns.
  • Now the 2-D array is created with integers between 0-5

3.Construct 3-D array using randint() function

In this example you will understand how to construct a 3-D array using np.random.randint() function

Example 1: Create a 3-D array using randint()

python
# create a 3-D array
a = np.random.randint(low = 0, high = 5, size = (2,3,4))
python
# print newly created array
print(a)
python
[[[0 0 2 4]
  [3 2 3 4]
  [1 3 3 1]]

 [[3 3 0 3]
  [0 0 1 1]
  [2 4 2 3]]]

Note When creating a 3-D array you have to specify both high and low parameters. If you specify only low parameter it returns a error.

Try the above scenario to know what kind of error it returns.

  • When is set to low = 0 and high = 5 the randint() takes integers between 0 to 4.
  • As you set the size as (2,3,4) which creates a 3-D array. with two sub arrays, three rows and four columns for each sub array.
  • Now the 3-D array is created with integers between 0-4

4.Test your knowledge

Q1: Can you create a 3-D arrays without defining high parameter?
Ans: No, you cannot create a 3-D array without defining a high parameter. That returns a value error.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master Python — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
🎓
Machine Learning Plus
AI & Data Science
Not Sure Which Course
Is Right for You?
Get a free callback from our learning advisor
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science