[issue34109] Accumulator bug
Victor Pires
report at bugs.python.org
Fri Jul 13 12:54:16 EDT 2018
New submission from Victor Pires <victorp1942 at gmail.com>:
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
----------
components: Interpreter Core
files: Bug.html
messages: 321623
nosy: vicpires
priority: normal
severity: normal
status: open
title: Accumulator bug
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7
Added file: https://bugs.python.org/file47689/Bug.html
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34109>
_______________________________________
More information about the Python-bugs-list
mailing list