Learning Goals 3 min
By the end of this lesson you will be able to:
- Find pick random (1) to (10) in the green Operators palette and explain that it reports a random whole number in the inclusive range — both endpoints can come up.
- Plug pick random () to () into a position slot (go to x: () y: ()), an effect slot (set [color v] effect to ()), and a delay slot (wait () seconds) — the three places it earns its keep.
- Use negative ranges like pick random (-200) to (200) to scatter sprites anywhere on the Stage, and explain why this single block is the difference between a boring game and a game that surprises you every play-through.
Warm-Up — predict the chaos 8 min
Random doesn't mean "anything". It means "you can't predict which exact number, but you can predict the range". Look at each stack and write down what you'd see if you clicked the flag five times in a row.
Puzzle 1
when flag clicked
say (pick random (1) to (6)) for (2) seconds
Reveal puzzle 1
The cat says a different whole number from 1 to 6 each time — same as rolling a six-sided dice. Could be 3, then 6, then 1, then 1 again, then 4. The block treats both endpoints as possible: 1 and 6 can come up. "Pick random 1 to 6" really is the simplest possible dice in Scratch.
Puzzle 2
when flag clicked
forever
go to x: (pick random (-200) to (200)) y: (pick random (-150) to (150))
wait (0.5) seconds
end
Reveal puzzle 2
Every half-second the cat teleports to a completely random spot on the Stage. The x can be anywhere from −200 to +200; the y from −150 to +150 — basically the whole visible Stage. The cat looks like it's flickering around the screen. This is the scattering pattern — used for stars, dust, fireflies, exploding particles.
Puzzle 3
when flag clicked
forever
next costume
wait (pick random (1) to (3)) seconds
end
Reveal puzzle 3
The cat changes costume at irregular intervals — sometimes 1 second between, sometimes 2, sometimes 3. The animation feels less robotic because it's not on a perfect beat. This is a tiny trick used by every professional game animator: random delays make characters feel alive. A fixed 2-second wait looks like a machine. A random 1-to-3-second wait looks like a creature thinking.
New Concept — pick random () to () 15 min
Open the green Operators palette. Scroll past the four arithmetic blocks from last class. You'll see this:
pick random (1) to (10)
What it does
Every time the block is asked for an answer, it picks a brand-new random whole number between the left number and the right number — and both endpoints can come up. With (1) to (6), you might get any of 1, 2, 3, 4, 5, or 6. With (0) to (1), you get either 0 or 1 — a coin flip.
The two-rule shortcut
Two things to remember and you've learned the block:
- Inclusive on both ends.
(1) to (10)can return 1 or 10 (or anything in between). - Whole numbers by default. Unless one of the slots has a decimal in it (like
(1.0)), you get integers. If you want decimals — for smooth random speeds, say — type0.0in a slot.
Random positions — the scatter pattern
The Stage is 480 wide (x from −240 to +240) and 360 tall (y from −180 to +180). To scatter a sprite anywhere fully on-Stage with a bit of margin, use:
go to x: (pick random (-200) to (200)) y: (pick random (-150) to (150))
You'll see this exact block in every catch-the-falling-thing game, every starfield, every "make 50 clones and spread them out" project. The negatives matter — without them, sprites only appear on the right half.
Random colour effect
The colour effect goes from 0 to 200 and wraps around — 200 lands back on the original hue. So a random colour for any sprite is simply:
set [color v] effect to (pick random (0) to (200))
Random delays — the "feels alive" trick
Fixed delays feel mechanical. Random delays feel organic:
when flag clicked
forever
say [Cuit!] for (0.5) seconds
wait (pick random (1) to (4)) seconds
end
Random costume
If a sprite has, say, 5 costumes, you can show a random one:
switch costume to (pick random (1) to (5))
One block, four classic uses
The same pick random () to () shows up in position, colour, delay, and costume slots. That's why it's the most-used Operators block in published Scratch projects — it's reusable everywhere you have a number slot you want to be unpredictable.
Worked Example — making the Lucky Number Machine's dice roll 12 min
We upgrade the Lucky Number Machine from last lesson. The hard-coded die1 = 4 and die2 = 5 are replaced by real random rolls. Eight steps.
pick random rolls them fresh every flag-click. The cat (✏️ badge) runs the whole script.Step 1 — Open the Lucky Number Machine project
Open the same project from Lesson 2. The cat already has the set [die1 v] to (4) and set [die2 v] to (5) blocks at the top.
Step 2 — Replace the hard-coded values with pick random
Find the set [die1 v] to (4) block. Click on the 4 inside it and replace it by dragging pick random (1) to (6) into that slot. Do the same for set [die2 v] to (5) — replace the 5 with another fresh pick random (1) to (6).
Step 3 — The full assembled stack
when flag clicked
set [die1 v] to (pick random (1) to (6))
set [die2 v] to (pick random (1) to (6))
set [total v] to ((die1) + (die2))
say (join [Total: ] (total)) for (2) seconds
set [bonus v] to ((total) * (2))
say (join [Bonus: ] (bonus)) for (2) seconds
Step 4 — Click the flag five times
Watch the watcher widgets for die1 and die2. They should show different numbers each time. The total changes from 2 to 12. The bonus doubles each total. The machine is alive — you never know what you'll get.
Step 5 — Add a target number
Make a new variable target. Set it to a hard-coded value of 7 for now (the most likely total with two dice). Put the set at the top of the script so it resets each flag-click. The target will become random in a later lesson.
Step 6 — Announce how close the total is
After the bonus say, add one more say:
say (join [Target was ] (target)) for (2) seconds
Step 7 — Test it thoroughly
Click the flag ten times. Note the smallest total and largest total you see. What's the minimum possible? (1 + 1 = 2.) Maximum? (6 + 6 = 12.) Did you ever see 2 or 12? Probably not — those are the rarest rolls. Did you see totals near 7 most often? Good — that's real probability at work.
What you just built: the dice engine. The machine now rolls by itself. Every lesson from now on adds polish — mod and round tidy the scoring, abs measures distance from the target, floor gives a clean final score.
Try It Yourself — three random drills 15 min
Goal: Build a one-block dice roller that rolls a six-sided dice when you press the space key, and says the number for 1 second. Pressing space again rolls a new number.
when flag clicked
forever
if <key [space v] pressed?> then
say (pick random (1) to (6)) for (1) seconds
end
end
Think: Hold the space key and the cat will roll many times very fast. That's because the if-then is checked every frame, and every check re-runs pick random from scratch. To make it roll only once per press, you'd add a wait at the end of the if-then body. (Try it!)
Goal: Build the "falling kuih" spawn pattern from L02-37, but using pick random on both x (where it spawns) and the fall speed (so some kuih fall fast, some fall slow). Two pick-random blocks, one clone-based stack.
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
show
go to x: (pick random (-200) to (200)) y: (180)
glide (pick random (2) to (6)) secs to x: (x position) y: (-180)
delete this clone
Think: The glide-duration slot accepts a number, so you can plug a random in there. Slow-falling kuih are easy to catch; fast-falling ones are a panic. One block turned a boring constant-speed game into a varied-difficulty game.
Goal: Build a random walker — a cat that takes a step in a random direction every half-second. The direction should be one of 0°, 90°, 180°, 270° (up, right, down, left). After 20 steps, it should stop and say where it ended up.
when flag clicked
go to x: (0) y: (0)
repeat (20)
point in direction ((pick random (0) to (3)) * (90))
move (20) steps
wait (0.5) seconds
end
say (join [Ended at x: ] (x position)) for (3) seconds
Think: Look at the direction calculation. pick random (0) to (3) gives one of 0, 1, 2, 3. Multiply by 90 and you get one of 0, 90, 180, 270 — exactly the four cardinal directions. You used arithmetic from last class to constrain the random output to the four values you needed. This is how programmers compose blocks: pick-random doesn't give you "north / east / south / west" directly, but pick-random plus multiply gives you exactly that.
Mini-Challenge — the kuih that all fall the same way 5 min
"Daniel's kuih are boring"
Daniel built a falling-kuih clone script. He wants each new kuih to appear at a different random x — but every kuih in his game spawns at the same x as the first one. Read his stack and find the bug.
when flag clicked
hide
set [spawnX v] to (pick random (-200) to (200))
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
show
go to x: (spawnX) y: (180)
glide (3) secs to x: (spawnX) y: (-180)
delete this clone
Why are all the kuih falling in the same vertical line?
Reveal one valid solution
The bug is in line 3: set [spawnX v] to (pick random (-200) to (200)). That line runs once at flag-click and stores one random number in the variable. From then on, spawnX never changes — so every clone reads the same value out of the variable.
The fix is to move the random roll into the clone's own script, so each clone re-rolls when it starts:
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
show
set [spawnX v] to (pick random (-200) to (200))
go to x: (spawnX) y: (180)
glide (3) secs to x: (spawnX) y: (-180)
delete this clone
Now every clone, the instant it starts, sets spawnX to a fresh random number — and uses that same fresh value for both the spawn position and the fall destination (so each kuih falls straight down, but at its own random x). The rule: pick random is re-rolled every time the block runs. If you want one value reused, store it in a variable — but make sure the storing happens fresh for each clone, not once before any clones exist.
Recap 3 min
You met Scratch's most-used Operators block: pick random () to (). It reports a random whole number in an inclusive range, both endpoints included, with negatives allowed. You used it to make the Lucky Number Machine's dice roll themselves — every flag-click now gives a fresh pair of numbers from 1 to 6. The checkpoint: the dice now roll themselves. Next lesson, mod and round enter to make the scoring fair and tidy.
- Pick random () to ()
- The Operators block that reports a random whole number in an inclusive range. pick random (1) to (6) can return any of 1, 2, 3, 4, 5, or 6.
- Inclusive range
- A range where both endpoints are possible values.
(1) to (10)is inclusive — both 1 and 10 can come up. (Some other programming languages are exclusive on one end; Scratch isn't.) - Scatter pattern
- go to x: (pick random (-220) to (220)) y: (pick random (-160) to (160)) — the standard way to drop a sprite anywhere on the Stage. The negatives are the key.
- Re-rolling
- Every time a pick random block is read, it produces a new number. If you need the same number reused (e.g. spawn x = land x for a straight fall), store one roll in a variable and read the variable both times.
- Organic delay
- A wait (pick random () to ()) instead of a fixed wait. The irregular rhythm makes sprites feel alive instead of mechanical — a tiny detail with a big impact.
Homework 2 min
The Random Aquarium. Build a small Scratch project where 10 fish clones swim around the Stage at random.
- Open a fresh project. Delete the default cat. From the sprite library, pick Fish (any fish). Shrink to size 60.
- Hide the original. Use repeat (10) to spawn 10 clones.
- In the when I start as a clone script: show, go to a random x and random y, point in a random direction (use pick random (0) to (360)), and set the colour effect to a random number 0 to 200.
- Then a forever loop: move 2 steps, if touching the edge turn 180 degrees, and wait a random short time (wait (pick random (0.1) to (0.4)) seconds) so each fish moves at its own pace.
- Save as
HW-L3-03-Random-Aquarium.sb3.
Bring back next class:
- The
.sb3file. - Your answer to: "Run the project five times. Are any two runs identical? Why or why not?"
Heads up for next class: SCR-L03-04 meets two more Operators blocks that pair beautifully with random — round () (which kills the ugly 3.3333333 from L03-02's division) and () mod () (which finds the remainder, and is the secret behind "every 5th frame" patterns). Together they add an even/odd bonus to the Lucky Number Machine and tidy its score display.