April 16, 2026
RNGs roll, comments troll
How the Roll Function Works (In APL\360 and Its Descendants)
Old-school random trick sparks fight: waste or smart paranoia
TLDR: APL/J’s “roll” uses a clever big-prime trick and the number 16807 to generate repeatable pseudo-random numbers, but the thread erupted over an extra zero-check. Some call it wasteful; others say old machines were fragile, so caution was smart—an enduring lesson in tradeoffs and tech history.
A dusty gem from the APL/J world just rolled back into the spotlight: the classic “roll” function, which spins out random-looking numbers by multiplying a secret seed by 16807 and trimming it with a giant prime (2,147,483,647). Math folks cheered the history—Euler! Mersenne primes!—but the comments zeroed in on one spicy detail: an extra check for zero that, according to the author, “can never happen.” Cue the slap-fight. One camp groaned that it’s wasted time in a tight loop; the other replied with a collective eye-roll, arguing that back on creaky mainframes with weak protection, paranoia kept bugs from bricking your session.
Humor poured in. People dunked on “7FFFFFFF” as the internet’s mood in hex. Others joked the generator’s so good it “never hits zero… and yet we keep looking,” while a history buff flexed that Euler proved the prime while blind—“peak devops.” Explainers popped up to translate the math: that big prime fits old 32-bit machines, and 16807 helps cycle through almost every number before repeating, which makes for smooth fake randomness. The real story isn’t the math, though—it’s the vibe war: minimalism vs. belt-and-suspenders coding, modern purity vs. retro survival instincts. And yes, the drama rolled harder than the dice.
Key Points
- •The roll function updates its seed as seed = (16807 × seed) mod 2,147,483,647.
- •The result ? y is computed as floor(y × seed / 2,147,483,647).
- •The modulus 2,147,483,647 is a Mersenne prime ((2^31)−1), proven prime by Euler in 1772.
- •Primitive roots modulo a prime generate all nonzero residues; 16,807 is such a base modulo 2,147,483,647, yielding a full-period sequence.
- •Examples with mod 7 (primitive roots 3 and 5) and mod 8 illustrate why prime moduli and suitable bases produce non-repeating cycles.