Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to get to the bottom of this by examining some old sources. I <em>suspected</em> that <code>randint</code> was implemented before Python's long integer: meaning that if you wanted a random number that included <code>INT_MAX</code>, you would have needed to call <code>random.randrange(0, INT_MAX + 1)</code> which would have overflowed and resulted in arguments of <code>(0, 0)</code> or <code>(0, INT_MIN)</code> depending.</p> <p>However, looking as far back as even <a href="http://www.python.org/download/releases/1.5/" rel="noreferrer">the Python 1.5.2 sources</a>, in <code>Lib/whrandom.py</code> we see:</p> <pre><code># # 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) </code></pre> <p><code>whrandom.randint</code> was continued to be deprecated in <a href="http://svn.python.org/view/python/branches/release20-maint/Lib/whrandom.py?view=markup" rel="noreferrer">2.0</a>, <a href="http://svn.python.org/view/python/branches/release21-maint/Lib/whrandom.py?view=markup" rel="noreferrer">2.1</a>, <a href="http://svn.python.org/view/python/branches/release22-maint/Lib/whrandom.py?view=markup" rel="noreferrer">2.2</a>, and <a href="http://svn.python.org/view/python/branches/release23-maint/Lib/whrandom.py?view=markup" rel="noreferrer">2.3</a>; but <code>random.randint</code> was marked as deprecated in <a href="http://svn.python.org/view/python/branches/release21-maint/Lib/random.py?view=markup" rel="noreferrer">2.1</a>, although no longer marked as deprecated in <a href="http://svn.python.org/view/python/branches/release22-maint/Lib/random.py?view=markup" rel="noreferrer">2.2</a>.</p> <p>Also, <a href="http://svn.python.org/view/python/branches/release21-maint/Lib/random.py?view=markup" rel="noreferrer"><code>random.py</code> from version 2.1</a> is the first to note in <code>random.randint</code>'s docstring:</p> <pre><code>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. """ </code></pre> <p>The only available source older than that is <a href="http://www.python.org/download/releases/early/" rel="noreferrer">the 0.9.1 source</a>, and as far as I can tell, <code>randint</code> was not implemented at that point.</p> <p>Thus, I conclude that the reasoning for <code>randint</code> <em>including</em> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload