Learning Pseudorandom Numbers with Transformers: Permuted Congruential Generators, Curricula, and Interpretability
Tao Tao, Maissam Barkeshli
TL;DR
This work probes whether Transformer models can learn the hidden recurrence and permutation structures of PCGs, a nontrivial class of PRNGs, by evaluating in-context prediction across multiple PCG variants and moduli up to $m=2^{22}$. The authors show that Transformers achieve strong in-context learning, even when outputs are truncated, and identify scaling laws, such as the required context growing approximately as $\tfrac{1}{2}\sqrt{m}$, with curriculum learning and pretrained initialization crucial for large moduli. They also demonstrate that curriculum strategies, data mixing, and transfer from smaller-modulus models enable stable scaling under fixed compute budgets, and reveal interpretable, rotation-invariant token embeddings alongside generator-separation dynamics in intermediate layers. Together, the results provide insights into how transformers encode modular arithmetic tasks, with implications for cryptography-inspired benchmarks, curriculum design, and model interpretability in structured arithmetic settings.
Abstract
We study the ability of Transformer models to learn sequences generated by Permuted Congruential Generators (PCGs), a widely used family of pseudo-random number generators (PRNGs). PCGs introduce substantial additional difficulty over linear congruential generators (LCGs) by applying a series of bit-wise shifts, XORs, rotations and truncations to the hidden state. We show that Transformers can nevertheless successfully perform in-context prediction on unseen sequences from diverse PCG variants, in tasks that are beyond published classical attacks. In our experiments we scale moduli up to $2^{22}$ using up to $50$ million model parameters and datasets with up to $5$ billion tokens. Surprisingly, we find even when the output is truncated to a single bit, it can be reliably predicted by the model. When multiple distinct PRNGs are presented together during training, the model can jointly learn them, identifying structures from different permutations. We demonstrate a scaling law with modulus $m$: the number of in-context sequence elements required for near-perfect prediction grows as $\sqrt{m}$. For larger moduli, optimization enters extended stagnation phases; in our experiments, learning moduli $m \geq 2^{20}$ requires incorporating training data from smaller moduli, demonstrating a critical necessity for curriculum learning. Finally, we analyze embedding layers and uncover a novel clustering phenomenon: the model spontaneously groups the integer inputs into bitwise rotationally-invariant clusters, revealing how representations can transfer from smaller to larger moduli.
