[issue24546] sequence index bug in random.choice

Steven D'Aprano report at bugs.python.org
Thu Jul 2 05:54:01 CEST 2015


Steven D'Aprano added the comment:

Your example of int(0.99999999999999995) returning 1 is misleading, because 0.999...95 is already 1.0. (1.0 - 1/2**53) = 0.9999999999999999 is the nearest float distinguishable from 1.0.

It seems to me that either random() may return 1.0 exactly (although I've never seen it) or that 0.9999999999999999*len(s) rounds up to len(s), which I guess is more likely. Sure enough, that first happens with a string of length 2049:

py> x = 0.9999999999999999
py> for i in range(1, 1000000):
...     if int(i*x) == i:
...             print i
...             break
...
2049


However your string has length 35, and it certainly doesn't happen there:

py> int(x*len(s))
34

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24546>
_______________________________________


More information about the Python-bugs-list mailing list