Python Accumulator Bug

Victor Jose Novaes Pires

2018-07-13

A function to return a number from 1 to 5 (inclusive) sometimes returns -1 when called thousands of times.

In [1]:
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])
3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51) 
[GCC 7.2.0]
-1 431
0 0
1 199425
2 201727
3 200219
4 199099
5 199099

Python 2.7

$ 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)

Python 3.5

$ 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

Python 3.6

$ 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

Python 3.7

$ 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