Tenhou Deck/Wall-Shuffling Algorithm

Crates.io docs.rs

[Tenhou] is an online Japanese Riichi Mahjong game. As of 2022-10-02, Tenhou has published their algorithm for shuffling the deck / wall of tiles, alongside with validation data for a particular game seed, including intermediate results.

This crate re-implements the published algorithm for reconstructing the full shuffled wall of any game from its RNG seed.

This crate is no_std.

MT19937 RNG

This crate bundles an implementation of [MT19937], copy-pasted from https://crates.io/crates/mt19937. This is mostly for no_std compatibility.

The Algorithm

Assuming u32 is little-endian (LSByte first) in byte buffers.

Each game begins by seeding an MT19937 RNG for the game. This seed can be retrieved as a base-64 encoding of the seed array.

Each round in the game requires a shuffle. We first derive the randomness as follows:

Then, we shuffle the wall with rnd: