Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:03:33+00:00 2026-05-14T03:03:33+00:00

It has always seemed strange to me that random.randint(a, b) would return an integer

  • 0

It has always seemed strange to me that random.randint(a, b) would return an integer in the range [a, b], instead of [a, b-1] like range(...).

Is there any reason for this apparent inconsistency?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:03:33+00:00Added an answer on May 14, 2026 at 3:03 am

    I tried to get to the bottom of this by examining some old sources. I suspected that randint was implemented before Python’s long integer: meaning that if you wanted a random number that included INT_MAX, you would have needed to call random.randrange(0, INT_MAX + 1) which would have overflowed and resulted in arguments of (0, 0) or (0, INT_MIN) depending.

    However, looking as far back as even the Python 1.5.2 sources, in Lib/whrandom.py we see:

    #
    # Get a random integer in the range [a, b] including both end points.
    # (Deprecated; use randrange below.)
    #
    def randint(self, a, b):
        return self.randrange(a, b+1)
    

    whrandom.randint was continued to be deprecated in 2.0, 2.1, 2.2, and 2.3; but random.randint was marked as deprecated in 2.1, although no longer marked as deprecated in 2.2.

    Also, random.py from version 2.1 is the first to note in random.randint‘s docstring:

    def randrange(self, start, stop=None, step=1, int=int, default=None):
        """Choose a random item from range(start, stop[, step]).
    
        This fixes the problem with randint() which includes the
        endpoint; in Python this is usually not what you want.
        Do not supply the 'int' and 'default' arguments.
        """
    

    The only available source older than that is the 0.9.1 source, and as far as I can tell, randint was not implemented at that point.

    Thus, I conclude that the reasoning for randint including the endpoint is known to only Guido himself at this point; given the docstring from Python 2.1, it sounds like the reason may have been a simple mistake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has always confused me. It seems like this would be nicer: ["Hello", "world"].join("-")
It has always struck me as strange that the C function fopen() takes a
It has always bothered me that the only way to copy a file in
There has always been a confusion with preg_match in php. I have a string
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript
Getting data from a database table to an object in code has always seemed
This is something that has always bothered me. Wouldnt it make more sense to
Im new to NHibernate, the configuration aspect of it has always seemed overly onerous
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
This has always bugged me to what is the best way to do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.