Piece randomizer
The rule a falling-block game uses to choose the next piece.
A piece randomizer chooses the next piece in a falling-block game. Its rule sets how often a player waits for a needed piece, and how long the longest waits get. The games read here span four ways: no memory at all, in Tengen’s Tetris; rerolls against repeats, in Tetris for the NES and for the Game Boy; a sequence dealt before play, in Dr. Mario; and a shuffled bag, as in games that follow the Tetris Guideline.
- MemorylessTetris (NES, Tengen) (1989). Three bits of a 16-bit generator, rerolled only on 0; both players get the same pieces.1,2
- RerollTetris (Game Boy) (1989). Up to three reads of the hardware divider, tested with an OR of type ids.3,4,5Tetris (NES) (1989). One reroll against repeats, biased by the previous piece.6,7,8,9,10,11,12
- Dealt in advanceDr. Mario (NES) (1990). 128 pills dealt at the start of each level, shared by both players and repeated in a loop.13,14
#How it works
A memoryless randomizer picks each of the seven pieces with probability 1/7, so a piece repeats the one before 1 time in 7 and nothing bounds a drought. Two ways to shape that:
- History and reroll. Remember the last piece (or several), and pick again if the new one matches. Repeats get rarer but stay possible, and droughts stay unbounded.
- Bag. Shuffle a set of pieces and deal it out before shuffling another. Every piece comes once per bag, which bounds droughts.
reroll (one piece of history): bag of 7:
p = random piece if bag empty: bag = shuffle(all 7)
if p == previous: p = random piece p = bag.pop()
previous = p
#Implementations
#Tetris (NES) (1989)
NES Tetris draws on a 16-bit linear feedback shift register that repeats after 32,767 steps6,6. The game steps it every frame, so a player’s timing changes the next piece7.
The pick takes 3 bits plus a pick counter. A result of 7, or a repeat, triggers one reroll with no further check8. The reroll adds the previous piece’s orientation id instead of its index, which doubles one of its eight results. After an O or an S, the doubled result is that same piece9.
Simulated over the generator’s whole cycle, a piece repeats with probability 1/32 after a T, J, Z, L, or I and 1/16 after an O or S10. Over a long game 4.03%15 of pieces repeat. The shares run from 13.89% (L, I) to 14.68% (S)11. Measured in the running game, 4.46% of pieces repeated the one before12,12.
#Tetris (Game Boy) (1989)
The Game Boy version has no generator: it reads the hardware divider register and reduces it modulo 7. It reads up to three times, rerolling when the pick, the next piece, and the spawning piece OR together to the spawning piece’s id3. That test rerolls three of a kind, but also any pick whose bits lie inside the spawning piece’s.
With independent reads the routine would deal 10.86% (L) to 16.22% (O), with 10.71% repeats4,4. The running game deals 10.84% L, 16.74% O, and 11.55% repeats in 9,000 pieces5,5. Its repeats are more than two and a half times as common as the NES game’s12.
#Tetris (NES, Tengen) (1989)
Tengen’s Tetris keeps no history. It takes the low bits of its generator, retries only on 0, and never checks the piece before1. Both players’ generators start from one seed, so a two-player game deals both the same sequence2.
#Dr. Mario (1990)
Dr. Mario deals 128 pills from 9 color pairs13 at the start of each level, shared by both players and read in a loop. Each pill steps from the previous one by a random amount that wraps unevenly, so the same pill follows itself 1 time in 814, more often than a uniform pick would give.
#The Random Generator (7-bag)
Games that follow the Tetris Guideline use the Random Generator, which deals all seven pieces in a random order and then another seven16. No more than 12 pieces17 can pass between two I pieces17.
#History
The Tetris Wiki traces the Random Generator to The New Tetris, which drew from a bag of 63 pieces, nine of each18.
Games
| Game | Year | How | Note |
|---|---|---|---|
| Tetris (NES, Tengen) | 1989 | Memoryless | three bits of a 16-bit generator, rerolled only on 0; both players get the same pieces |
| Tetris (Game Boy) | 1989 | Reroll | up to three reads of the hardware divider, tested with an OR of type ids |
| Tetris (NES) | 1989 | Reroll | one reroll against repeats, biased by the previous piece |
| Dr. Mario (NES) | 1990 | Dealt in advance | 128 pills dealt at the start of each level, shared by both players and repeated in a loop |
Every claim about this mechanic, by game (19)
References
- ^ ab
getNextTetrominosteps the player's 16-bit generator five times and keeps the low three bits, trying again only when they are 0; the result, 1 to 7, is the next piece, with no check against the piece before.Codezohassadar/TengenTetrisDisasmHow we know
lda player1TetrominoNext,x … sta player1TetrominoCurrent,x … adc #player1RNGSeed … jsr genNextPseudoRandom5x … and #$07 … beq @loopUntilNotZero … sta player1TetrominoNext,x
main.asm#L3693-L3705 · upstream ↗ 3805genNextPseudoRandom5x: ⋮ 3819genNextPseudoRandom:
main.asm lines 3805–3832 at df38b4f · upstream ↗ Checked against the evidence by its author, an AI model, 2026-10-21.
Checked by a second AI model, 2026-10-21.
- ^ abAt the start of a game both players' generators are loaded with the same seed, and only
getNextTetrominosteps them, so in a two-player game both players get the same sequence of pieces. The shared seed itself is stepped on every pass of the main loop before the game.Codezohassadar/TengenTetrisDisasmHow we know
lda rngSeed … sta player1RNGSeed … sta player2RNGSeed … lda rngSeed+1 … sta player1RNGSeed+1 … sta player2RNGSeed+1
main.asm#L3319-L3326 · upstream ↗ 48mainLoop:main.asm lines 48–50 at df38b4f · upstream ↗ Checked against the evidence by its author, an AI model, 2026-10-21.
Checked by a second AI model, 2026-10-21.
- ^ abTo pick a piece, the game reads the hardware divider
DIV, reduces it modulo 7, and checks the result against the piece now spawning and the one after it; it reads again when the three type ids OR together to the spawning piece's, and takes the third read regardless.Codealexsteb/tetris_disassembly · “Tetris (World) (Rev 1)”How we know
5414 ld a, [rNEXT_BLOCK_TYPE] 5415 ld [hl], a 5416 and $fc 5417 ld c, a
main.asm lines 5414–5417 at b4bbceb · upstream ↗ 5444 ld h, $03 5445l_2043: 5446 ldh a, [rDIV] 5447 ld b, a 5448l_2046: 5449 xor a 5450l_2047: 5451 dec b 5452 jr z, l_2054 5453 inc a 5454 inc a 5455 inc a ⋮
main.asm lines 5444–5479 at b4bbceb · upstream ↗ In the cartridge (“Tetris (World) (Rev 1)”): the spawn routine, func_2007. We don't reproduce the bytes.
Checked against the evidence by its author, an AI model, 2026-10-20.
Checked by a second AI model, 2026-10-21.
- ^ abcIf its three reads were independent and uniform, the routine would deal L 10.86% of the time and O 16.22%, with the others between, and 10.71% of pieces would repeat the one before.Verifiedalexsteb/tetris_disassembly
How we know
5446 ldh a, [rDIV] 5447 ld b, a ⋮ 5457 cp $1c ⋮ 5465 dec h 5466 jr z, l_2062 5467 or d 5468 or c 5469 and $fc 5470 cp c 5471 jr z, l_2043
main.asm lines 5444–5479 at b4bbceb · upstream ↗ Measured by a script in MesenCE 2.2.1 (build 4b8a196), on . It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^ abcIn 9,000 pieces dealt over 474 games on the original Game Boy, L came 10.84% of the time and O 16.74%, and 11.55% of pieces repeated the one before.Verified“Tetris (World) (Rev 1)” · alexsteb/tetris_disassembly
How we know
In the cartridge (“Tetris (World) (Rev 1)”): the spawn routine, func_2007. We don't reproduce the bytes.
5446 ldh a, [rDIV] 5447 ld b, a ⋮ 5457 cp $1c ⋮ 5465 dec h 5466 jr z, l_2062 5467 or d 5468 or c 5469 and $fc 5470 cp c 5471 jr z, l_2043
main.asm lines 5444–5479 at b4bbceb · upstream ↗ Measured by a script. It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^ abcThe random number generator is a 16-bit linear feedback shift register at
$0017; each step shifts it right and feeds bit 1 XOR bit 9 into the top bit, and from its seed,$8988, it repeats after 32,767 steps.VerifiedCelestialAmber/TetrisNESDisasm · Birken, "Applying AI to Nintendo Tetris" · “Tetris (USA)”How we know
5468generateNextPseudorandomNumber: 5469 lda tmp1,x 5470 and #$02 5471 sta tmp1 5472 lda tmp2,x 5473 and #$02 5474 eor tmp1
main.asm lines 5468–5483 at 94581c0 · upstream ↗ 347@continueWarmBootInit: 348 ldx #$89 349 stx rng_seed 350 dex 351 stx rng_seed+1
main.asm lines 347–351 at 94581c0 · upstream ↗ This PRNG repeatedly and deterministically generates 32,767 distinct values, each cycle starting with the original seed.
Picking Tetriminos · archived copy ↗ In the cartridge (“Tetris (USA)”): generateNextPseudorandomNumber, CPU $AB47. We don't reproduce the bytes.
Measured by a script in MesenCE 2.2.1 (build 4b8a196), on Tetris (USA) (SHA-1
3026d28b63d9…). It fails if the result disagrees with the claim.The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-20.
Checked by a second AI model, 2026-10-21.
- ^ abThe NMI handler steps the generator every frame, so the time a player takes to place a piece changes which piece comes next.CodeCelestialAmber/TetrisNESDisasm · Birken, "Applying AI to Nintendo Tetris"
How we know
270.if NWC <> 1 271 ldx #rng_seed 272 ldy #$02 273 jsr generateNextPseudorandomNumber 274.endif
main.asm lines 270–274 at 94581c0 · upstream ↗ Meaning, the number of frames that it takes the player to position a piece actually affects which piece comes up next.
Picking Tetriminos · archived copy ↗ Checked against the evidence by its author, an AI model, 2026-10-20.
Checked by a second AI model, 2026-10-21.
- ^ ab
pickRandomTetriminoadds a pick counter to the generator's high byte and keeps the low 3 bits; a result of 7, or the same piece as last time, makes it step the generator and take the high byte's low 3 bits plus the previous piece's orientation id, modulo 7, with no further check.CodeCelestialAmber/TetrisNESDisasm · Birken, "Applying AI to Nintendo Tetris"How we know
2974 inc spawnCount 2975 lda rng_seed 2976 clc 2977 adc spawnCount 2978 and #$07 2979 cmp #$07 2980 beq @invalidIndex 2981 tax 2982 lda spawnTable,x 2983 cmp spawnID 2984 bne useNewSpawnID 2985@invalidIndex: ⋮
main.asm lines 2974–3002 at 94581c0 · upstream ↗ 3013spawnTable: 3014 .byte tDown, jDown, zHoriz, oFixed, sHoriz, lDown, iHoriz
main.asm lines 3013–3014 at 94581c0 · upstream ↗ Otherwise, the next pseudorandom number is generated and a mask is applied to retain the lower 3 bits of the high byte before adding the prior spawn orientation ID.
Picking Tetriminos · archived copy ↗ Checked against the evidence by its author, an AI model, 2026-10-20.
Checked by a second AI model, 2026-10-21.
- ^ abThe reroll adds the previous piece's orientation id, not its index, so one of its eight results is doubled; after an O (
$0A) or an S ($0B) the doubled result is the same piece again.CodeCelestialAmber/TetrisNESDisasm · Birken, "Applying AI to Nintendo Tetris"How we know
123tDown = $02 ⋮ 128jDown = $07 129zHoriz = $08 ⋮ 131oFixed = $0A 132sHoriz = $0B ⋮ 135lDown = $0E ⋮ 139iHoriz = $12
constants.asm lines 121–139 at 94581c0 · upstream ↗ Using the prior spawn orientation ID in the sum as opposed to the prior Tetrimino type introduces a bias because the spawn orientation ID values are not evenly distributed.
Picking Tetriminos · archived copy ↗ Checked against the evidence by its author, an AI model, 2026-10-20.
Checked by a second AI model, 2026-10-21.
- ^ abRun over every state of the generator and every pick count, the routine repeats a T, J, Z, L, or I with probability 1/32 and an O or S with 1/16, so 4.0% of the pieces in a long game repeat the one before.VerifiedCelestialAmber/TetrisNESDisasm
How we know
2974 inc spawnCount 2975 lda rng_seed 2976 clc 2977 adc spawnCount 2978 and #$07 2979 cmp #$07 2980 beq @invalidIndex 2981 tax 2982 lda spawnTable,x 2983 cmp spawnID 2984 bne useNewSpawnID 2985@invalidIndex: ⋮
main.asm lines 2974–3002 at 94581c0 · upstream ↗ Measured by a script in MesenCE 2.2.1 (build 4b8a196), on . It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^ abIn a long game the routine deals T and S most often (14.67% and 14.68%) and L and I least (13.89% each); J, Z, and O come 14.29–14.30% of the time.VerifiedCelestialAmber/TetrisNESDisasm · Birken, "Applying AI to Nintendo Tetris"
How we know
2974 inc spawnCount 2975 lda rng_seed 2976 clc 2977 adc spawnCount 2978 and #$07 2979 cmp #$07 2980 beq @invalidIndex 2981 tax 2982 lda spawnTable,x 2983 cmp spawnID 2984 bne useNewSpawnID 2985@invalidIndex: ⋮
main.asm lines 2974–3002 at 94581c0 · upstream ↗ T and S appear more frequently and L and I less frequently.
Picking Tetriminos · archived copy ↗ Measured by a script in MesenCE 2.2.1 (build 4b8a196), on . It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^ abcdIn 4,800 pieces dealt by the running game, 4.46% repeated the piece before, against 4.03% simulated and 14.3% for a uniform pick; 6.29% of O and S pieces repeated (simulated 6.25%) and 3.76% of the others (3.13%).Verified“Tetris (USA)” · CelestialAmber/TetrisNESDisasm
How we know
In the cartridge (“Tetris (USA)”): pickRandomTetrimino, CPU $9907; generateNextPseudorandomNumber, CPU $AB47. We don't reproduce the bytes.
2974 inc spawnCount 2975 lda rng_seed 2976 clc 2977 adc spawnCount 2978 and #$07 2979 cmp #$07 2980 beq @invalidIndex 2981 tax 2982 lda spawnTable,x 2983 cmp spawnID 2984 bne useNewSpawnID 2985@invalidIndex: ⋮
main.asm lines 2974–3002 at 94581c0 · upstream ↗ Measured by a script. It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^ abAt the start of each level
generatePillsReservedeals 128 pills, shared by both players. Each pill is one of 9 color pairs, found by adding a random 0 to 15 to the previous pill's index and subtracting 9 until it fits; the game reads the reserve in a loop, so the same 128 pills come round again.CodeNostaljipi/dr-mario-disassemblyHow we know
170 lda #pillsReserveSize ;Equals to the number of pills to generate ⋮ 180 dex 181 lda rng0 ;Max the rng value at 15 182 and #$0F ⋮ 184 adc pillId ⋮ 187 cmp #colorCombination_right - colorCombination_left 188 bcc @storePill ;If within the array size (9), is a valid pill index ⋮ 191 sbc #colorCombination_right - colorCombination_left ⋮ 194 sta pillsReserve,X ;Starts from the end of the array
prg/drmario_prg_level_init.asm lines 169–194 at ecb9899 · upstream ↗ 124 jsr generatePillsReserve ;Same reserve used by both playersprg/drmario_prg_level_init.asm line 124 at ecb9899 · upstream ↗ 464 ldx currentP_pillsCounter ;If not demo, then use the player's pill reserve 465 lda pillsReserve,X ⋮ 472 inc currentP_pillsCounter ;Increase the pills counter ⋮ 474 and #pillsReserveSize-1 ;Wraparound over $7F because we have a maximum of 128 pills stored
prg/drmario_prg_game_logic.asm lines 464–475 at ecb9899 · upstream ↗ 824colorCombination_left: 825.db $00,$00,$00,$01,$01,$01,$02,$02 826.db $02 827colorCombination_right: 828.db $00,$01,$02,$00,$01,$02,$00,$01 829.db $02
data/drmario_data_game.asm lines 824–829 at ecb9899 · upstream ↗ Checked against the evidence by its author, an AI model, 2026-10-21.
Checked by a second AI model, 2026-10-21.
- ^ abBecause the 16 random values wrap at 9, steps of 0 to 6 from the previous pill's index each come from two values and steps of 7 and 8 from one, so the same pill follows itself with chance 2/16, where a uniform pick would give 1/9.CodeNostaljipi/dr-mario-disassembly
How we know
182 and #$0F 183 clc ;Add to index of previous pill (probably as a means to improve random) 184 adc pillId ⋮ 187 cmp #colorCombination_right - colorCombination_left ⋮ 191 sbc #colorCombination_right - colorCombination_left 192 jmp @checkIfValidIndex
prg/drmario_prg_level_init.asm lines 182–192 at ecb9899 · upstream ↗ Checked against the evidence by its author, an AI model, 2026-10-21.
Checked by a second AI model, 2026-10-21.
- ^Over a long game, 4.03% of pieces repeat the one before, against 14.3% (1 in 7) for a uniform, memoryless pick.VerifiedCelestialAmber/TetrisNESDisasm
How we know
2974 inc spawnCount 2975 lda rng_seed 2976 clc 2977 adc spawnCount 2978 and #$07 2979 cmp #$07 2980 beq @invalidIndex 2981 tax 2982 lda spawnTable,x 2983 cmp spawnID 2984 bne useNewSpawnID 2985@invalidIndex: ⋮
main.asm lines 2974–3002 at 94581c0 · upstream ↗ Measured by a script in MesenCE 2.2.1 (build 4b8a196), on . It fails if the result disagrees with the claim.
The numbers come from the run; its author, an AI model, checked the script measures what the claim says, 2026-10-25.
Checked by a second AI model, 2026-10-21.
Checked by a second AI model, 2026-10-25.
- ^The Random Generator, or 7-bag, used by games that follow the Tetris Guideline, deals all seven pieces in a random order, then another seven.WikiTetris Wiki, “Random Generator”
How we know
The Random Generator generates a sequence of all seven tetrominoes (I, J, L, O, S, T, Z) in a random order, as if the pieces were drawn from a bag.
lead · archived copy ↗ The quote matches our archived copy.
- ^ abA 7-bag never goes more than 12 pieces between two I pieces.WikiTetris Wiki, “Random Generator”
How we know
The Random Generator can produce a run of no more than 12 tetrominoes between one I piece and the next
lead · archived copy ↗ The quote matches our archived copy.
- ^The Tetris Wiki traces the Random Generator to The New Tetris, whose version drew from a bag of 63 pieces, nine of each.WikiTetris Wiki, “Random Generator”
How we know
The Random Generator was originally developed for The New Tetris. This early version of the algorithm used a bag size of 63—nine of each piece
lead · archived copy ↗ The quote matches our archived copy.
Sources
Code
- (CelestialAmber), Amber Brault and contributors. 2024. “TetrisNESDisasm: a disassembly of Tetris (NES).” Commit 94581c0. CelestialAmber/TetrisNESDisasm @ 94581c0 · licence, all uses
- 1989. “Tetris (USA).” · licence, all uses
- alexsteb. 2021. “tetris_disassembly: a disassembly of Tetris (Game Boy).” Commit b4bbceb. alexsteb/tetris_disassembly @ b4bbceb · licence, all uses
- 1989. “Tetris (World) (Rev 1).” · licence, all uses
- zohassadar. 2024. “Tengen NES Tetris Disassembly.” Commit df38b4f. zohassadar/TengenTetrisDisasm @ df38b4f · licence, all uses
- Nostaljipi. 2021. “Dr. Mario (NES) Disassembly.” Commit ecb9899. Nostaljipi/dr-mario-disassembly @ ecb9899 · licence, all uses
Secondary
- Birken, Michael. 2014. “Applying Artificial Intelligence to Nintendo Tetris.” original · archived · licence, all uses
- Tetris Wiki contributors. 2025. “Random Generator.” Tetris Wiki. original · archived · licence, all uses