Picking balls out of a box, containing a mixtures of different balls gives some interesting probability puzzles.
In this one, suppose a ball is picked from random from a box containing 6 red balls, 4 white balls, and 5 blue balls. Here are some questions: What is the chance the ball picked is red? blue? not red? red or white?
Here, we'll represent the box of balls as an array as shown in the code below by the balls = ... line. As usual, N will be the number of times you'd like to reset everything, and pick a ball. The varible gotit ("got it") will count the number of times you picked the ball whose color you wanted.
Now you try. Choose a value of N and edit the ?? to be a color ball you'd like to pick ("r", "w", or "b").
Type your code here:
See your results here:
Now, let's connect this code to some basic probability "common sense" theory. For the red ball, you can think of choosing a red ball as
$$\frac{\textrm{ways of choosing a red ball}}{\textrm{total ways of choosing a ball}}$$
which here is $6/15=0.4$, which is what your code should display for large enough $N$.
Here's another way of thinking about it: The sample space of balls is 15 total possible. If you have an equally likely chance of choosing any ball, then there's a $1/15$ chance of choosing any ball. Since there are 6 red balls, you then have a $6\times 1/15$ chance of choosing a red ball. Picking a white or blue ball can be calculated using a similar argument. (Chance of blue=5/15=1/3, chance of white=4/15.)
What about the "not red?" Well, this will just be 1-"the chance of picking red." or $1-0.4=0.6$. "1-something" in this business kind of means "not," or "not red." Why? Well 1 means 100% certainty, and if 0.4 of the certainty goes into picking a red ball, then the "rest" or $1-0.4$ would mean picking a ball that was not red.
You can also think of the "not red" in this way:
$$\frac{\textrm{ways of choosing a blue or white ball}}{\textrm{total ways of choosing a ball}}$$
which for "not red" (i.e. a blue or white ball) would be (4+5)/15=9/15=0.6.
Choosing a red or white ball can be computed in your code with if balls[b] == "r" or balls[b] == "w" then. The answer can be computed via:
$$\frac{\textrm{ways of choosing a red or white ball}}{\textrm{total ways of choosing a ball}}$$
which here would be 10/15=0.3.
Share your code
Show a friend, family member, or teacher what you've done!