- Learnt28 min · why random repeats, Fisher-Yates, shuffled answers
- Basic25 min · the shuffled quiz
- Challenge 1–327 min · five from eight, missed ones again, the broken swap
- Extrabonus · the unfair shuffle
Review — Quiz Engine I 5 min
- A question is one line: question, three answers, the right answer, cut apart at every |.
splitwalked the line withletter (i) ofandlength of.- A new question was one new line in the bank, not new blocks.
- Typing 4 read part 5 — the answer itself — until we checked for 1, 2 or 3.
Quick-fire
Your quiz asks the same five questions in the same order every time. Why is that a problem?
Reveal the answer
Players learn the order, not the facts — “the third one is honey”. And the right answer is always in the same place, so they learn that too.
Today’s Topic 3 min
- Why picking at random repeats questions
- The Fisher-Yates shuffle: swap from the bottom up
- Shuffling the answers, and finding the right one again with
(item # of (thing) in [options v])
Learning outcome
By the end of this lesson you will be able to:
- Explain why random picks are not a shuffle.
- Shuffle any list fairly, with a temp variable for the swap.
- Test whether a shuffle is fair by running it a thousand times.
Learnt — a shuffle you can trust 28 min
1 · Random is not the same as shuffled
The easy idea: pick a random question each time. With 8 questions and 8 picks, watch how many different ones you actually get.
delete all of [asked v]
repeat (8)
add (pick random (1) to (length of [bank v])) to [asked v]
end
How to use this: Click the green flag to start it. Nothing else to press. Then press it again — the count changes every time.
One game proves nothing, so the next demo plays a thousand of them in one go.
How to use this: Click the green flag to start it. Nothing else to press — a thousand games take only a moment.
2 · Deleting as you go eats the bank
Quiz Master’s fix was to delete a question once it was asked. No repeats — but look at the bank afterwards.
How to use this: Click the green flag to start it. Nothing else to press — watch the bank list empty.
3 · Fisher-Yates: swap from the bottom up
A shuffle keeps every question and only changes the order. Start at the last place. Swap it with a random place from 1 up to itself — it may even swap with itself. That place is now done. Move up one, and do it again.
define shuffle
set [n v] to (length of [bank v])
repeat ((length of [bank v]) - (1))
set [j v] to (pick random (1) to (n))
set [temp v] to (item (n) of [bank v])
replace item (n) of [bank v] with (item (j) of [bank v])
replace item (j) of [bank v] with (temp)
change [n v] by (-1)
end
temp holds one item while the other moves.How to use this: Click the green flag to start it. Nothing else to press — the numbers 1 to 8 are shuffled one swap at a time.
4 · Is it fair?
Fair means every order is as likely as every other. One test: shuffle 1 to 8 a thousand times and count how often each number lands in first place. A fair share is 1000 ÷ 8 = 125.
How to use this: Click the green flag to start it. Nothing else to press — watch the firsts list.
5 · Shuffle the answers too
Copy parts 2, 3 and 4 into an options list and shuffle it with the same blocks. Then find where the right answer went.
delete all of [options v]
add (item (2) of [parts v]) to [options v]
add (item (3) of [parts v]) to [options v]
add (item (4) of [parts v]) to [options v]
shuffle answers :: custom
set [right v] to (item # of (item (5) of [parts v]) in [options v])
shuffle answers is a copy of shuffle that works on options. A My Block cannot take a list as an input, so each list needs its own copy.How to use this: Click the green flag to start it. Nothing else to press — the same question is shown three times.
Basic — the shuffled quiz 25 min
Open your quiz from Quiz Engine I. Put eight lines in the bank.
Step 1 — the shuffles
- Make the variables
n,j,tempandright, and a listoptions. - Make the
shuffleMy Block from Learnt section 3. - Right-click its define hat, duplicate it, and make
shuffle answers— the same blocks withoptionsin every list dropdown.
Step 2 — the quiz loop
- Call
shuffle :: customunder the green flag, before the loop. - In the loop, after the split, add the blocks from Learnt section 5.
- Ask with the options in place of parts 2–4, and check against
right.
ask (join (item (1) of [parts v]) (join [ 1) ] (join (item (1) of [options v]) (join [ 2) ] (join (item (2) of [options v]) (join [ 3) ] (item (3) of [options v]))))))) and wait
set [pick v] to (answer)
repeat until <<(pick) > (0)> and <(pick) < (4)>>
ask [Type 1, 2 or 3.] and wait
set [pick v] to (answer)
end
if <(pick) = (right)> then
change [score v] by (1)
say [Right!] for (0.6) seconds
else
say [No!] for (0.6) seconds
end
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter for each question. Play twice and compare.
A question comes up twice.
The shuffle is inside the loop. Move shuffle :: custom above repeat (length of [bank v]).
The right answer is marked wrong.
The check still looks at part 5. Now that the answers are shuffled, compare pick with right.
right is 0.
item # of did not find the answer in options. The fifth part of that line is spelt differently from the answer it should match — check for a stray space.
Five from eight
A bank can be bigger than a game.
- Keep eight lines in the bank, but a game asks only five.
- No question appears twice in a game.
- A new game can pick a different five.
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter for each question. Watch the asked list.
Teacher reveal
Shuffle, then change the loop from repeat (length of [bank v]) to repeat (5). The first five of a shuffled bank are five random questions with no repeats.
Missed ones come back
Practise what you got wrong.
- A wrong answer adds that question’s line to a
missedlist. - After the round, the missed ones are asked again.
- Still wrong? It goes to the back of the line. The game ends when
missedis empty.
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter. Get a couple wrong on purpose.
Teacher reveal
In the round, a wrong answer runs add (item (q) of [bank v]) to [missed v]. Then:
repeat until <(length of [missed v]) = (0)>
split (item (1) of [missed v]) :: custom
// shuffle the answers and ask, as in the round
if <(pick) = (right)> then
delete (1) of [missed v]
else
add (item (1) of [missed v]) to [missed v]
delete (1) of [missed v]
end
end
“Add it to the end, then delete it from the front” sends a question to the back of the line.
The broken swap
Someone “tidied up” the shuffle by deleting the temp variable. Run it, then explain what went wrong.
- Shuffle 1 to 8 with the broken swap below.
- Count how many different numbers are left.
- Explain, in one sentence, where the missing numbers went.
replace item (n) of [deck v] with (item (j) of [deck v])
replace item (j) of [deck v] with (item (n) of [deck v])
How to use this: Click the green flag to start it. Nothing else to press — watch the numbers as each broken swap happens.
Teacher reveal
The first replace overwrites place n with place j’s number. By the second replace, place n already holds j’s number, so j gets its own number back. n’s old number is gone for ever. temp saves it before it is overwritten.
The unfair shuffle
A shuffle that looks fine can still be unfair. “Swap every place with any place” sounds just as random as Fisher-Yates. Test it.
- Shuffle the three letters A, B, C six thousand times with each method.
- Count how often each of the six orders comes out: ABC, ACB, BAC, BCA, CAB, CBA.
- A fair shuffle gives each about 1000.
How to use this: Click the green flag to start it. Nothing else to press — 12,000 shuffles take a few seconds.
Summary 5 min
- Random picks repeat. With 8 questions, a game of 8 random picks shows only about 5.3 of them.
- Deleting as you go stops repeats but empties the bank.
- Fisher-Yates swaps from the bottom up, each place with one at or above it. It keeps every item.
- A swap needs
temp. Shuffle the answers too, and find the right one withitem # of.
- Shuffle
- Changing the order of a list without losing or copying anything in it.
- Swap
- Two items trading places. It needs somewhere to hold one of them for a moment.
- Fair
- Every possible order is just as likely as every other.
Hand in
Save your shuffled quiz. Next lesson it leaves the one-player world: two to four players, one laptop, passed round the room.