It’s 50/50 – it either happens or it doesn’t!
There’s a cliche probability joke, where the answer to any question, no matter how complicated, is simply: “It’s 50/50 – either it happens or it doesn’t!” and now I’m proud to say I got that joke into a Couch Slouch column in WaPo: https://www.washingtonpost.com/sports/khrisdavisownssportsgreateststatisticaloddityever/2019/04/21/473ad022645011e982bafcfeff232e8f_story.html
I also managed to work 52factorial in as well, with full digit expansion! Enjoy.
python: Decoding multiple JSON strings in a file
I wanted to be able to decode a stream that had multiple, undelineated, JSON string representations in it. For example, a file like this:
[1, 2, 3] [4, 5, 6] [7, 8] [9, 10, 11, 12]
Which has multiple JSON arrays in it, separated only by (optional) whitespace. Perhaps they might be separated by other characters in other applications (e.g., comma separated JSON strings)
First stop, as always, was stackoverflow, which had some relevant answers, like this one:
I decided to write a variant of the several “read it in chunks, try to JSONDecode it, keep reading more if it doesn’t yet parse” solutions. Here is my take at this problem, as a gist:
https://gist.github.com/outofmbufs/bcf3bccc1fe0bb0824871ec5e02cc60e
Posting this in the hopes, as always, that someday someone will be looking for something like this and stumble across it via google (if I’ve somehow worked enough key phrases into this posting)
Supreme Ruler of the Universe: StarSpangled Banner
When I am appointed Supreme Ruler of the Universe, any “creative interpretation” of the Star Spangled Banner will be punishable in kind. The offender will be repeatedly subjected to a medley of Free Bird, “creatively interpreted,” until they see the error of their ways.
Supreme Ruler of the Universe: Are You a Robot?
Every now and then I make an observation about something I would do to improve everyone’s life, well — almost everyone — if only I were made Supreme Ruler of the Universe. I’m collecting these phrases and posting them periodically on my site.
For example:
When I am Supreme Ruler of the Universe, the devs who code the “click every picture that contains a car” robot detector will get run over by cars, ideally driven by robots.
I’ll be adding more; they will all be in the Supreme Ruler of the Universe category so you can find them easily when you are deciding whether you wish to support my humble campaign for this position.
Scotch: Ice Sphere vs One Large Ice Cube
A friend recently asked “what is the difference between an ice sphere and one (large) ice cube?”
Well, let’s do some math. First we have to make some simplifying assumptions, which honestly really would need to be verified. But let’s not let lack of evidence get in the way of doing some fun math!
The idea behind the sphere of ice is that it cools your drink while presenting the minimum surface area, and (presumably) therefore minimizing drink dilution. A sphere has the minimal surface area for a given volume, which is why soap bubbles generally form spheres.
Let’s assume that the total cooling capacity of an ice cube is related to its volume. If we have a cube with side length a, then the volume of that cube will be:
V_{c} = a^{3}
Similarly, if we have a sphere of radius r, its volume will be:
V_{s} = (4/3)πr^{3}
Since we want the same cooling capacity in both the cube and the sphere, we need to set the two volumes equal, and then we can determine what the cube sidesize a would be for the same volume as a sphere of radius r:
V_{c} = V_{s}
a^{3} = (4/3)πr^{3}
a = [(4/3)πr^{3}]^{(1/3)}
[1] a = [(4/3)π]^{(1/3)}•r
Equation [1] tells us what size cube we need (side length a) in terms of a sphere of radius r, so that the volume of the cube and the volume of the sphere are the same.
Now let’s look at surface area. Surface area of a sphere of radius r:
S_{s} = 4πr^{2}
Surface area of a cube of side length a:
S_{c} = 6a^{2}
which, using equation [1] to get that in terms of r becomes:
S_{c} = 6[(4/3)π]^{(2/3)}r^{2}
and therefore the ratio of the surface area of the cube, S_{c}, to the sphere, S_{s}, becomes:
S_{c}/S_{s} =6[(4/3)π]^{(2/3)}r^{2} / 4πr^{2}
=6[(4/3)π]^{(2/3)} / 4π
=3[(4/3)π]^{(2/3)} / 2π
= 1.24 (approx)
So the cube with the same total volume as a sphere will have about 24% more surface area. Still way better than having multiple small cubes of ice in your drink. Enjoy!
Coin flip simulation still running – 62 days!
The coin flip experiment I described before is still running… so far for 62 days. It is running on a dedicated linux box on my network:
nw@randomlinux$ uname a Linux randomlinux 4.9.06amd64 #1 SMP Debian 4.9.881 (20180429) x86_64 GNU/Linux nw@randomlinux:~$ lscpu Architecture: x86_64 CPU opmode(s): 32bit, 64bit Byte Order: Little Endian CPU(s): 4 Online CPU(s) list: 03 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 76 Model name: Intel(R) Atom(TM) x5Z8350 CPU @ 1.44GHz Stepping: 4 CPU MHz: 482.695 CPU max MHz: 1920.0000 CPU min MHz: 480.0000 BogoMIPS: 2880.00 Virtualization: VTx L1d cache: 24K L1i cache: 32K L2 cache: 1024K NUMA node0 CPU(s): 03
and the updated results are:
 Trials Completed: 5,008,009
 Total Coin Flips: 8,604,595,583,786
 Longest Trial: 2,785,974,537,774
 Trials Ending in 2 flips: 2,504,265 (50.005%)
