# Sheldon Ross 10: Exercise 3.49

Question: A and B play a series of games with A winning each game with probability p. The overall winner is the first player to have won two more games than the other.

• Find the probability that A is the overall winner.
• Find the expected number of games played.

Simulation Solution

The code for the simulation is a little convoluted because we need to capture the absolute difference of the number of wins of the players A and B. The code is provided for you as a reference. Please drop me a line if you have any questions. Code

I have coded this in Mathematica only. This code is a little computationally extensive and you could adjust the number of loops based on the density of the data you wish to achieve.

```Framed@
Grid@
Partition[
Table[
With[{p = prob},
Module[{topSimulation, AWins, BWins, chartPair},
chartPair = {AWins, BWins};
topSimulation = Table[
Module[{simulation},
simulation = Table[
Module[{a = 0, b = 0, c = 0, probability = p},
While[True,
c += 1;
If[RandomReal[] <= probability, a += 1, b += 1];
If[Abs[a - b] >= 2, Break[]]];
{{"A", "B"}[[Position[{a, b}, Max[a, b]][[1, 1]]]], c}
]
, 100];
Mean /@ (#[] & /@ # & /@ GroupBy[simulation, #[] &])
],
1000];
{AWins, BWins} = {#["A"] & /@ topSimulation, #["B"] & /@ topSimulation};
DistributionChart[chartPair,
ChartElementFunction -> "PointDensity",
ChartLabels ->
Placed[{{"A", "B"}, N@Mean@Select[#, NumberQ] & /@ {AWins, BWins}}, {Below, Above}],
PlotRange -> {Automatic, {0, 10}},
AspectRatio -> 1, ImageSize -> 260,
PlotLabel -> Style["p = " <> ToString@prob, Italic]]
]
]
, {prob, Range[0.1, .9, 0.1]}], 3]```

End of the post 🙂

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.