# The psychology of randomness

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:

• When asked to pick a random number between 1 and 100, most people will choose a number that is odd, often prime, and approximately 1/3 or 2/3 of the way between the lower and upper limits. For some reason we think these values are "more random" than other numbers. Try it sometime! It is amazing how many people will choose 67, while nobody ever goes for 50.

• My mother, a smart lady who is generally good at math, is absolutely convinced that if you roll an (unloaded) dice 10 times and get a 6 each time, the odds of getting another 6 on your 11th attempt must be vanishingly small. Indeed, 11 in a row is unlikely, but her intuition simply cannot accept that, having already rolled 10, the odds of an 11th remain 1/6.

• The shared birthday party trick and Monty Hall problem are famous examples of how unintuitive even simple probability questions can be.

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.

1. Venatu says:

Why not Wizard of Oz and Dark Side of the Moon? Im intrigued

Great article as always

2. Pete says:

"My mother, a smart lady who is generally good at math, is absolutely convinced that if you roll an (unloaded) dice 10 times and get a 6 each time, the odds of getting another 6 on your 11th attempt must be vanishingly small. Indeed, 11 in a row is unlikely, but her intuition simply cannot accept that, having already rolled 10, the odds of an 11th remain 1/6."

It’s what I call: "Subjective probability vs. Objective probability".

She knows the odds are 1/6 (objectively), however, she changes the game from getting 1 to 6 when rolling the dice to "It can/cannot happen" and assigns a lower probability to "It can happen" based on her passed experiences (her life’s experience serves as a MonteCarlo simulation).

The Monty Hall problem is very interesting (if I remember correctly if is mentioned in the film "21"). I believe the solution may vary depending on many factors: the precise rules of the game (say, the number of total doors, whether you are offered to switch doors after you first pick, if the host is obliged or not to open one door before offering you to switch), whether you count on information of passed shows, whether you want to use that info in case you have it, and last but not least your own perception of risk.

3. Roel says:

"Our brains are tremendously good at spotting subtle patterns in seemingly random noise"

That is also how some people manage to memorize quite a many digits of pi: the digits are random for sure, but patterns do appear all the time.

4. ShawnHargreaves says:

> Why not Wizard of Oz and Dark Side of the Moon? Im intrigued

http://en.wikipedia.org/wiki/Dark_Side_of_the_Rainbow

5. PiRX says:

This, especially part about song shuffling reminded me of article about card shuffling. I don’t remember it in details, bet main idea was instead of generating random numbers which represent cards, you should pick one card from stack at random then "compact" stack and repeat. I think same approach should be used for playlists.

6. wow, I’m so glad I now have a place to point people when this topic comes up ðŸ˜› I have a funny story, I take my two kids and niece to school.  They always fight for who gets to sit in the front seat.  I (stupidly) said that we’d roll a dice every morning and whomever got the highest roll would get the spot.  Well, my poor daughter had the unluckiest rolls ever for weeks upon weeks.  She was convinced that this was the most unfair method of choosing ever.  We reverted to round robin and they are all much happier ðŸ˜›

7. tony says:

what about Uniform Poisson-Disk sampling for a nice even scatter?

8. Zenfar says:

Wrote some C# code and wouldn’t you know it the first number it gave me was 67.  Also got two engineers at work upset of the Monty Hall problem, of course they weren’t software guys…

9. Racky says:

Beware of using time as a factor in your random calculations. My 3D starfield was nice and evenly distributed on my A500, but fully of patterning on the A1200… Ahh, them were the days!

10. Erzengel says:

Randomness can be a little crazy in games. Maybe it’s a part of the psychology of randomness, but I’ve noticed in certain games that are highly reliant on randomness that it never seems to actually be random. It seems to favor my enemy every single time. I’ve had times when the game puts me at 65 to 20 odds of winning, and I consistently lose. I’ve had times when my odds are 3 to 2.5 of winning, and I lose, so I load the last save and wait a turn. Now my odds are 2.5 to 3, the same likelihood as the AI had of winning last turn (when they won) and I still lose. I know this is technically valid, but as a player–a player on the lowest difficulty setting–this is incredibly frustrating and makes me wish that I could punch the AI in the nose for using loaded dice.

