- Learnt28 min · pick random, tidy ranges, stock
- Basic24 min · the morning routine
- Challenge 1–327 min · selling out, one in five, mystery
- Extrabonus · restock overnight
Review — where we got to 5 min
([floor v] of ())throws away everything after the dot.(() mod ())is the remainder — what is left over.- The ladder takes the biggest coin first, then hands the leftover down.
Quick-fire
What is 47 mod 10? And what is floor of 47 ÷ 10?
Reveal the answer
7 and 4. Four 10c coins fit into 47c, and 7c is left over. The pair always answer those two questions together.
Today’s Topic 3 min
(pick random (1) to (6))— a number nobody can predict- Choosing the range so the numbers are useful, not just random
- Random stock, and a shelf that can be empty
- Chance: making something happen one time in five
Learning outcome
By the end of this lesson you will be able to:
- Use pick random to give a variable a fresh value each run.
- Turn a random number into a tidy price or a sensible stock count.
- Make something happen with a known chance.
Learnt — a machine that surprises you 28 min
1 · Why bother?
Your machine is correct and it is boring. The muffin costs 45c today, tomorrow, and for ever. Play it twice and you already know everything.
Real machines are not like that:
- Prices change. There are offers on some days and not others.
- Shelves run out, and get refilled overnight.
- Sometimes there is a promotion, and you cannot tell in advance.
2 · The block
Operators has one block for this, and it is the whole lesson:
(pick random (1) to (6))
- Drag
(pick random (1) to (10))out of Operators. - Click it on its own in the palette. A number pops up.
- Click it again. And again. Watch the number change.
- Change the two slots to 1 and 6, and click it six more times.
3 · Taming the range
A price of 37c is legal but awkward — your change ladder has no 1c or 2c coins. You want prices that are multiples of 5, so pick the number of fives and multiply:
set [price v] to (pick random (30) to (60))
set [price v] to ((pick random (6) to (12)) * (5))
Six to twelve fives is 30c to 60c, in steps of 5. The random block chooses; the multiply keeps it tidy.
How to use this: Click the green flag and watch four prices go by. Then press it again — you will get four different ones.
4 · Random stock, and an empty shelf
Give each snack its own count. A snack that rolls a 0 has sold out already and should not be on the shelf at all:
when flag clicked
show
set [muffins v] to (pick random (0) to (3))
if <(muffins) = (0)> then
hide
say [Sold out.] for (2) seconds
else
say (join (muffins) [ left.]) for (2) seconds
end
How to use this: Click the green flag. Press it four or five times — sooner or later a shelf comes up empty.
Basic — the morning routine 24 min
Open your machine from last lesson. Everything fixed becomes a roll.
Step 1 — today’s price
- Find the block that sets the price and delete the number in it.
- Drop in
((pick random (6) to (12)) * (5)). - Show the price box on the Stage.
- Press the flag five times. Write down the five prices.
Step 2 — today’s stock
- Make a variable called
muffins. - Under the flag, add
set [muffins v] to (pick random (1) to (3)). - Show that box too.
Step 3 — tell the customer
- After the coin questions, say what today’s price is with a
join. - Tell them which key to press.
Step 4 — sell one
- On the key script, compare the money with today’s price using
if / else. - If there is not enough, say how much more is needed (Lesson 3-1, Challenge 2).
- If there is, take one off the shelf with
change [muffins v] by (-1), glide to the tray and say the change.
when flag clicked
go to x: (-124) y: (76)
show
set [muffins v] to (pick random (1) to (3))
set [price v] to ((pick random (6) to (12)) * (5))
ask [How many 20c coins?] and wait
set [twenties v] to (answer)
ask [How many 50c coins?] and wait
set [fifties v] to (answer)
set [money v] to (((twenties) * (20)) + ((fifties) * (50)))
say (join [Muffins today: ] (join (price) [c. Press A.])) for (3) seconds
How to use this: Click the green flag, then click the stage once. Type 3, then 2, then press A. Press the flag again for a new price.
The price changes while I am reading it.
The pick random is inside a forever loop, or inside the say block itself. Roll it once into a variable, then use the variable.
The price is different in two places in the same run.
The same thing: two separate pick random blocks give two separate numbers. One roll, one variable, used everywhere.
My snack never comes back after it sells out once.
show is missing under the green flag.
Sometimes the change is an awkward number like 7c.
The price is not a multiple of 5. Use ((pick random (6) to (12)) * (5)) rather than (pick random (30) to (60)).
Selling out
A shelf with three muffins on it should be able to run out.
- Every sale takes one off the count.
- When the count hits 0 the sprite hides and the slot says “Sold out!”.
- Pressing the key again after that does nothing.
How to use this: Click the green flag and watch it sell the shelf out by itself. Press the flag again for a different starting stock.
Teacher note
The classic bug is a count that goes negative — the pupil hides the sprite but never guards the key script. Ask them what the stock box says after four presses on a shelf of three.
One in five
Add a promotion: now and then, the snack is free.
- Each go has a one-in-five chance of being free.
- When it is free, the machine says so and takes no money.
- A counter shows how many free ones you have had.
How to use this: Click the green flag and watch eight goes roll past. Press it again — the number of free ones changes.
Teacher reveal — chance in two blocks
set [roll v] to (pick random (1) to (5))
if <(roll) = (1)> then
say [Lucky! This one is free.] for (1) seconds
else
say (join [Rolled ] (join (roll) [. Pay up.])) for (1) seconds
end
One in five means five possible numbers and one of them wins. It does not mean one free in every five goes — run it three times and somebody will get none and somebody will get three. That is worth saying out loud; it is the same reason a dice can give you three sixes in a row.
The mystery slot
One slot where you do not know what you are buying until it drops.
- A sprite with three costumes — one for each snack.
- A random number picks the costume.
- The price is rolled too, so even the cost is a surprise.
How to use this: Click the green flag and watch the slot change its mind five times before it drops.
Teacher reveal — a number can choose a costume
set [which v] to (pick random (1) to (3))
switch costume to (which)
switch costume to () takes a costume name from its dropdown, but it will also take a number dropped into the slot — costume 1, costume 2, costume 3. That is the trick, and Level 2’s Monster Maker used the same one. Pupils who cannot drop the variable in are usually trying to type into the dropdown instead of dragging onto it.
Restock overnight
Selling out ends the game. A real machine gets refilled — so add a night, without pressing the green flag.
- Pressing R refills every shelf with a fresh random count.
- A counter says which morning it is.
- The prices are rolled again too. It is a new day.
How to use this: Click the green flag, then click the stage once and press R a few times. Watch the stock boxes and the morning counter.
Teacher reveal
when [r v] key pressed
change [nights v] by (1)
set [muffins v] to (pick random (1) to (4))
set [donuts v] to (pick random (1) to (4))
show
say (join [Morning ] (join (nights) [. Restocked.])) for (1.5) seconds
Note the show — a shelf that sold out is hidden, and restocking it without showing it again gives you invisible muffins. Pupils will find that one on their own within a minute, which is the best way to find it.
Summary 5 min
(pick random (1) to (6))gives a whole number, both ends included.- Roll once into a variable. Rolling twice gives you two different numbers.
- Multiply a small roll to keep it tidy:
((pick random (6) to (12)) * (5)). - A chance of one in five is five numbers and one of them winning.
- One in five does not mean once in every five. Randomness is lumpy.
- Random
- A value you cannot predict before it happens.
- Range
- The lowest and highest a random number is allowed to be.
- Chance
- How likely something is — one in five, one in a hundred.
- Stock
- How many of something a shelf has left.
Hand in
Save your machine. Next lesson is the last of the arc: a discount that lands on a half cent, and a till that has to balance at the end of the day.