BrightPath
Back to Course
Year 8 Coding & Computational Thinking Maths Link: Probability

Simulations and Probability

Use code to simulate experiments and explore how experimental probability approaches theoretical probability!

Maths Connection: Experimental vs Theoretical Probability

Theoretical Probability

What should happen based on maths. A fair coin has P(Heads) = 1/2 = 50%.

Experimental Probability

What actually happens when you try it. Flip a coin 10 times and you might get 6 heads (60%).

The Law of Large Numbers says: the more times you repeat an experiment, the closer your experimental probability gets to the theoretical probability. Code lets us test this with thousands of trials instantly!

Coin Flip Simulator

Theoretical probability: Heads = 50%, Tails = 50%. Let's see what happens when we simulate it!

function flipCoin() {
    return Math.random() < 0.5 ? "Heads" : "Tails";
}

function simulateFlips(numFlips) {
    let heads = 0, tails = 0;
    for (let i = 0; i < numFlips; i++) {
        if (flipCoin() === "Heads") heads++;
        else tails++;
    }
    return { heads, tails };
}

Dice Roll Simulator

Theoretical probability for each face of a fair die: 1/6 = 16.67%. Let's test it!

function rollDice() {
    return Math.floor(Math.random() * 6) + 1;
}

function simulateRolls(numRolls) {
    let counts = [0, 0, 0, 0, 0, 0];
    for (let i = 0; i < numRolls; i++) {
        counts[rollDice() - 1]++;
    }
    return counts;
}

Key Vocabulary

Simulation

Using code to model a real-world process and run it many times to observe patterns.

Random Number

A number generated unpredictably. Math.random() gives a random decimal between 0 and 1.

Theoretical Probability

The probability calculated using maths. E.g., P(Heads) = 1/2 for a fair coin.

Experimental Probability

The probability found by actually doing the experiment: successes / total trials.

Law of Large Numbers

More trials = experimental probability gets closer to theoretical probability.

Trial

One run of an experiment. Flipping a coin once = one trial.

Worked Examples

1

Coin flip: 10 flips gave 7 heads. What is the experimental probability?

Formula: Experimental probability = successes / total trials

Calculation: P(Heads) = 7 / 10 = 0.70 or 70%

Note: The theoretical probability is 50%. With only 10 flips, 70% is quite possible due to randomness. With more flips, it would get closer to 50%.

2

What does Math.floor(Math.random() * 6) + 1 generate?

Step 1: Math.random() generates a decimal from 0 to 0.999...

Step 2: Multiply by 6: gives 0 to 5.999...

Step 3: Math.floor() rounds down: gives 0, 1, 2, 3, 4, or 5

Step 4: Add 1: gives 1, 2, 3, 4, 5, or 6 -- a dice roll!

3

Why do 10 flips give different results each time, but 100,000 flips are always close to 50%?

Answer: This is the Law of Large Numbers.

With few trials (10), random variation has a big impact -- one extra head changes the percentage by 10%.

With many trials (100,000), random variations cancel each other out, and the result converges toward the theoretical probability of 50%.

Knowledge Check

Select the correct answer for each question.

Question 1

You simulate 200 coin flips and get 108 heads. What is the experimental probability of heads?

Question 2

The theoretical probability of rolling a 6 on a fair die is:

Question 3

What does the Law of Large Numbers tell us?

Question 4

What values can Math.floor(Math.random() * 10) produce?

Question 5

Why are simulations useful in science and engineering?

Key Concepts Summary