As a Quality Assurance Technician, this also makes me wonder… is this a bug? How can I tell if it’s a bug? Maybe there’s a problem in the code that, even though my likelihood of winning is 10 times higher than the AI’s, the AI somehow is 50 times more likely?

This makes me hate randomness, at least when the numbers are hidden.

11. Olle Håkansson says:

Erzengel: A common way to deal with that is to have randomness without replacement. I.e, for example, exchange a 6-sided dice with a deck of cards with the numbers 1-6 on each card. You don’t replace any cards until you’ve drawn your last one. While the precise mechanics of the game may still make it very frustrating to "always" get the bad card at the wrong situation, in my experience it’s less annoying in the way you mentioned. Here’s an article going into this with far more depth than I’m willing to go for now ðŸ™‚

http://www.gamedev.net/reference/articles/article2206.asp

A good example (not sure if it’s actually true though ðŸ™‚ is Warcraft 3 where you get critical hits by randomness without replacement.

12. tam says:

See http://en.wikipedia.org/wiki/Perlin_noise for a way to integrate some randomness in our distribution of objects on a 2d grid.

About the random feature in code, I thought it was based mainly on the coding language, the compiler and the processor the compiled software was running on. But having never tested this theory I can not vouch for it, however seducing it is.

13. Tom says:

The given probability analysis of the Mony Hall problem is misleading because it assumes you’re always going to switch doors. There’s a more complete analysis diagram on the discussion page demonstrating that your chances of winning either a goat or a car are the same.  Wait, can you win a goat?

> That is also how some people manage to memorize quite a many digits of pi: the digits are random for sure, but patterns do appear all the time.

What???  pi is 100-percent deterministic.  It might *look* random and exhibit some perceived properties of randomness, but it’s as far from random as you can possibly get.  This is akin to saying all the zeroes in 1.000000000 are random but just happen to look the same.

Erzengel:  This is likely because the people responsible for calculating the player’s odds (i.e., the same people who programmed the game) assume the player follows exactly the same rules and decision-making logic as the game itself, which is absolutely not true.

On the occasions I find myself being consistently whooped by game AI, I revise my strategy to counteract the hard-coded rules by which the AI plays, and I generally win.  Even when there is randomness in the decision-making, knowing the rules vastly increases your chances of winning.  Human factors such as anger and impatience will hinder your odds.  I suppose there’s an elaborate psychology between players and game AI.

14. Tom says:

Wait, my mistake.  I interpreted the second diagram wrong.  So, yeah, switch doors.

15. Keith Peters says:

One of my favorite examples that demonstrated our psychology of random is the flipping coin experiment. Take two friends and put them in different rooms with a piece of paper and pencil. Tell one of them to flip a coin 100 times and note down heads or tails on each flip. Tell the other one to write down heads or tails randomly on their paper.

When they are done, they give you the papers and you have to decide which is which. How do you do it? Easy, the list with the real coin flips will almost certainly have at least one chain of 5 heads or 5 tails in a row. The person faking random would never think to do such a thing, as it's obviously "not random"

16. Jared says:

Do you happen to have a reference for this intriguing claim: When asked to pick a random number between 1 and 100, most people will choose a number that is odd, often prime, and approximately 1/3 or 2/3 of the way between the lower and upper limits. For some reason we think these values are "more random" than other numbers. Try it sometime! It is amazing how many people will choose 67, while nobody ever goes for 50?

Jared

17. Wayne says:

Are there studies supporting this observation?
“When asked to pick a random number between 1 and 100, most people will choose a number that is odd, often prime, and approximately 1/3 or 2/3 of the way between the lower and upper limits. For some reason we think these values are “more random” than other numbers. Try it sometime! It is amazing how many people will choose 67, while nobody ever goes for 50.”