A simpler problem¶

Imagine a family with four children.

What is the probability that the family will have exactly three girls?

There are various ways to answer this question. One way, is to use simulation.

Simulation makes a model of the problem. We use the model to generate simulated data. If the model is a good one, the simulated data should look like the real data.

First we need to simulate a family of four children.

Then we need to count the number of girls.

We do this many many times, and see how often we get a count of 3.

In our model, the chances of any one child being a boy or a girl is 0.5, or 50%. To do our simulation, we need something random, like the toss of a coin, to decide if the next child is a boy or a girl.

Tossing a coin is a bit time-consuming, and your computer is well designed to solve this task in a more efficient way. It can generate random numbers.

In this next cell, we fetch a routine (actually, a function) to generate random numbers. We fetch it from the Numpy library, of which you will see much more. For now, all you need to know is that the function we want is called `randint` in the Numpy `random` sub-library. We fetch it like this:

```# Fetch the randint function from the Numpy library.
from numpy.random import randint
```

Now we have the `randint` function, we can run the following cell 4 times, to get 4 random numbers between 0 and 1.

Call 0 a boy, and 1 a girl. If we run this four times, then we have one simulated family. We can count how many 1s (girls) we got in the four runs, and that is the simulated number of girls, for this family.

```# Return a random number that is either 0 or 1.
# The second arguments, 2, tells the function to return numbers up to, but
# not including 2.
randint(0, 2)
```
```0
```

`randint` is a function.

`randint(2)` calls the function, and returns a random number, that is either 0 and 1.

It’s inconvenient to have to run this cell many times. We really need some machinery to make the computer do that for us. We need variables, functions, comparisons and arrays. We will deal with those next.