# Puyo Puyo Tsu/Upcoming Pair Randomizer

This page describes how Puyo Puyo Tsu picks random puyo pairs. Standard battle rules are assumed.

## Contents

## Pair pools and buffers

The game keeps 3 main pools or "buffers" that store the puyos you'll get at various steps:

- there's a buffer for the currently falling pair, for each player;
- there's a buffer for the upcoming 3 pieces, for each player;
- there a big buffer that holds the randomized pairs for your game, in their order of appearance, that is shared among all players.

Pairs are passed along these buffers as they are picked from the later one to make their way to the first one.

At the beginning of a battle, the game will generate the big buffer holding all the pairs you'll ever be able to get during that game. This is a pool shared by both players, but depends on the chosen difficulty. Thus, the game actually generates 3 pair pools:

- Pool #1 at 0xFFAD00, holding pairs chosen among a 3-color subset only;
- Pool #2 at 0xFFAE00, holding pairs chosen among a 4-color subset only;
- Pool #3 at 0xFFAF00, holding pairs chosen among a 5-color subset only.

Each pool holds 256 puyos, making 128 pairs. The game will pick a pair from the relevant pool sequentially and loop if it ever reaches the end of the pool.

## Pair-pool random prefill algorithm

The pool generation works as follows:

- the full color-set of the game is shuffled in RAM at 0xFFA4E2, on 8 bytes. Initial order is: 0, 1, 3, 5, 4, 6 and 5 random permutations occur. For the sake of the example, we will consider the initial order in the next items;
- each pool is initialized with the colors taken from that set, from the beginning up to the desired number of different colors, and loops through. With our color-set, that means pool #1 gets 0-1-3-0-1-3-..., pool #2 gets 0-1-3-5-0-1-3-5-... and pool #3 gets 0-1-3-5-4-0-1-3-5-4-... as I said, the true order has been shuffled;
- each pool is shuffled by performing 256 pseudo-random permutations (swapping 2 puyos). Two puyos get chosen to be swapped: one is picked sequentially, beginning from the last one (number 255) all the way up to puyo number #0 in the pool. The second puyo is chosen by randomly picking an index. How so? Well, the game uses the previously explained random number generator, and only keeps the lowest byte from the 32-bit value. That makes an 8-bit value, that will necessarily be between 0 and 255. A good random index for choosing a random puyo as a swapping candidate;
- after shuffling the 3 pair pools, the first two pairs of pool #2 and #3 are overwritten by the first two pairs of pool #1. This ensures you'll only get 3 different colors in your first 2 pairs;
- lastly, the next two pairs of pool #3 are overwritten by the next two pairs of pool #2, effectively limiting the start of this pool to 4 colors.

The game then picks the pairs from those pools, sequentially, as it keeps a counter of how many puyos a player got so far. The counter is on 8 bits, so it will naturally loop after reaching 255.

## Game routines analysis

Here's the full analysis of the subroutine, with side-by-side comparison of the Genesis and Arcade versions:

Here's the color-set shuffling subroutine:

## Notable facts

### Loop

The game will loop through available pairs. 256 puyos in the randomized pool make 128 pairs, which amounts to about 3.55 full boards before a player will loop through pairs.

### First pairs of the game

One might notice that the first three pairs you get are not the first 3 of the generated pair pool. Actually, they are in reversed order. The first pair dealt by the game is the third one of the pool, while the third piece is the first in the pool. This is how the game picks the first three pairs only. However, it loops in the correct order of appearance.

### Uniform distribution

In a 4-color game, the pair pool initially gets exactly 64 puyos of each of those 4 colors. The margin of error amounts to the only 2 pairs which are overwritten, but the buffer is mostly uniform.

This means that, over the course of a battle, if one were to use 128 pairs, he would get exactly the same amount of puyos of each color.

### Randomization Biases

The permutation algorithm may shift more puyos of one specific color towards the beginning (or the end) of a pool, effectively reducing the odds of getting that color greatly either at the end or the beginning of a battle.

Consequently, it means that if a player gets lots of unicolor pairs and a few other of the same color, he will have quickly depleted the pool from that color and is not likely to get more any time soon.

Actually, the Puyo Puyo Tsu Random Number Generator suffers from strong biases and will pick series of numbers resulting in very low short-term variance that may pack same-color puyos close to each other.