Marsaglia's mental pseudo random number generator
I like random numbers. They are useful; whether it’s making a decision (“coffee or tea”), performing a Monte-Carlo Simulation (“if we have 5-10 critical incidents per week, with a workaround time of 4-8 hours each, how many people do we need to perform 24/7 services taking sick leave and vacation into account”) or if doing solo Role Playing Games (“Critical hit? Critical FAIL, lol!”).
I always have dice with me, at my desk I do have a Magic Eight Ball, and because I need a lot of random numbers with good quality really fast, my workstation has a “Infinite Noise TRNG” hardware random number generator.
But if you want to do that in your head, good luck. We humans suck at inventing random numbers. It’s really that bad.
But Marsaglia, the inventor of the Diehard suite, came up with a clever idea how to create good-enough random numbers in your head with very simple math, as John D. Cook reported 1 in his blog.
It’s really pretty simple, I just lift it from John’s site as it’s trivial:
state = 42 # Set the seed to any two digit number
def random_digit():
tens = lambda n: n // 10
ones = lambda n: n % 10
global state
state = tens(state) + 6*ones(state)
return ones(state)
But… last night, when I tried to fall asleep, I was generating
random numbers rather than counting sheep, and bam, I found
something unexpected. The number 59
converged into itself, just
like the trivial case 0
. Meaning:
- If you pick
59
as the random seed, it will only return a9
and the next state will also be59
.
So much for getting sleep…