random.randint() wierdness.

Alex Martelli alex at magenta.com
Thu Aug 3 06:15:14 EDT 2000


"Steve Juranich" <sjuranic at condor.ee.washington.edu> wrote in message
news:Pine.SOL.3.96.1000802151516.21059D-100000 at condor.ee.washington.edu...
> Could somebody please explain this?
>
> >>> for i in range(20):
> ...     random.randint(1, 32)
> ...     print i
> ...
> 14
> 0    <---------   I don't get it.  Why?

Because, after 'just throwing into the void' the
random number (the interactive interpreter prints
what you 'throw into the void'), you explicitly
print i, whose first value is 0.  range(0,20), in
fact, returns the values 0, 1, 2, ... 17, 18, 19.

> 14
> 1
    [snip]
> 14
> 18
> 32
> 19

...as also shown by this sequence -- alternate
random numbers (printed for you by the
interactive interpreter; would be ignored in
a script) and then the growing value of i.


> As I read the library reference, it says that random.randint() is supposed
> to return an integer between 1 and 32 (as I have called it above).
However,
> the output shows that this is clearly not the case.  Could somebody please
> tell me what's going on?  Is this a bug?  Thanks.

The output shows it's doing exactly that.  Is your
print i a bug?  I don't know, because I don't know
what you were trying to do.


Alex






More information about the Python-list mailing list