
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
participants (1)
-
Steven D'Aprano