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.

Pseudocode

 * Function tgmRandomize(history as list of 4 pieces) 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