🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.randint()

AI & DATA SCIENCE // np-random-randint

np.random.randint() returns random integers from a low bound (inclusive) up to a high bound (exclusive).

Syntax

np.random.randint(low, high=None, size=None)

Deep Dive Course

Called with just one argument, randint(n) generates integers from 0 up to, but not including, n; called with two, randint(low, high), it generates integers from low up to, but not including, high, matching Python's range()-style half-open interval convention. The size parameter, an integer or a shape tuple, controls how many random integers to generate and their arrangement, defaulting to a single scalar value if omitted.

1Understanding np.random.randint()

Called with just one argument, randint(n) generates integers from 0 up to, but not including, n; called with two, randint(low, high), it generates integers from low up to, but not including, high, matching Python's range()-style half-open interval convention. The size parameter, an integer or a shape tuple, controls how many random integers to generate and their arrangement, defaulting to a single scalar value if omitted.

💡

Like Python's range(), randint()'s upper bound is exclusive — randint(1, 7) simulates a six-sided die, values 1 through 6, not randint(1, 6), which would only ever produce values 1 through 5.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.randint(1, 7, size=5))
localhost:3000

2Practical Example

Here is a real-world application of np.random.randint() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(0)
dice_rolls = np.random.randint(1, 7, size=(2, 3))
print(dice_rolls)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.randint():

1. Remember the high argument is exclusive, matching range()'s convention — add 1 if you need an inclusive upper bound, like simulating a die roll

2. Pass a size tuple directly to generate a whole array of random integers in one call, instead of looping and calling randint() repeatedly

3. Prefer the newer Generator API's integers() method over the legacy randint() in new code, since it offers more explicit control over interval endpoints

⚠️

Tip: Like Python's range(), randint()'s upper bound is exclusive — randint(1, 7) simulates a six-sided die, values 1 through 6, not randint(1, 6), which would only ever produce values 1 through 5.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.randint(1, 7, size=5))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
print(np.random.randint(1, 7, size=5))
Example 02Advanced Example
import numpy as np

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

Best Practices

  • Remember the high argument is exclusive, matching range()'s convention — add 1 if you need an inclusive upper bound, like simulating a die roll
  • Pass a size tuple directly to generate a whole array of random integers in one call, instead of looping and calling randint() repeatedly
  • Prefer the newer Generator API's integers() method over the legacy randint() in new code, since it offers more explicit control over interval endpoints

Interview Question

Why would np.random.randint(1, 6) fail to correctly simulate a standard six-sided die?

Hint: Think about which values that call could actually produce, given the exclusive upper bound.

randint()'s high argument is exclusive, matching Python's range()-style convention, so randint(1, 6) only ever produces the integers 1, 2, 3, 4, or 5 — it can never produce 6, since the upper bound itself is excluded from the possible results. To correctly simulate a six-sided die with values 1 through 6, you need randint(1, 7), passing one more than the largest value you actually want to be possible.

Exercises

MediumPractice using np.random.randint() in a real scenario.
View Solution
import numpy as np

np.random.seed(0)
print(np.random.randint(1, 7, size=5))

Frequently Asked Questions

Why would np.random.randint(1, 6) fail to correctly simulate a standard six-sided die?

randint()'s high argument is exclusive, matching Python's range()-style convention, so randint(1, 6) only ever produces the integers 1, 2, 3, 4, or 5 — it can never produce 6, since the upper bound itself is excluded from the possible results. To correctly simulate a six-sided die with values 1 through 6, you need randint(1, 7), passing one more than the largest value you actually want to be possible.

Related Functions

np-random-randomnp-random-choicenp-random-seed