October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Python randint(): Both Ends Included (and the NumPy Trap)

Python’s random.randint includes both endpoints. NumPy’s randint and Generator.integers exclude the upper endpoint by default, so use 7 to generate values through 6—or set endpoint=True.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

random.randint(a, b) in Python’s standard library includes both endpoints: a result can be a or b. NumPy’s similarly named functions differ: their upper bound is excluded by default. For a six-sided die, use random.randint(1, 6) in Python, but use np.random.randint(1, 7) or rng.integers(1, 7) with NumPy’s default settings.

Are both ends included in Python’s randint()?

Yes. Python’s standard-library random.randint(a, b) returns an integer N satisfying a <= N <= b. The Python 3.14.8 documentation describes it as an alias for randrange(a, b + 1) (Python documentation: random.randint).

For example, random.randint(1, 6) can return any integer from 1 through 6, including both 1 and 6. This differs from Python’s familiar range(start, stop) convention, where the stop value is excluded: randrange(start, stop, step) chooses from the corresponding range (Python documentation: random.randrange).

Why does NumPy randint() behave differently?

NumPy’s legacy np.random.randint(low, high) includes low but excludes high. Its possible results are in the half-open interval [low, high), so the largest result is high - 1 (NumPy documentation: numpy.random.randint).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That means np.random.randint(1, 6) returns values from 1 through 5—not 6. A one-argument call has another easily missed detail: np.random.randint(5) means values from 0 through 4, because the interval is [0, 5).

How to generate integers from 1 through 6

Use the call that matches the API’s endpoint convention:

API Endpoint behavior Call for values 1 through 6
random.randint(a, b) Includes both bounds random.randint(1, 6)
np.random.randint(low, high) Includes low; excludes high np.random.randint(1, 7)
rng.integers(low, high) Includes low; excludes high by default rng.integers(1, 7)
rng.integers(low, high, endpoint=True) Includes both bounds rng.integers(1, 6, endpoint=True)

For new NumPy code, create a generator with rng = np.random.default_rng() and call rng.integers(...). Its high argument is still exclusive by default; the endpoint=True option makes it inclusive (NumPy documentation: Generator.integers; NumPy beginner guide).

What to check when translating between APIs

  • Check which library the function belongs to. The shared name randint does not mean the bounds work the same way.
  • Check what the upper-bound argument represents. In Python’s standard library it is a possible result; in NumPy’s default calls it is the first value that cannot be returned.
  • When translating an inclusive range to NumPy’s default API, add one to the upper bound. For example, Python’s 1-through-6 range becomes NumPy’s (1, 7).
  • Use endpoint=True when you want NumPy’s upper argument to be included explicitly.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

NumPy integer dtype note

NumPy’s randint default integer dtype depends on platform sizing; the reference notes that the default corresponds to np.intp sizing since NumPy 2.0. If your code requires a particular fixed-width integer type, specify dtype rather than relying on the platform default (NumPy documentation: numpy.random.randint).

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.