Sayed's Blog

Game Randomness And Bags

Posted February 22nd, 2020

Randomness is important to get right in games.

Not enough and a game becomes too predictable. Getting a good score becomes a game of memory rather than skill. A game quickly becomes old because there is not much variety.

Too much and a game becomes too unpredictable. It can also make the game unplayable, or too easy. If a game is too random you can't guarantee that it is theoretically beatable and you also can't guarantee that it will be difficult. Randomness can completely invalidate the skill of the player.

Another issue with too much randomness is that it actually seems less random. Human brains are pattern seeking, and true randomness means that things that don't look random can happen. A truly random coin can return 3 heads in a row, but it won't seem random to a human.

True randomness can create the impression of unfairness, and imagined randomness can seem more fair. If the world "knows" that it gave you a band hand in a card game two times in a row, and then compensates by giving a good hand, that seems fair, and people often expect it.

These are all important problems in a game. Generally, a game is expected to be:

  • Novel
  • Playable
  • Difficult, requiring skill
  • "Fair" - not rigged
  • Satisfying

And all of these are affected by the amount, and type of randomness in a game.

In game design, there are many different ways that randomness can be categorised and used in a game. Game randomness is often split into "input" randomness and "output" randomness. Input randomness is randomness that occurs before the player makes a decision, such as in level generation. Output randomness is randomness that occurs after the player makes a decision. One example of this is random amounts of damage when a player attacks.

In this post, however, I will only be discussing one technique to control randomness in games. This technique is called random bag generation. I will also only be discussing it in the context of input randomness.

Random Bags

Random bag generation is a technique used to make random spawning more enjoyable.

Let's say you can spawn multiple types of objects, for example, a vampire or a mummy or a skeleton in a Halloween game.

Instead of choosing a monster to spawn randomly, you put all of the options in a "bag". When you spawn a monster, you randomly remove one from the bag.

Next time you spawn, the monster you just spawned is no longer in the bag, so you won't have the same monster multiple times in a row. When the bag is empty, you refill the bag. If the last monster in a bag is a mummy, and the first monster in the next bag is also a mummy, then you have a mummy twice in a row, but that is acceptable.

Case Studies


Initially, Tetris would randomly choose a tetromino at the moment of spawning. Not from a bag, but from a random number generator. This led to the problem of having the same tetromino multiple times in a row. To fix this, a later version would check if the spawned tetromino was the same as the previous one, and choose one more time.

Later versions extended the history to look at the last few tetromino.

Finally, they used a bag implementation. All 7 tetromino were put in a bag and shuffled. A tetromino would be chosen at random from the bag, and, when the bag was empty, it was refilled.

My Own Game

In my game Deluge, I ran into problems with deciding where to spawn the obstacles.

The spawning algorithm I was using meant that people were able to get very high scores without moving because they can stay at the sides of the world and obstacles were not spawning often enough on the sides.

How did I solve it? I looped through every horizontal "region" in the game world and added a random position within that region to a bag. I also made sure that there was a higher frequency of positions at the sides. And then, when spawning an obstacle, I would choose a random position from the bag. This made the game much more fun because you had to be alert to win. I could guarantee that the game won't be easy a large percentage of the time.

Closing Thoughts

Hopefully this technique not only showed how to use bags for random spawning, but also showed how randomness and game design are used together.

Next time you play a game, think about how randomness is used, especially in spawning. Try to guess the techniques used by the developers.