On Tue, Dec 06, 2022 at 07:58:09PM -0500, David Mertz, Ph.D. wrote:

> You have an error in the code you posted. You never use R2 after one 
> call to SystemRandom.

Ah so I do, thanks for picking that up!

James, see how *easy* it is for experts to notice bugs, at least some of 
them, in a short piece of code that gets right to the point? The more 
extraneous and irrelevant code you give, and that includes old dead 
comments, the more places for bugs to hide and the harder it is for 
people reading to follow the code and spot the error.

Here is the corrected code (only one line needs to be fixed) and its 
output. Note that the conclusion doesn't change.


```python
import random
from statistics import mean, stdev
R2 = random.SystemRandom()
d2 = [R2.randint(1, 9) for i in range(100000)]
triples = 0  # Count the number of triples.
for i in range(len(d2)-3):
    a, b, c = d2[i:i+3]
    if a == b == c:  triples += 1

print("mean =", mean(d2))
print("stdev =", stdev(d2))
print("runs of three:", triples)
```

And the output:

```
mean = 4.9922
stdev = 2.5837929328526306
runs of three: 1204
```



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6OWHLTZW5X65DRVYO4OOR2XHGRPAZ4SP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to