This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vicpires
Recipients vicpires
Date 2018-07-13.16:54:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1531500850.33.0.56676864532.issue34109@psf.upfronthosting.co.za>
In-reply-to
Content
A function to return a number from 1 to 5 (inclusive) sometimes returns -1 when called thousands of times.

from random import randint
import sys

def rand5():
    """Returns a random integer from 1 to 5 (inclusive)"""
    r5 = -5 # This *should* accumulate from zero to 24
    for _ in range(4):
        r5 += randint(1, 7)

    for i in range(5):
        if (r5 == -1): # BUG: This should never happen, r5 = [0; 24]
            return r5
        if (r5 in [(i + j*5) for j in range(5)]):
            return (i + 1)


d = {key: 0 for key in range(-1, 6)} # Should only be range(1, 6)...

for _ in range(int(1e6)): # One million
    d[rand5()] += 1

print(sys.version)
for key in d:
    print(key, d[key])



$ python2.7 Bug.py
2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609]
(0, 0)
(1, 200514)
(2, 200273)
(3, 200689)
(4, 199588)
(5, 198543)
(-1, 393)

$ python3.5 Bug.py
3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
0 0
1 200956
2 200529
3 200017
4 199047
5 199030
-1 421

$ python3.6 Bug.py
3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51)
[GCC 7.2.0]
-1 421
0 0
1 200562
2 202025
3 200116
4 198899
5 197977

$ python3.7 Bug.py
3.7.0 (default, Jun 28 2018, 13:15:42)
[GCC 7.2.0]
-1 433
0 0
1 200039
2 200676
3 200629
4 199322
5 198901
History
Date User Action Args
2018-07-13 16:54:18vicpiressetrecipients: + vicpires
2018-07-13 16:54:10vicpiressetmessageid: <1531500850.33.0.56676864532.issue34109@psf.upfronthosting.co.za>
2018-07-13 16:54:10vicpireslinkissue34109 messages
2018-07-13 16:54:10vicpirescreate