MyLeoNes™

Randomness in programs — Computing, 11–13

Randomness gives programs choices that are hard to predict, useful for games, models, and fair selection.

The big idea

Randomness means that a program cannot tell in advance exactly which result will come next. A computer often creates this effect with a rule that produces a long, mixed-looking sequence of numbers. The program can then use one number to choose, shuffle, or vary something.

The problem it solves

A game with the same enemy, card order, or maze every time soon becomes easy and dull. A program also needs a fair way to choose one item instead of always choosing the first. Randomness adds variety and helps a program make selections without a person directing every one.

A worked example

To choose one of six faces on a die, use a random number from 1 to 6. Step 1: the program produces 4. Step 2: it looks up face 4 in its list. Step 3: it draws that face on screen. Run it again and you might get 2 or 6; over many fair runs, each face should appear about one-sixth of the time.

A tempting mistake

It is reasonable to expect every short run to look balanced: three heads and three tails in six coin flips. Chance does not promise that. Six heads can happen, although it is unusual; fairness describes the long-run chances, not a pattern that must appear in every small group.

Where you meet it

Randomness appears in shuffled playlists, game maps, raffle draws, and simulations of weather or traffic. It can make a game feel fresh, but a designer still chooses the chances: a rare treasure might have a 1% chance, while a common one has 30%. Random does not mean careless.

Keep exploring

Other languages

Loading MyLeoNes™…