# TGM randomizer

Most games in Arika's Tetris The Grand Master series randomize the order of tetrominoes using an algorithm that makes successive identical tetrominoes less common. It involves keeping a history of the four most recent tetrominoes and trying to choose a random tetromino not in the history. It "rolls the dice" a given number of times and takes the first tetromino that doesn't match any in the history. TGM1 uses 4 tries; subsequent games using the TGM randomizer use 6 tries.

The history is not a unique list. If the randomizer fails to generate a unique tetromino, which happens about 3.5 percent of the time in a 6-try system, then two or more of one tetromino may occupy elements of the history.

A few additional behaviors exist in the beginning of the game.

• The game never deals an S, Z or O as the first piece.
• The state of the history is initialized to a fixed state:
• In TGM1, the history begins filled with 4 Z pieces.
• In TGM2, the history begins with a Z,S,Z,S sequence.

## Pseudocode

Function tgmRandomize(history as list of 4 pieces by reference, numTries as integer by value) as piece:
For try = 1 to numTries:
candidatePiece = random element of set {I, J, L, O, S, T, Z}
If candidatePiece is not in history:
Exit loop
Move all tetrominoes in history back by one position
Put candidatePiece at front of history
Return candidatePiece