Introduction
Pseudo-Random Number Generators (PRNGs) are deterministic algorithms designed to produce sequences that mimic randomness. While fundamental to modern computing and cryptography, their deterministic nature implies the existence of hidden mathematical structures. This research investigates whether Transformer models can "see through" this illusion of randomness by reverse-engineering the underlying algorithms of Linear Congruential Generators (LCGs). By treating PRNGs as a controlled testbed, the study explores the limits of in-context learning and the emergent mechanisms Transformers develop to solve obfuscated sequential tasks.
Methodology
The study focuses on Linear Congruential Generators (LCGs), defined by the recurrence: $$x_{t+1} = (a x_t + c) \pmod m$$
Transformers were trained to autoregressively predict the next number in these sequences under two paradigms:
- Fixed Modulus (FM): The modulus $m$ is constant across training, requiring the model to generalize to unseen multipliers ($a$) and increments ($c$).
- Unseen Modulus (UM): The model must generalize to entirely unseen $m, a,$ and $c$ at test time.
To handle large numbers (up to $m=2^{32}$), the authors utilized a base-$b$ tokenization scheme with "Abacus" positional embeddings, which encode both sequence position and digit significance. Mechanistic interpretability techniques, such as PCA and activation patching, were employed to decode the model's internal logic.
Mechanistic Insights
The investigation revealed several sophisticated emergent algorithms:
The Look-Back and Copy Mechanism
Models often exploit a mathematical shortcut: for an LCG, skipping $2^k$ steps causes the lower $k$ bits to become constant or highly periodic. Transformers learn to "look back" in the context window to copy these repeating patterns, simplifying the prediction of higher-order bits.
Residue Number Systems and Factorization
PCA on embedding layers showed that models naturally group numbers by their remainders modulo small primes (e.g., 2, 3, 7). This suggests the emergent use of a Residue Number System (RNS), where the model decomposes a complex composite modulus into simpler, independent prime-based sub-problems. Specific attention heads were found to specialize in these individual prime factors.
Grokking and Generalization
A notable finding was the occurrence of "grokking," where the model suddenly achieves high test accuracy long after training performance has stabilized. This transition typically occurs when the model moves from memorizing repeating patterns within the context window to learning the underlying algebraic rule.
Key Results
The models demonstrated remarkable scalability, solving the FM task for moduli up to $2^{32}$ and the UM task up to $2^{16}$. A critical finding was the "depth-to-complexity" requirement: the Unseen Modulus task requires a minimum of three Transformer layers to execute the necessary "estimate-factorize-predict" pipeline.
Furthermore, the data efficiency of these models is high; the number of in-context examples required to achieve accuracy scales sublinearly with the modulus (approximately $m^{1/4}$).
Conclusion and Future Directions
This work proves that Transformers can reverse-engineer modular arithmetic in-context to predict pseudo-random sequences. While successful for vanilla LCGs, future research should explore more complex PRNGs like Permuted Congruential Generators (PCGs) or truncated LCGs, which are designed to obfuscate linear states. Additionally, investigating whether models trained on broader arithmetic tasks can zero in on these mechanics without specialized PRNG training remains an open question.
Summary updated