← Back to NumPy Course | Chapter 9: Random Module | Lesson 3 of 6

np.random.randint()

randint draws random whole numbers from a range you specify.

In this page:

  1. np.random.randint()
Syntax
python
np.random.randint(low, high, size)

np.random.randint()

np.random.randint(low, high, size) returns integers from low up to but not including high. If you give one argument, it means high and low defaults to 0. Use size to build arrays of any shape.

Note: The high value is exclusive, so randint(1, 7) simulates a die.

Example: np.random.randint()

python
import numpy as np

np.random.seed(3)
print(np.random.randint(1, 7, size=10))
print(np.random.randint(0, 100, size=(2, 3)))

# Output:
# [3 1 2 4 1 1 1 6 6 4]
# [[74 41 10]
#  [21 38 96]]
Related Topics
Common Mistakes
  1. Expecting high to be included
  2. Forgetting size returns a single number
  3. Using rand and casting to int
Chapter Summary
  • high is exclusive
  • size controls the output shape
  • One argument means high
  • Returns integers
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.