Updated breakdown of the percentages of trials completed in N flips, and the corresponding prediction:
length (flips)  actual %  theoretical % 

2  50.005%  50.00% 
4  12.503%  12.50% 
6  6.244%  6.25% 
8  3.903%  3.906% 
10  2.736%  2.734% 
12  2.062%  2.051% 
14  1.610%  1.611% 
16  1.316%  1.309% 
I’m letting this run until the next power failure terminates the experiment. Unfortunately the little box this is running on is not set up on a UPS. My power typically glitches (short outages) a few times each year (I’ve written about this before) so I don’t know how long to expect this run to get, but as long as it is still running I will keep collecting the data. Let’s hope for no thunderstorms in the hilltop area any time soon.
Christmas Lights 2018
They are (finally) sort of working. There seems to be a wiring problem with some of the strings; consequently they are “out of sync” (e.g., red when the rest are green, etc). I am hopeful that will be fixed Friday… in the meantime, I just choose to interpret the cacophony of color as “festive” 🙂
Coin flips and Catalan Numbers
In a previous post I wrote about this question:
On average, how many times do you have to flip a (fair) coin to get an equal number of heads and tails?
and suggested that the answer was probably infinite (on average), although half the time you get there in just two flips: HT or TH, which are half the possible cases for two flips: HH, HT, TH, TT.
To review: there are 4 possible combinations of two flips: HH, HT, TH, TT. Each is equally likely. Two of them – HT and TH – reach equal heads/tails equality. Let’s call this “reaching delta zero” where delta refers to the difference in heads vs tails count. Since two out of four equallylikely sequences reach delta zero, there is a 50% chance of getting there in just two flips.
The next stage has four flips (three flips will never have delta zero), and there are sixteen possibilities for four flips. However, half of them were eliminated at two flips, leaving only the sequences that start with HH or TT:
HHHH, HHHT, HHTH, HHTT TTHH, TTHT, TTTH, TTTT
Two of these sequences reach delta zero: HHTT and TTHH. So there is a 2 out of 8 ( i.e., 1 out of 4) chance of reaching delta zero at four flips, except we have to account for the 50% chance of never even getting to four flips, so there is a 1 out 8 = 12.5% chance of finishing at four flips.
In the prior post I discussed the catalan numbers and how they relate to the number of these sequences. From that discussion we can compute the probabilities of a given trial run ending (reaching delta zero) at any particular stage. This works out as follows:
# FLIPS % reaching equality here 2 50% 4 12.5% 6 6.25% 8 3.91% 10 2.73% 12 2.05% 14 1.61%
Unlike the chart shown in the previous posting, these numbers are the chance of the trial ending at the specific level (e.g., a 3.91% chance any given trial run will take exactly 8 flips, no more / no fewer).
I wrote a simulation in python to test this, and I have been running it for over three weeks. Highlights of key results:

 Trials Completed: 3,414,318. This is how many runs were made where each run consisted of python code along these lines (except the actual code used some unrolling and optimization techniques I’ve written about before). This code is essentially simulating flipping a coin and counting the “delta” between the number of heads and tails, so each flip either adds one or subtracts one from that running delta and equality of heads and tails is reached when the delta becomes zero.
