#### Problem:

- Write a problem to simulate the tossing of a sequence of successive coin tosses.

- Search the suitably generated sequence to find a particular pattern, e.g., HTH. Report where this sequence was found – after how many flips in the generated sequence. Allow the user to enter the pattern to look for via the UI (User Interface).

- Modify your program to repeat steps 1 and 2 a variable number of times; allow the user to specify this value in the UI. Each repeat should use the same search pattern, but a new sequence of source coin tosses.

- Modify your program to keep track of where the pattern was found in each generated sequence. Report the overall average number of coin tosses required to the find the search pattern across all of the repeats.

- If your program is correct, you will find that the average number of tosses necessary to find the search pattern HTH is 10, however, HTT it is 8.
**Why is this (this is another conditional probability problem)?**

This isn’t another program idea/challenge …

If you think the HTH vs HTT is a bit odd (or not) how about this one:

“I have two children. One is a boy born on a Tuesday. What is the probability that I have two boys?”

It sounds trivial. The Tuesday bit is just window dressing, so we are looking at “I have two children, one a boy. What is the probability I have two boys?” So with one child a boy, surely there is 50 per cent chance that the other child is a boy and a 50 per cent chance it’s a girl. Which makes the probability of having two boys 0.5, or 50 per cent. There’s a one in two chance. But unfortunately that is not correct.

The reason we get confused is that when trying to imagine the situation we think of the ‘first’ child we come to being a boy, then look at the options for the second child being a boy. However the description of the situation would also work if the first child is a girl and the second child is a boy. The only way to be absolutely certain is to work through every possible combination. It’s a trifle tedious, but it delivers the result:

Child A | Child B | |

1. | Boy | Girl |

2. | Boy | Boy |

3. | Girl | Boy |

4. | Girl | Girl |

These are the four possible combinations, each equally possible. Of these, three are situations that match my initial statement ‘I have two children, one is a boy’. In all but case 4, one of the children is a boy. But only one of those three combinations with a boy also makes the second child a boy. So the answer to ‘I have two children, one a boy. What is the probability I have two boys?’ is not 50 per cent, or one in two, it is one in three!

But there is a more fiendish part. We were wrong in discarding the Tuesday. Saying the boy was born on a Tuesday changes the probability.

To see this we need a much bigger table. It starts like this:

Child A | Child B | |

1. | Boy (Mon) | Girl (Mon) |

2. | Boy (Tue) | Girl (Mon) |

3. | Boy (Wed) | Girl (Mon) |

4. | Boy (Thu) | Girl (Mon) |

… | ||

14. | Girl (Sun) | Girl (Mon) |

15. | Boy (Mon) | Girl (Tue) |

16. | Boy (Tue) | Girl (Tue) |

… |

In total we have 196 entries in this table. We go through every single sex/day combination in the first column combined with a girl born on Monday (fourteen of them in all), then every single sex/day combination in the first column combined with a girl born on Tuesday (fourteen of these too) and so on until we have cycled through every option for the second child. Now we need to know two things. How many of those pairs feature a boy born on a Tuesday (like item 2 above) and how many of those have a second boy? We are going to have one combination of child A as a boy born on Tuesday with every possible child B – fourteen of those, plus thirteen other combinations where child B was a boy born on Tuesday, but child A wasn’t (we have already counted the instance were both child A and child B are a boy born on Tuesday). So there are 27 rows that match our circumstance of having a boy born on Tuesday.

We now need to pin down how many of those rows had two boys. The first set of fourteen all had a boy as child 1, and half of those – seven also had a boy as child 2. Of the thirteen additional rows where child B was a boy born on a Tuesday, six would have child A also a boy. So of the 27 rows with a boy born on a Tuesday, thirteen of them have a second boy. The answer to ‘I have two children. One is a boy born on a Tuesday. What is the probability I have two boys?’ is 13 in 27 – almost, but not quite, one in two.

This really upsets common sense (see how bad humans are at probability!)