**Question: **Find the expected number of flips of a coin, which comes up heads with probability p, that is necessary to obtain the pattern h, t, h, h, t, h, t, h.

**Analytical Solution**

N/A

**Simulation Solution**

It takes a few lines of code(provided below) to simulate this problem.

**Code**

accumulatingMean[list_List] := N[Accumulate[list] / Range[1, Length[list]]]

With[{runs = 10000},
accumulatingMean@Table[Module[{sequence = RandomChoice[{0, 1}, 8], i},
i = Length@sequence;
While[True,
If[sequence == {1, 0, 1, 1, 0, 1, 0, 1}, Break[]];
sequence = Rest[sequence] ~ Join ~ {RandomChoice[{0, 1}]};
i += 1;
];
i
], runs] //
DistributionChart[#, "ChartElementFunction" -> "PointDensity",
ImageSize -> 788,
ChartLegends ->
Placed["Distribution of the number of trials needed to get the \
sequence {1,0,1,1,0,1,0,1} " <> ToString[runs] <> " runs", Below]] &]

End of the post

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

### Like this:

Like Loading...