delta = random.choice((1, 1)) flips = 1 while delta != 0: delta += random.choice((1, 1)) flips += 1

 Total number of coin flips: 3,197,243,199,696. Yes, you are reading that correctly, over 3 trillion simulated flips. I will point out that python, like many (most? all?) runtimes these days uses the Mersenne Twister random number generator with period 2**199371; no older style random number generators that have much much shorter periods would be suitable for this sort of experiment.
 Longest run: 1,204,531,561,152. Yes, that’s another “you read that right”. The longest string of coin flips it took to reach equality was a little over 1.2 trillion. That is, after the first flip generated a delta of +1 or 1, in that sequence it wasn’t until 1.2 trillion flips later that the delta was back down to zero.
 Runs ending at 2 flips: 1,707,887 (50.02%). This is as we predicted. experimental results error is 728 out of over 3.4M trials, which comes to a difference of about 213 ppm (or however you want to think of that difference from the perfect 50%).
Here’s a breakdown of the percentages of trials completed in N flips, and the corresponding prediction:
length (flips)  actual %  theoretical % 

2  50.02%  50.00% 
4  12.498%  12.50% 
6  6.251%  6.25% 
8  3.901%  3.906% 
10  2.739%  2.734% 
12  2.061%  2.051% 
14  1.613%  1.611% 
16  1.312%  1.309% 
I found this writeup nicely describing the math of this and in particular analyzing that the average will indeed diverge to infinity.
The results of seeminglysimple probability questions are often counterintuitive. “How long will it take me to get an equal number of heads and tails while flipping a coin?” and the answer is “fairly often it could take a very very long time”
Flipping a coin until reaching equal heads and tails
Today’s question is: how many coin flips, on average, would we have to do if we keep flipping a coin until the number of heads equals the number of tails?
Obviously it takes at least two, and might take many more. After two flips there are four possible outcomes:
 HH: Heads on flip 1, heads on flip 2.
 HT: Heads on flip 1, tails on flip 2.
 TH: Tails on flip 1, heads on flip 2.
 TT: Tails on flip 1, tails on flip 2.
From basic probability theory (which you should go review if necessary), each of these four outcomes is equally likely. Two out of the four of them have an equal number of heads and tails; therefore, we know that 50% of the time we try: “Flip a coin until the number of heads and tails are equal” we will be done in two flips. But the other 50% of the time we will have to keep going.
How much longer in that other 50% of the time? The first thing that should be obvious is that there will have to be an even number of flips. There will never be an equal number of heads and tails in 3 flips, or 5 flips, or any odd number of flips.
So we have to do at least 2 more flips 50% of the time we start out (i.e., whenever we start out with HH or TT).
Starting with HH the next string of possibilities is:
 HH HH
 HH HT
 HH TH
 HH TT – this one reaches equal heads/tails
Similarly, starting with TT:
 TT HH – this one reaches equal heads/tails.
 TT HT
 TT TH
 TT TT
This time there were eight possibilities, and only 2 of them reach equality. Note that even though there are four flips of the coin there aren’t 16 possibilities, because eight of the sequences start with either HT or TH and therefore reach equality of heads and tails at 2 flips. Or looked at another way, those branches of the 2**n tree (2 possibilities for 1 flip, 4 possibilities for 2 flips, 8 for 3 flips, 16 for 4 flips, etc) were pruned off.
To see this explicitly, here is the complete list of all 16 possible sequences of 4 flips:
HHHH HHHT HHTH HHTT HTHH ** Never happens; equality after HT start HTHT ** ... HTTH ** ... HTTT ** ... THHH ** Never happens; equality after TH start THHT ** ... THTH ** ... THTT ** ... TTHH TTHT TTTH TTTT
So, in review:
 50% of the time 2 flips is enough.
 In the remaining 50% case:
 25% of the time (2 cases out of 8 as shown above) 4 flips is enough.
 75% of the time (6 cases out of 8) more than 4 will be needed.
