How the CSPRNG Random Number & Statistical Engine Works
Generating truly unpredictable numbers inside a deterministic silicon microprocessor is one of the foundational challenges of modern cryptography, Monte Carlo physics simulations, clinical trial randomization, and fair lottery drawings. Almost all basic online random number generators rely on JavaScript's Math.random()—a fast, deterministic pseudorandom number generator (PRNG) that is never suitable for security, raffles, or statistical audits because its internal state can be reconstructed from just a few outputs.
Our workstation replaces standard pseudorandom routines with a multi-distribution Cryptographically Secure Pseudorandom Number Generator (CSPRNG) directly connected to your operating system's hardware entropy pool via window.crypto.getRandomValues():
- Unbiased Integer & High-Precision Decimal Sampling: Choose any custom range [Min, Max] and generate up to
1,000 numberssimultaneously as whole integers (ℤ) or floating-point decimals (2, 4, or 6 decimal places) using 53-bit IEEE 754 mantissa construction. - Independent Draws vs. Unique Lottery / Raffle Mode: Switch between sampling with replacement (independent dice rolls or coin flips where duplicates occur naturally) and sampling without replacement (Unique Lottery Mode, guaranteeing zero duplicate numbers for 6/49 Lotto, Eurojackpot 5/50, or raffle ticket draws).
- Live Statistical & Shannon Entropy Telemetry: Every draw automatically computes the sample Sum (Σ), Arithmetic Mean (µ) compared against theoretical expected value E[X] = (Min+Max)/2, Median (Q2), Even/Odd parity ratio, Population Standard Deviation (σ), and total Shannon Entropy in bits (H = N log2(|Ω|)).
Why Naive Modulo Arithmetic (rand() % N) Creates Modulo Bias
Even when a developer uses a cryptographic 32-bit unsigned integer source R ∈ [0, 232-1], mapping that 32-bit word into a user-specified range of size K = Max - Min + 1 using the remainder operator (min + (R % K)) introduces a mathematical flaw known as Modulo Bias.
A 32-bit unsigned integer has exactly 232 = 4,294,967,296 possible states. Unless your target range size K is an exact power of two (2, 4, 8, 16, …), 232 is not evenly divisible by K. Consider a simple illustrative example using a small random source that produces integers from 0 to 7 (8 states) to pick a random number among K = 3 choices ($0, 1, 2$):
0 % 3 = 0,3 % 3 = 0,6 % 3 = 0→ Outcome 0 is mapped by 3 states (37.5% probability).1 % 3 = 1,4 % 3 = 1,7 % 3 = 1→ Outcome 1 is mapped by 3 states (37.5% probability).2 % 3 = 2,5 % 3 = 2→ Outcome 2 is mapped by only 2 states (25.0% probability).
Because 8 mod 3 = 2, the lower residues (0 and 1) receive an extra mapping from the incomplete final cycle! To eliminate Modulo Bias completely (0.0000% skew), our engine implements strict Rejection Sampling:
// Unbiased CSPRNG Rejection Sampling in [min, max]:
const range = max - min + 1;
const maxUnbiased = Math.floor(0x100000000 / range) * range;
let r;
do {
r = crypto.getRandomValues(new Uint32Array(1))[0];
} while (r >= maxUnbiased); // Discard the incomplete upper remainder zone!
return min + (r % range);
By calculating the largest multiple of K that fits inside 232 and discarding any raw 32-bit integer falling inside the tiny upper remainder tail [M, 232-1], every single integer in [Min, Max] is guaranteed to have an identical mathematical probability of P(x) = 1/K.
From John von Neumann’s 1946 Middle-Square to Hardware CSPRNG
In 1946, while working on thermonuclear neutron transport simulations (the Monte Carlo method) on the ENIAC computer at Los Alamos, mathematician John von Neumann proposed the Middle-Square Method: take a 4-digit number, square it to produce an 8-digit number, and extract the middle 4 digits as the next random value. Von Neumann famously remarked: "Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin." He knew that deterministic formulas eventually fall into short repeating loops (for example, starting at 3792 immediately locks into a stagnant cycle).
Later decades introduced the Linear Congruential Generator (LCG), Xn+1 = (aXn + c) mod m, which George Marsaglia proved in 1968 suffers from severe serial correlation—when plotted as 3D triplets (Xn, Xn+1, Xn+2), all points fall onto a small number of parallel flat hyperplanes. Modern browsers upgraded Math.random() to the Xorshift128+ algorithm, which passes basic statistical tests but remains non-cryptographic: observing just three consecutive numbers allows an attacker using an SMT solver (such as Microsoft Z3) to solve the 128-bit state registers and predict every future draw.
By contrast, our generator calls the operating system's kernel CSPRNG (/dev/urandom ChaCha20 stream cipher on Linux/macOS/Android, BCryptGenRandom on Windows, and Secure Enclave TRNG on iOS), which continuously harvests physical Shannon entropy from CPU thermal silicon jitter (RDRAND / RDSEED ring oscillators), disk I/O interrupt nanosecond timings, and hardware interrupt variance.
Box–Muller Transform: Generating Gaussian Bell Curve Numbers
While lotteries and dice require a flat Uniform Distribution (where every number between 1 and 100 is equally likely), natural phenomena—such as human height, IQ scores, measurement errors, financial asset log-returns, and thermal molecular velocities—follow a Gaussian Normal Distribution N(µ, σ2), where values cluster around the central mean µ and taper off symmetrically toward the tails.
When you switch the Probability Distribution selector to Gaussian Normal (Box–Muller Bell Curve), our engine applies the exact analytical transform published by George Edward Pelham Box and Mervin Edgar Muller in 1958. Taking two independent uniform CSPRNG samples U1, U2 ∈ (0, 1), the engine synthesizes a standard normal random variate Z0 ∼ N(0, 1):
Z_0 = Math.sqrt(-2.0 * Math.log(U_1)) * Math.cos(2.0 * Math.PI * U_2);
The variate Z0 is then scaled to your chosen [Min, Max] window by centering the mean at µ = (Min + Max) / 2 and setting the standard deviation to σ = (Max - Min) / 6 (so that ± 3σ spans 99.73% of your range according to the empirical 68–95–99.7 rule).