Shuffle
RANDOMNESS — *a value you can't predict, picked fresh each time.* A random primitive produces an unpredictable result — a dice roll, a shuffled deck, a coin flip — so programs can surprise, vary, and stay fair. The same code can behave differently every run.
Press play to listen along. The line being read lights up as you go.
Show full transcript
Loading transcript…
In the corner of the CodeRealm workshop sat a covered cup, and it was, at that exact moment, doing something no one in the room could predict.
Loop had it on the bench in front of a class of squinting kids. It was a plain tumbler, lidded, and when Loop gave it a shake it made a dry, rattling music — a whole crowd of small numbered tokens knocking together in the dark. He held it up so everyone could hear it.
"This is Shuffle," Loop said. "Randomness. Watch closely, because I promise you I don't know what happens next either."
He tipped the cup. A single token slid out onto the bench and rolled to a stop. `4`.
"Four," Loop said, as if it were the most ordinary thing in the world. He scooped the token back in, capped the cup, shook it — that rattling music again — and drew once more. `1`. Then again, `6`. Then `1` a second time.
A kid in the front row leaned forward, frowning. "Do it once more."
Loop did. `3`. The kid sat back, a little defeated and a little delighted, because they had genuinely been trying to guess and there had been nothing to grab onto — no pattern, no rhythm, no next-in-line. Just a cup, a shake, and a fresh surprise every single time.
"That," Loop said, "is the whole job. When a program needs something you can't guess ahead of time — a dice roll, a random enemy, a shuffled quiz — you shake Shuffle and draw. A value nobody can see coming. Not the workshop, not you, not even me."
He set the cup down, and it sat there quietly with all its unknown tokens hidden inside, patient and full of surprises.
Loop noticed a kid at the back who wasn't laughing.
This kid had built a guessing game the week before, and it had gone wrong in the most boring way possible: every time you played it, the "secret" number was `7`. Always `7`. The first player found it in a run, told everyone, and after that the game was dead. No one could be surprised twice.
"It wasn't broken," the kid said, miserable. "It ran perfectly. It was just... the same. Every time. I made a game that couldn't surprise anybody."
Loop pulled the cup over and set it in front of them.
"You didn't need a bigger machine," Loop said. "You needed one that forgets. Watch." He shook the cup and drew — `5` — capped it, shook, drew again — `2`. "It doesn't remember the last draw. It has no favorite number. There's no seven waiting in there for you to trip over. Every draw is fresh."
The kid picked up the cup themselves. Shook it. Drew. `6`. Shook. Drew. `1`. And something loosened in their chest — the flat, stuck feeling of the same, the same, the same lifting off, replaced by the small rising fizz of I don't know what's next. They shook it a third time just to feel it again.
"See," Loop said gently. "That jumpy, don't-know feeling? That's the thing your game was missing. Not a bug. A surprise."
The kid brought their game back to the workshop the next morning and set the cup on the bench like it belonged there.
"I want to build it right this time," they said.
Loop didn't ask them to prove anything. He asked one question. "What does randomness give a program?"
The kid didn't answer with words. They wired Shuffle into the guess-a-number game so that each round, the workshop shook the cup and drew a fresh secret token from 1 to 9 — and then they made Loop play it. Loop guessed `4`. Wrong; it was `8`. They reset. Loop guessed `8` on purpose this time, grinning. Wrong again; it was `2`.
"You can't cheat it," the kid said, "because I don't even know what it'll draw. It picks fresh, every round, from the cup."
Loop played three more rounds and lost every guess, and by the end he was laughing.
"You belong here," he said, and set the cup back on the bench beside the kid's game like it had always lived there.
By afternoon the kid had a little crowd, and one of them wanted a game of their own.
"I need a dice," this new kid said. "For a board game. But I don't get it — how does a cup roll a six?"
The kid who now understood Shuffle pulled the cup over. "Watch. You put six tokens in — 1 to 6. Shake." Rattle. "Draw." `3`. "There's your roll. Want a coin flip instead? Two tokens — heads and tails. Want a random cheer? Fill a list with phrases, shuffle it, and draw one. Same idea every time: shake, draw, get a fresh one."
The new kid dropped in the six tokens and drew. `5`. Then `5` again.
"Wait — two fives! Is it stuck? Am I due for a low one now?"
The first kid shook their head, remembering exactly how Loop had said it. "That's the honest part. It doesn't keep score. Two fives in a row doesn't make the next draw any less likely to be a five. The cup has no memory and no fairness-debt — it isn't 'saving up' a low number for you. Every draw is fresh and completely on its own."
The new kid drew again, half-braced for a five, and got `1`, and the whole small crowd let out a whoa. They played on, and every roll landed like a tiny gift nobody could open early.
When the workshop emptied out, the first kid stayed behind with the cup, running their fixed game one more time, alone.
They'd wired it so it drew a fresh secret number and a random cheer and shuffled the questions into a new order. They ran it. `3`, a chirped Nice one!, the questions in one order. They ran it again. A different secret, a different cheer, a different order entirely. Same exact code. Brand-new run.
And then it happened: the game surprised them. They'd built every line of it, and it still did something they hadn't seen coming — and they laughed out loud in the empty room, alone, at their own machine.
The cup sat on the bench, lid on, tokens hidden, quietly ready for the next shake. It wasn't thinking. It wasn't feeling lucky. It was just a cup of tokens you shook and drew from, fresh every time. But the kid, watching a thing they'd made astonish them run after run, felt the brightest, jumpiest kind of happy — the delight of not knowing what comes next, of building something alive enough to still amaze the one who built it.
They shook the cup one more time before they left, just to feel that small rising fizz, and drew a `6`, and grinned all the way out the door.
The CodeRealm ensemble
Shuffle is part of CodeRealm's distributed-narrative cast. Each character embodies a different curricular primitive; together they teach the full subject.
-
Stash
Variable / storage — the labeled box that holds a value until you call for it
-
Fork
Conditional / branching — chooses a path based on what's true right now
-
Trek
Loop / iteration — keeps going around until the work is done
-
Module
Function / encapsulation — does one job well and can be called anywhere
-
Glitch
Debugging / inspection — finds bugs gently, never shaming; 'there's always a reason'
-
Order
Sequence / syntax — reminds you that order matters in code
-
Row
A list: many values lined up in a numbered row, so you can grab item number three instantly or walk through them one by one.
-
Port
Input and output: the doorway that brings information in from the world (a key press, a sensor) and sends results back out.
-
Ping
An event: a waiting bell that does nothing until its trigger happens, then runs its code the instant it is struck.