MyLeoNes™

Randomness in programs — Computing, 7–10

Programs can create results that feel unpredictable, such as a shuffled card or a surprise in a game. Learning how this works helps you use chance without confusing it with a guarantee.

A programmed chance

Randomness means that you cannot know the next result just by looking at the earlier ones. A program can choose one item from a list, such as a colour or a number, to make a game less predictable. The computer follows a method for making the choice; it is not thinking or wishing.

Why programs use chance

If a game always behaved in exactly the same way, you could memorise every surprise and soon become bored. Chance solves this problem by varying choices, while keeping the rules fair enough to play. Computers are actually very regular, so they use special calculations to imitate random results.

Choosing a number

Suppose a game needs one number from 1 to 6. First, the program makes the possible list: 1, 2, 3, 4, 5, 6. Next, it uses its chance method to select one entry, say 4. The game then shows four stars or gives four points. On another turn, the answer may be different.

Random is not equal turns

It is reasonable to think that if a 6 has not appeared for five turns, it must appear next. But each fair turn starts again with the same six possibilities, so 6 is not owed to you. Over many turns, results may become fairly balanced; over a short run, repeats and gaps are normal.

Chance outside games

Random choices help a music app shuffle songs, a website show different examples and a scientist select a fair sample of people or objects. In each case, the program needs clear limits: which items can be chosen and how often should matter. When you see a surprise, ask what rules control it.

Keep exploring

Other languages

Loading MyLeoNes™…