We can start to do some math:
Percent of time reach equality in 4 flips or fewer: 50% in 2 flips 25% of 50% = 12.5% in 4 flips 75% of 50% = 37.5% takes more than 4 flips
We can now say 62.5% (50% + 12.5%) of the time it will take 4 flips or fewer (i.e., 2 or 4) to get to equality, and 37.5% of the time it will take more than 4.
How many more? Carrying this casebycase analysis out further gets messy quickly. Fortunately this problem is related to something called a Catalan number. A description of them can be found in the Online Encyclopedia of Integer Sequences (A000108). There is also a wikipedia entry which I paraphrase here:
There are many counting problems in combinatorics whose solution is given by the Catalan numbers.
C(n) is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X’s and n Y’s such that no initial segment of the string has more Y’s than X’s.
For example, the following are the Dyck words of length 6:
XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY.
The way they describe that is a bit confusing, plus we are actually interested in both XXXYYY (HHHTTT) and YYYXXX (TTTHHH). Here’s another description from wikipedia:
C(n) is the number of ways to form a “mountain range” with n upstrokes and n downstrokes that all stay above a horizontal line. The mountain range interpretation is that the mountains will never go below the horizon.
and this accompanying picture:
source/attribution: Wikipedia File:Mountain_Ranges.png
This is better and more directly illustrates why the numbers we need are two times the Catalan number, because we are interested in both mountain ranges that start by going up first (e.g., first flip is heads and eventually we need to get back “down” to equal heads/tails) or by going down first (e.g., first flip is tails and eventually we need to get back “up” to equal heads/tails).
Here is a listing of all possible sequences of 2 flips, 4 flips, 6 flips, 8 flips, and 10 flips that end up with an equal number of heads and tails:
# 2 sequences of 2 flips: 2 * Catalan(n=0) HT TH # 2 sequences of 4 flips: 2 * Catalan(n=1) HHTT TTHH # 4 sequences of 6 flips: 2 * Catalan(n=2) HHHTTT HHTHTT TTHTHH TTTHHH # 10 sequences of 8 flips: 2 * Catalan(n=3) HHHHTTTT HHHTHTTT HHHTTHTT HHTHHTTT HHTHTHTT TTHTHTHH TTHTTHHH TTTHHTHH TTTHTHHH TTTTHHHH # 28 sequences of 10 flips: 2 * Catalan(n=4) HHHHHTTTTT HHHHTHTTTT HHHHTTHTTT HHHHTTTHTT HHHTHHTTTT HHHTHTHTTT HHHTHTTHTT HHHTTHHTTT HHHTTHTHTT HHTHHHTTTT HHTHHTHTTT HHTHHTTHTT HHTHTHHTTT HHTHTHTHTT TTHTHTHTHH TTHTHTTHHH TTHTTHHTHH TTHTTHTHHH TTHTTTHHHH TTTHHTHTHH TTTHHTTHHH TTTHTHHTHH TTTHTHTHHH TTTHTTHHHH TTTTHHHTHH TTTTHHTHHH TTTTHTHHHH TTTTTHHHHH
Because we have two variations (heads first, tails first), the number of sequences possible for a given generation (“n” value) is double the catalan number C(n). The formula for the nth Catalan number is:
# python code. Note: // is integer division def catalan(n): return comb(2*n, n) // (n + 1)
where comb() is the combinatoric function “m take n”.
Given all this we can compute how many patterns of n flips will result in our experiment finishing with an equal number of heads and tails. First let’s just print out a bunch of Catalan numbers (times 2):
import math def comb(m, n): if m < n: raise ValueError("requires m >= n") top = 1 for i in range(n): top *= mi return top // math.factorial(n) def catalan(n): return comb(2*n, n) // (n + 1) for n in range(20): print("flips={:2d}, 2C(n={})={}".format( 2*(n+1), n, 2*catalan(n)))
Running this gives:
flips= 2, 2C(n=0)=2 flips= 4, 2C(n=1)=2 flips= 6, 2C(n=2)=4 flips= 8, 2C(n=3)=10 flips=10, 2C(n=4)=28 flips=12, 2C(n=5)=84 flips=14, 2C(n=6)=264 flips=16, 2C(n=7)=858 flips=18, 2C(n=8)=2860 flips=20, 2C(n=9)=9724 flips=22, 2C(n=10)=33592 flips=24, 2C(n=11)=117572 flips=26, 2C(n=12)=416024 flips=28, 2C(n=13)=1485800 flips=30, 2C(n=14)=5348880 flips=32, 2C(n=15)=19389690 flips=34, 2C(n=16)=70715340 flips=36, 2C(n=17)=259289580 flips=38, 2C(n=18)=955277400 flips=40, 2C(n=19)=3534526380
In the output above, think of n as the generation number where each generation is two flips. Like many things in computers and math we number the generations starting at zero, so the zeroth generation has 2 flips, and there are 2*C(0) == 2 combinations (HT, TH) that reach equality at that generation. Similarly the second generation (n=1) has four flips, and two of the sequences of four flips will reach equality at that generation. We showed this explicitly above by listing all the possibilities. These Catalan numbers become large quickly, confirming that explicit casebycase analysis is going to get quite difficult. We need to figure out some general math instead.
The next thing we need to figure out is how many of the sequences there will be in each generation. We already know that it is less than 2**flips because some of them are cut off each generation – as was shown when there were only 8 sequences to consider at four flips (instead of 2**4 which would be 16).
If there are q sequences in generation n, the number of them that have equal numbers of heads and tails is given by 2*C(n); those sequences will not continue any further. Each remaining active sequence in generation n becomes four possible sequences in generation n+1, from appending HH, HT, TH, and TT outcomes.
Therefore, if q(n) is the number of active sequences in generation n, the number of sequences in generation n+1 will be:
q(0) = 4 # by definition q(n+1) = 4 * (q(n)  2*C(n))
Picking that back apart: q(n) is the number of sequences there will be at generation n, and 2*C(n) is the number of those that will reach equality. Therefore we subtract 2*C(n) from q(n) and that is the number of sequences that need to go for more flips; each of those sequences will expand to four possibilities in the next generation, hence the multiplication by 4.
It’s more convenient to express that equation in terms of the current generation and the previous generation (rather than current and next):
q(0) = 4 q(n) = 4 * (q(n1)  2*C(n1))
As alread noted, some of those sequences will reach equality; for example this formula gives eight sequences at n=1 (four flips). We listed those eight before, and two of them (2*C(1)) reach equality at that generation (so six of them go on to n=2 / six flips).
This is a recursive formula that doesn’t let us compute an arbitrary q(n) without going through all the cases leading up to it, but just for grins lets write some code and print out a list of q(n) values and see if we agree with it:
for n in range(10): if n == 0: q = [4] else: q.append(4 * (q[n1]  (2*catalan(n  1)))) # q[i] will be the computed value at generation i print(q)
This is some quickanddirty code that only works if we request the q values sequentially (as is done in this example), but it’s good enough for now. Running this (with the catalan function as previous defined) results in:
[4, 8, 24, 80, 280, 1008, 3696, 13728, 51480, 194480]
This matches the results we got manually – at n=0 (the very first pair of flips) there are 4 sequences to consider: HH, HT, TH, TT. At n=1, four flips, there are eight sequences to consider and we listed them above. We know, from that list, that 2 of those eight sequences reach equality, leaving 6 “active” sequences for the next generation, and we know that each of those 6 active sequences will turn into four new sequences (from appending HH, HT, TH, and TT) … so there will be 24 sequences to consider at the next generation.
Ok, so what does this tell us about how many times we’re going to have to flip that coin, on average, to get back to equal?
We have all the tools needed to calculate the chance that you reach equality by (or before) generation n. Let E(n) be the probability that we have an equal number of heads and tails by generation n (i.e., in 2*(n+1) flips). We know, by definition (inspection of all the cases: HH, HT, TH, TT) that E(0) = 0.50 (50%). The probability of reaching equality by arbitrary generation n will be made up of two components:
 the chance you reached equality by or before generation n1, which is
