[docs] Mistake in random.py documentation?

Petr Viktorin encukou at gmail.com
Sat Jan 23 15:33:28 EST 2016


On 01/22/2016 04:27 PM, Tyler Arbour wrote:
> Hi,
> 
> I’m reporting what I /think/ might be a mistake for the documentation of
> random.py. Sorry that I am short on time and have never reported a bug
> (new user), so apologies if the Tracker is the preferred way.

Indeed, the tracker is the preferred way; e-mails tend to get forgotten
if no one gets to them soon enough.

> Reading the stuff below, it is unclear whether *randrange(0,
> 5)* includes 5 in the set it’s choosing integers from.

It does link to `range`, where the docs are a bit more specific – though
maybe too technical. The best place to learn about range is the
tutorial, which does say "The given end point is never part of the
generated sequence":
https://docs.python.org/3/tutorial/controlflow.html#the-range-function

> Furthermore, *randint(0, 5)* explicitly says “alias for randrange(a,
> b+1) which to me suggests *randint* /includes/ the “stop” integer and
> *randrange* does not.
> 
> I tested both (*/see screenshot/*) and see that neither consider 5 in
> the set. This seems wrong to me. I’m curious to know though, so I’d love
> a response if at all possible.

You forgot to change "randrange" to "randint" in your second test. With
the change, it should work as expected. (I'm using lists as I don't have
Numpy handy right now):

>>> import random
>>> [random.randrange(0, 5) for i in range(100)]
[0, 0, 0, 4, 2, 2, 2, 4, 0, 4, 1, 4, 1, 0, 3, 1, 0, 4, 0, 1, 2, 2, 3, 0,
1, 4, 2, 0, 0, 0, 2, 0, 2, 4, 0, 0, 2, 0, 2, 1, 3, 4, 0, 4, 1, 1, 2, 3,
3, 4, 3, 2, 0, 3, 4, 0, 2, 3, 2, 3, 1, 4, 0, 2, 3, 4, 3, 0, 0, 0, 1, 1,
1, 2, 1, 3, 0, 3, 3, 4, 0, 0, 2, 3, 0, 2, 0, 3, 3, 1, 0, 4, 2, 3, 4, 1,
1, 1, 3, 0]
>>> [random.randint(0, 5) for i in range(100)]
[0, 2, 3, 2, 1, 5, 2, 0, 4, 5, 2, 1, 0, 1, 4, 4, 4, 0, 3, 0, 3, 0, 3, 0,
1, 2, 4, 3, 5, 2, 4, 2, 5, 2, 3, 3, 3, 0, 2, 4, 0, 1, 5, 0, 4, 0, 5, 0,
3, 3, 3, 1, 4, 1, 0, 3, 4, 0, 1, 5, 2, 4, 0, 0, 2, 1, 1, 1, 5, 0, 2, 4,
5, 5, 2, 4, 5, 1, 1, 2, 3, 1, 3, 4, 1, 3, 4, 4, 3, 2, 3, 0, 1, 1, 1, 4,
4, 4, 3, 5]
>>>


Thanks for taking the time to write, though! It was an easy mistake to
make, so don't let it discourage you from reporting any future bugs you
might spot – after you double-check, of course.



More information about the docs mailing list