Monthly Archives: March 2024

Random Number Generation on Scientific Calculators

Introduction

The Casio fx-991EX is a widely used scientific calculator popular among students and educators. One of its features is the Ran# function, which generates a random decimal between 0 and 1. At first glance, it seems surprising that a simple handheld device without internet access, a system clock, or complex operating system components can produce random numbers.

This report explores the mechanisms by which the fx-991EX generates these random numbers, describing its likely internal methods, limitations, and initial data observations.


Understanding Randomness

There are two broad types of randomness:

  • True randomness: Derived from physical processes that are inherently unpredictable (e.g., radioactive decay, atmospheric noise).
  • Pseudo-randomness: Numbers generated by deterministic algorithms that appear random but are fully determined by an initial input called a “seed.”

Casio calculators, including the fx-991EX, rely on pseudo-random number generators (PRNGs) due to hardware constraints and cost-efficiency.


Core Mechanism of Pseudo-Random Number Generator (PRNG)

A PRNG uses a mathematical formula to generate a sequence of numbers that mimics true randomness. The core components include:

  • Seed: The initial value from which the sequence is generated.
  • Algorithm: The mathematical procedure, often a lightweight one like a linear congruential generator (LCG), which uses a formula of the form:
Xₙ₊₁ = (a * Xₙ + c) mod m

Where:

  • Xₙ is the current state,
  • a, c, and m are fixed constants,
  • mod ensures the result stays within a set range.

This process is efficient and can run on simple microcontrollers inside calculators, like the one inside modern Casio scientific calculators.


Where Does the Randomness Come From?

Unlike computers, the fx-991EX lacks a real-time clock or user-settable seed. So how does it avoid always producing the same numbers?

Likely entropy sources include:

  • Startup hardware state: Electrical noise or slight differences in how circuits initialize when powered on.
  • Button press timing: The precise moment you press the Ran# button might capture a different internal timer value.
  • Uninitialized memory: Leftover data in RAM or registers after startup.
  • Battery or solar cell voltage variations: While less likely, small variations in power supply might theoretically introduce variability if sampled by the microcontroller, though no confirmed evidence shows Casio explicitly uses this.

These factors help introduce subtle differences, but these are just my theory


Insights from the Calculator Community

Recent community discussions (such as those on Reddit) suggest that Casio’s graphing calculators, like the fx-CG50, may use a combined linear congruential generator (CLCG) rather than a simple LCG. A CLCG combines multiple LCG streams to achieve a much longer period (potentially up to 10^18 iterations) and improve statistical uniformity.

Community testers advise testing the raw Ran# function, as higher-level probability simulators (like dice-roll apps) may add extra layers of code that obscure the PRNG’s raw behavior. Advanced testers apply statistical methods like the chi-squared test or autocorrelation analysis to check fairness over large datasets.

An example set of outputs I gathered from an fx-991EX (sampled manually) includes:

2/125, 111/1000, 31/100, 11/1000, 159/1000, 153/1000, 453/500

While no obvious pattern emerges at a glance, detecting the underlying mathematical pattern would require thousands of samples and advanced statistical tools.

A known online tool for exploring PRNG behavior in TI calculators can be found here: TI-30X Calculator Tricks. Although this tool focuses on TI-30X devices, its concepts may guide similar investigations into Casio’s systems.


Could We Find the Pattern?

To uncover the exact algorithm, one would need to:

  • Collect a large dataset: Several thousand or even tens of thousands of random outputs.
  • Look for repetitions: Detecting the cycle length (period) of the PRNG.
  • Fit known formulas: Testing whether an LCG or CLCG formula fits the progression of outputs.
  • Analyze constants: Reverse-engineering the a, c, and m parameters.
  • Potential firmware analysis: For full certainty, one might attempt to extract the calculator firmware, disassemble it, and locate the exact random generation routine—though this is highly technical and may void warranties or breach usage terms.

While theoretically possible, I don’t have time.


Limitations

It is important to know:

  • The calculator’s random numbers are not cryptographically secure.
  • Given enough outputs, the PRNG’s sequence will eventually loop (within a finite period).

Hypothesis: What Casio Likely Uses Internally

Based on industry knowledge, embedded system design, and community research, it is reasonable to hypothesize that:

  • The fx-991EX employs a combined linear congruential generator (CLCG) or a similarly robust PRNG.
  • It initializes the seed using a mix of button timing, internal counters, startup state, and possibly minor hardware variations.
  • Each call to the random function advances the PRNG to the next number in the deterministic sequence.

Experimental Validation

To test these hypotheses, one can perform experiments:

  • Record long sequences of random numbers.
  • Check for repeating cycles or recognizable formula patterns.
  • Apply statistical methods like chi-squared tests, frequency analysis, or serial correlation.
  • Compare behaviors across multiple devices or after controlled resets.

Such experiments can reveal how much entropy is injected during startup and help identify the algorithm’s structure. If you are interested please do this experiment, I really want to know.


Appendix

  • PRNG (Pseudo-Random Number Generator): An algorithm that generates a sequence of numbers that appear random.
  • Seed: The initial value that determines the sequence generated by the PRNG.
  • LCG (Linear Congruential Generator): A simple and widely used type of PRNG.
  • CLCG (Combined Linear Congruential Generator): A PRNG that combines multiple LCGs to achieve longer periods and better randomness.
  • Entropy: A measure of unpredictability or randomness in a system.

To learn more about RNG, check out its Wikipedia article.


If you’re reading this and you own a Casio calculator (or even another brand), I’d love to know:

  • Have you ever noticed patterns in the random numbers?
  • Can you run a hard reset and see if two devices match?
  • Are you interested in helping collect and log large data batches?

Let me know in the comments!


Extras: Cloudflare Lava Lamp Wall

While Casio calculators rely on mathematical formulas to simulate randomness, companies like Cloudflare take it to the next level. In their San Francisco office, they famously use a wall of real, physical lava lamps as an entropy source to seed their cryptographic random number generators.

The constantly shifting, unpredictable light patterns from the lava lamps are captured by cameras and turned into raw randomness — an example of using true physical entropy instead of math-based pseudo-randomness. It’s a fun reminder that in high-security systems, randomness often comes straight from the messy, chaotic physical world! Check out Tom Scott’s video below.

Another video to help:

Thanks for reading!