A first pass at the simple problem#
Remember the three girls in a family problem?
Now we have variables and functions, we can do a first pass at solving the problem.
First we import
Numpy, and create a Random Number Generator (RNG):
import numpy as np
# Make a random number generator
rng = np.random.default_rng()
# Show the result
rng
Generator(PCG64) at 0x7F0F6C20B220
This RNG can give us random numbers from 0 up to, but not including 2, like this:
# Generate a random number that is either 0 or 1
rng.integers(0, 2)
1
You can run the same cell over and over by pressing Cmd (Mac) or Control (Windows) and Enter together (Cmd - Enter or Control - Enter). This is like Shift - Enter, except Cmd/Control - Enter leaves the focus inside the cell, so you can run the same again immediately.
We take 1 to mean a girl, and 0 to mean a boy.
Now we have variables, we can do better than running this cell four times, and counting the number of times we see 1.
# Get four random numbers between 0 and 1.
a = rng.integers(0, 2)
b = rng.integers(0, 2)
c = rng.integers(0, 2)
d = rng.integers(0, 2)
# Count the number of ones by adding them up. Show the result.
a + b + c + d
3
Using Cmd/Control - Enter, run this cell 100 times. Count the number of times you see the result 3. Divide that number by 100, and you have an estimate of the number of families with four children that have exactly three girls.
Still, it’s boring to have to run that cell 100 times. We might also like the computer to execute these steps many more times - say - 10000 times. We will need to do some more programming to tell the computer how to do this.