The psychology of randomness

Originally posted to Shawn Hargreaves Blog on MSDN, Thursday, December 17, 2009

Due to some quirk of evolution, human beings are remarkably good at intuitively approximating solutions to complex calculus problems, but appallingly bad at estimating probability.

Pretty much anyone is able to catch a ball, or judge when it is safe to pull out into traffic. These tasks require complex predictions at least up to the second derivative (involving position, velocity, and acceleration), yet our subconscious can solve them in seconds.

Our intuitions are not so smart when dealing with probability:

This matters to game programmers, because we often use random numbers to control our game simulations. If I had a penny for every time I heard someone say "I think the random number generator must be broken, because the numbers I get back aren't very random", well, that would be a lot of pennies.

I ran this test program:

    Random random = new Random();

    for (int i = 0; i < 10; i++)
    {
        Trace.WriteLine(random.Next(100));
    }

Results:

    50
    76
    23
    1
    17
    16
    19    // Huh? Three similar numbers in a row
    8
    90
    90    // Yowzer! Same value twice in a row can't be right?

But these numbers are perfectly random. In fact, when you choose ten random numbers, there will often be strings of repeated or similar values. One of the most unlikely outcomes would be if the results were evenly distributed without any clustering! When we hear "random", we tend to think "evenly distributed, with a slight random perturbation over the top". These are not the same thing.

Our brains are tremendously good at spotting subtle patterns in seemingly random noise. Perhaps too good. We are so focused on trying to identify a pattern that our subconscious fixates on even the slightest deviation from an even distribution. A truly random series of numbers will never be evenly spaced, but when you combine a talent for identifying patterns with a poor understanding of probability, it's no wonder our poor little brains get confused :-)

Try this experiment some time: pick any movie other than Wizard of Oz, any music other than Dark Side of the Moon (preferably things you know well), and play them at the same time. Isn't it amazing how well they fit together? This occurs because our pattern detection skills focus on the handful of places where things do randomly happen to line up, and we are too bad at probability to contrast this with the much larger number of things that don't line up at all.

As game developers, we often use random number generators as an approximation for things that are not truly random at all, but which are too complex for us to properly evaluate.  We could simulate vegetation growth patterns, soil erosion, light and shade, the impacts of logging and lightning fires and deer grazing and the wind shadow of that distant mountain range. Nah. Let's just randomly scatter our tree sprites over the hillside...

The trick to getting good results from random numbers is to think about what characteristics we would like the resulting data to have, and understand that a series of truly independent random numbers is rarely the best distribution.

When planting trees, rather than just repeating a random position selection, we could cover our terrain with a regular grid. Perhaps we could perturb the grid on a coarse level, making some areas have smaller and denser cells than others. We can then plant one tree per cell, including a small random offset to hide the repeating pattern, and perhaps randomly skipping some cells while planting more than one tree in others. The resulting distribution will be far from random in the mathematical sense, but will look more random to the average human being, and that's what counts, right?

Likewise, a random song shuffle algorithm should avoid repeating the same song too close together. Sure, playing it twice in a row is a valid random result, but probably not what the user wanted!

Randomness in games is usually an approximation for something more complex, and not independent samples like you get when tossing a coin, rolling a dice, or calling Random.Next(). By understanding this, we can process our random numbers to produce results that are less random, yet more convincing and artistically pleasing.

Blog index   -   Back to my homepage