DATA SCIENCE · CYCLE 2 · L20 · TEXTBOOK CHAPTER
Can You Fake Random?
A coin has no memory. People forget that, and it costs them: they see a streak and bet that it must end. Today you work out what chance says before you flip, and see what really happens when you do.
After this chapter, you can
- Find a theoretical chance, and the chance of two or three in a row.
- Say why the next flip does not depend on the last ones.
- Compare an observed share with a theoretical chance.
- Use
=RANDBETWEEN to flip 100 and 1,000 times.
For your project: your ANALYSIS tab starts with your question, your number and your two groups. The checkpoint is at the end.
Why It Matters
Monte Carlo, 1913.
At the Monte Carlo Casino on 18 August 1913, the roulette ball landed on black 26 times in a row. As the streak grew, gamblers bet more and more on red, sure that red was due. They lost, spin after spin.
The wheel has 37 pockets: 18 red, 18 black and one green zero. On every spin the chance of red is 18 in 37. On spin 27, after 26 blacks, the chance of red was still 18 in 37, the same as on spin 1. The wheel did not remember the last spins. Red was never due.
The Idea
A coin has no memory
Each flip is a new chance, the same as the first. Results like that are independent: one result does not change the chance of the next.
| What it is | When you know it |
| theoretical chance | the ways it can happen ÷ all the equally likely ways | before you flip |
| observed share | the times it happened ÷ the tries | after you flip |
In a few tries the observed share can be far off. In many tries it settles near the theoretical chance.
Two or three in a row? Each flip is independent, so multiply the chances. That gives the chance of the whole streak, worked out before it starts. Once part of the streak has happened, the next flip is a new chance again.
Worked Examples
Example 1 · Count the equally likely ways. Roll a fair six-sided die. What is the chance of a 5 or a 6?
Create a strategyList every way the die can land. They are equally likely. Count the ways that give a 5 or a 6.
Apply the ideaThe die can land 1, 2, 3, 4, 5 or 6: 6 equally likely ways. Two of them, 5 and 6, count. The chance is 26, which is 1 in 3, or about 33.3%.
Reflect and checkThe ways that count can never be more than all the ways, so a chance is never more than 1 in 1, 100%. Here 2 of 6 is a third of the faces, and a third of 100% is about 33.3%.
Example 2 · Three heads in a row. Flip a fair coin three times. What is the chance of three heads?
Create a strategyEach flip is heads with chance 12, and the flips are independent, so multiply. Check it by drawing every way three flips can go.
Apply the idea
12 × 12 × 12 = 18, which is 1 in 8, or 12.5%.
Every way three flips can go: 8 equally likely paths. One of them, HHH, is three heads.
Reflect and checkThe tree agrees: 1 path of 8. Now say you have flipped HHH already. The chance that flip 4 is heads is 1 in 2, the same as flip 1. The 1 in 8 was the chance of the whole streak, worked out before the first flip. Four heads in a row, before any flip, is 1 in 16.
Example 3 · Observed against theoretical. Sam flipped a fair coin 40 times and got 26 heads. The class pooled its flips: 2,000 flips, 1,012 heads. Compare each with the theoretical chance.
Create a strategyObserved share = the times it happened ÷ the tries. The theoretical chance of heads is 50% every time.
Apply the idea
| Flips | Heads | Observed share | Theoretical chance |
| 40 | 26 | 26 ÷ 40 = 65% | 50% |
| 2,000 | 1,012 | 1,012 ÷ 2,000 = 50.6% | 50% |
Reflect and checkSam’s 65% does not mean the coin is broken. Forty flips is few, and a few tries wobble. Two thousand flips settled within one point of 50%. The more tries, the closer the observed share comes to the theoretical chance.
In Sheets: Flip a Coin 1,000 Times
- Go to sheets.new.
- Click cell A1. Type:
=RANDBETWEEN(0,1) Press Return.
- Click cell A1. Press Command-C.
- Click the Name box, left of the formula bar. Type A1:A1000. Press Return.
- Press Command-V. One coin, flipped 1,000 times. 1 is heads, 0 is tails.
- Click cell L1. Type:
=AVERAGE(A1:A100) Press Return.
- Click cell L2. Type:
=AVERAGE(A1:A1000) Press Return.
- New flips: click cell W10, type x, press Return. L1 and L2 change.
The mean of 1s and 0s is the share of 1s. So L1 is the observed share of heads after 100 flips, and L2 is the share after 1,000. Your task sheet does the same for ten coins at once, in columns A to J. The flips change every time the Sheet changes. To keep a set, freeze it: select the flips, press Command-C, then Edit > Paste special > Values only.
The Same in Python
The same 1,000 flips, in four lines. Python came in L19b; one word here is new. .sample( ) draws rows at random, the way =RANDBETWEEN does in Sheets.
In Sheets this is: =RANDBETWEEN(0,1) pasted into A1:A1000, then =AVERAGE(A1:A1000).
In English:
- Get the pandas toolbox and call it pd.
- Make a column of 0 and 1, and call it coin. 1 is heads, 0 is tails.
- Draw from coin 1,000 times, putting each one back, and call the draws flips.
- Print the mean of flips: the share of heads.
Predict: near what number will it print? Run it three times. Then change 1000 to 10 and run it three more: which wobbles more?
Can’t run it here? Show what it prints
0.507
Yours will differ: the flips are new every run. With 1,000 flips the share stays close to 0.5. With 10 it can print 0.2 or 0.8.
Want to type your own? The Python practice page has tasks and a notebook.
Try It: Wobble, Then Settle
Press each button a few times. Each press is a new set of flips, and drops a dot at its share of heads. Watch which row spreads out and which row bunches up.
Can You Fake Random?
Ask people to write 100 coin flips from their heads and they switch from H to T too often, and avoid long streaks. Real flips have longer streaks than people expect. A computer flipped 100 coins 20,000 times: 81% of those sets had a streak of 6 or more in a row. Only about 3 sets in 100 had no streak longer than 4, so a list of 100 like that is probably made up.
In your project
Your Cycle 2 project ends on how sure you are that a difference between two groups is real. Chance makes every share and every mean wobble, the way 40 flips wobble. Later in this cycle you will shuffle your own data to see how big a gap chance alone can make.
Check Yourself
Answer each one before you look. The first two are from L19 and L19b.
Find the SD of 1, 2, 3, 3, 11 by hand. L19
- Mean: 20 ÷ 5 = 4.
- Minus the mean: −3, −2, −1, −1, 7.
- Squared: 9, 4, 1, 1, 49. Add: 64.
- Divide by n − 1 = 4: 16.
- Square root: √16 = 4.
What does line 2 print? L19b
1steps = pd.Series([1, 2, 6])
2print(steps.median())
In Sheets this is: =MEDIAN on three numbers. In English: 1. Make a column of the three numbers, and call it steps. 2. Print the median of steps.
In order, 1, 2, 6: the middle value is 2. pandas keeps a median as a decimal, so it prints 2.0.
Roll a fair six-sided die. What is the chance of an even number, as a percent?
6 equally likely ways; 2, 4 and 6 are even: 3 of 6, which is 1 in 2, or 50%.
Flip a fair coin twice. What is the chance of two heads in a row, as a percent?
12 × 12 = 14, which is 1 in 4, or 25%. The four equally likely ways: HH, HT, TH, TT.
A fair coin has landed tails 4 times in a row. What is the chance the next flip is heads?
The coin has no memory: every flip is 1 in 2. 1 in 16 is the chance of four tails in a row, worked out before any flip. 1 in 32 is five in a row.
You flip a coin 50 times and get 31 heads. What is the observed share of heads, as a percent?
31 ÷ 50 = 0.62, which is 62%.
Same coin: 31 heads in 50 flips. Is the coin unfair?
A fair coin rarely gives exactly half. In 50 flips the observed share wobbles by chance. To test the coin, flip it many more times and see where the share settles.
Three flips, done 400 times. About how many times would you expect three heads?
Three heads is 1 in 8. One eighth of 400: 400 ÷ 8 = 50.
Two lists of 30 flips. One is real; one was made up.
List 1: HHTTTHTHHHHHHTTHTTTHHTHTHHTTTH
List 2: HTHTTHTHHTHTHTTHTHTHHTHTTHTHTH
List 2’s longest streak is 2, and it switches letters 24 times in 29 chances. List 1 has a streak of 6 heads. Real flips of 30 usually have a longest streak of about 5, so List 1 looks real.
Project Checkpoint
Stuck on any of this? Ask Mr. Case in class or send him a message in Canvas. Name the example or the question number, and say which step lost you.