Post

Randomness in game

How do computers handle the generation of random numbers? Let me explain with the help of Professor GPT.

Computers generate random numbers using algorithms called pseudo-random number generators (PRNGs). These algorithms start with a seed value, typically based on some external input such as the current time, and then use mathematical operations to produce a sequence of numbers that appear random. However, since the process is deterministic, meaning that given the same seed value, the sequence of numbers will be identical, these numbers are not truly random. Instead, they are called pseudo-random because they mimic the behavior of true randomness for many practical purposes.

During the development of the Tetris game, I faced a similar issue: when dropping the block rapidly, identical next blocks were generated, and the pattern became recognizable. This made me realize that randomness should not be solely based on time.


Figure 1. Tetris game with an inappropriate random algorithm

Here is the code with the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

  enum BlockType
  {
    T,
    I,
    S,
    Z,
    L,
    J,
    SQ,
    BlockTypeCount
  };
  
void TetrisBlock::createBlock(){	
    
		// Seed using the current time 
    std::srand(static_cast<unsigned int>(std::time(nullptr)));
    // Random BlockType using function rand()
    blockType = static_cast<BlockType>(std::rand() % BlockTypeCount);
    
    // more 
}



This is how I fixed.

1
2
3
4
5
6
7
8
9
10
11
12
void TetrisBlock::createBlock(){	
   
		// 1. Seed the Mersenne Twister pseudo-random number generator (mt19937) with a random value from std::random_device.
		std::mt19937 mt(std::random_device{}());
		// 2. Create a uniform distribution for generating integers between T and BlockTypeCount - 1, inclusive.
		std::uniform_int_distribution<int> dist(T, BlockTypeCount - 1);
		// 3. Generate a random integer using the distribution and the mt19937 generator, then cast it to a BlockType enum value.
		blockType = static_cast<BlockType>(dist(mt));
    
    // more 
} 

Figure 1. Tetris game with the Mersenne Twister pseudo-random number generator

Reference

Wikipedia-Random number generation
Wikipedia-Mersenne Twister pseudo-random number generator

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.