E(n1)
 the chance you had to keep going, which is whatever is left after you take out the E(n1) chance you already finished; so that is
1  E(n1)
, which we will then multiply by the chance that you reach equality on this nth generation.
We know from the above formulas that q(n) is the number of sequences “in consideration” in generation n. We also know that the number of sequences that will reach equality in generation n is given by: 2C(n) (twice the catalan number for n).
Therefore we know the chance you reach equality in a specific nth generation is the number of such sequences that reach equality, divided by the total number of sequences that would be in consideration at that generation:
2C(n)/q(n)
You can verify that yourself with the four flip case (n=1), where there were 8 sequences (q(1) == 8) and 2 of them (C(1) == 1, 2 times that is 2) reached equality, leading to a 25% chance of being done in that case (with that case only happening 50% of the time). Putting all that together we finally get:
E(n) = E(n1) + (1 – E(n1)) * 2C(n)/q(n)
Again we have a recursive formula that requires us to iterate through the cases. Here’s some code:
def q(n): if n == 0: return 4 else: return (4 * (q(n1)  (2*catalan(n1)))) def bigE(n): if n == 0: return 0.50 else: em1 = bigE(n1) return em1 + ((1  em1)*((2*catalan(n)) / q(n)))
Python experts will immediately realize there are several efficiency and memory problems lurking here; on my machine trying bigE(500) takes about 5 seconds, and for large enough value bigE will fail with an exception because the recursion gets too deep. We can fix those problems but for now this code suffices to just get a feel for some results. I ran the calculations for some values and printed out the chance of NOT reaching equality (1E(n)) by the nth generation:
Chance equality NOT reached after 2 flips: 50.00% Chance equality NOT reached after 4 flips: 37.50% Chance equality NOT reached after 6 flips: 31.25% Chance equality NOT reached after 8 flips: 27.34% Chance equality NOT reached after 10 flips: 24.61% Chance equality NOT reached after 20 flips: 17.62% Chance equality NOT reached after 50 flips: 11.23% Chance equality NOT reached after 100 flips: 7.96% Chance equality NOT reached after 200 flips: 5.63% Chance equality NOT reached after 400 flips: 3.99% Chance equality NOT reached after 600 flips: 3.26% Chance equality NOT reached after 800 flips: 2.82% Chance equality NOT reached after 1000 flips: 2.52%
These values seem pretty counterintuitive to me. A 2.52% chance of not reaching equality within 1000 flips is huge. It means that if you sit down periodically and try this yourself with a coin (assuming it is a fair coin), about 1 time out of 40 you will have to do 1000 flips and still would have not reached equality. I think most people when asked how frequently that would happen (especially people who misunderstand the “law of averages”) would guess it to be very rare, and certainly not as likely as a 1 in 40 shot.
I opened this posting with the question “what would be the average number of flips it takes to reach equality” but I’m still not able to answer that question. I can’t tell yet whether these formulas, with their tailingoff values as they get large, converge onto a bounded number or whether in fact the average number of flips to reach equality might be unbounded (i.e., infinite). I’m going to wrap up this post just to get it “out there” and do some more google research later. I do wonder if I may have reached the limit of my mathematical pay grade in this area.
I have written a python simulator to do some “flip until equality” experiments, but I’m not yet prepared to write it up as I want to ensure that its results are valid, and without better math support I’m not sure yet. I will say the preliminary results from the python simulator suggest that the answer to “what is the average” might be “infinity” (!!) though I am not yet certain of that. I have definitely had some astoundinglylong runs come out of the simulator so far though — the longest I’ve seen at the moment being over 380 billion flips before equality was reached (I *think* the python random number generator is suitable for this task, having a period of 2**19937, but that’s just one of many questions I have at the moment about my simulation code).
With the caveat that I’m not 100% certain my code’s results are correct, here are some preliminary results of interest from the simulation.
I’ve run 1,607,009 trials (sequences of coin flips, each starting with one flip and finishing when the number of heads and tails is equal). Out of those trials, 804,083 reached equality in two flips. That’s 50.036% which tracks the computed/expected result correctly. Equality in four flips was reached in 200,809 trials, or 12.496% which also tracks the 12.5% prediction. The longest sequence seen was 380,172,077,974 flips — 380 billion flips!
The top ten longest flip sequences at the moment are:
380,172,077,974 33,378,243,830 27,175,971,118 25,567,423,414 22,337,890,602 11,709,891,508 11,678,616,684 10,918,847,300 10,600,505,508 9,424,437,132
These results are fueling my suspicion that the overall average might be unbounded… i.e., that these huge sequence length numbers will occur frequently enough to drive the average towards infinity. But I’m not certain of that (can’t really be certain of it until we do more math).
Sequences of length 2 occurred 804,093 times, length 4 occurred 200,809 times, etc so if you imagined a very long list of numbers – 804,093 entries with “2”, 200,809 entries with “4”, etc, and added them all up and computed the average entry value you’d get 398,917 (obviously I didn’t do this manually, I did it by weightedaverage of my recorded data points). This number might be meaningless though, if my speculation about the average being unbounded is correct (i.e., if the average will continue to increase as more trials are run). But at the moment if you ask me to answer “Dammit, Neil, what’s the average?” I guess I’d say 400K flips needed, on average, to reach equality. Again that’s a huge number and somewhat counterintuitive; you’d think a fair coin would reach my heads/tails equality criteria in “some reasonable” amount of time. Most human intuitions about probability are wrong, and assuming my simulation is returning valid results, this appears to be yet another case of that.