Issue34109
Created on 2018-07-13 16:54 by vicpires, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg321623 - (view) | Author: Victor Pires (vicpires) | Date: 2018-07-13 16:54 | |
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
|
|||
| msg321624 - (view) | Author: Tim Peters (tim.peters) * ![]() |
Date: 2018-07-13 17:04 | |
? I expect your code to return -1 about once per 7**4 = 2401 times, which would be about 400 times per million tries, which is what your output shows. If you start with -5, and randint(1, 7) returns 1 four times in a row, r5 is left at -5 + 4 = -1. |
|||
| msg321625 - (view) | Author: Victor Pires (vicpires) | Date: 2018-07-13 17:17 | |
Sorry, you are right |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:03 | admin | set | github: 78290 |
| 2018-07-13 17:17:15 | vicpires | set | status: open -> closed resolution: not a bug messages: + msg321625 stage: resolved |
| 2018-07-13 17:14:19 | vicpires | set | files: - Bug.html |
| 2018-07-13 17:04:13 | tim.peters | set | nosy:
+ tim.peters messages: + msg321624 |
| 2018-07-13 16:54:10 | vicpires | create | |
