# Help me get random numbers by mental arithmetic.July 19, 2011 8:07 AM   Subscribe

I would like a pseudo-random number generation algorithm that I can calculate in my head.

So often when I'm out and about I find myself in desperate need of some random numbers! For example, I often want to have randomly varying intervals of time when exercising or giving rewards during dog training. I would like to be able to generate a sequence of pseudo-random numbers just by mental arithmetic. To be specific, let's say that the number should be uniformly distributed between 1 and 10 (although if there are easy ways to vary the interval, that would be great). Math & algorithm experts, can you suggest a suitable algorithm?

Please don't suggest that I roll a die or use my phone or do anything else that involves my hands, because I'm using my hands for dog training/exercise/whatever. Also I like mental arithmetic: doing some calculations in my head is part of what will make this fun.
posted by medusa to Science & Nature (18 answers total) 5 users marked this as a favorite

Knuth has a bit to say about RNGs and gives a few simple algorithms in his books.. Wiki gives the general form of an algorithm and inputs for it ..
posted by k5.user at 8:17 AM on July 19, 2011

The most common way in poker is to use the second hand on your wrist-watch - does that fall under the same category as rolling a die or using your phone?
posted by RustyBrooks at 8:22 AM on July 19, 2011 [2 favorites]

Does this thread help?
posted by mauvest at 8:27 AM on July 19, 2011

You could memorize the first 60 digits of pi and use the second hand on your watch to choose which digit to select. (or substitute your favorite irrational number.)
posted by advicepig at 8:55 AM on July 19, 2011 [4 favorites]

What you really need is a way to generate bits.

When you've worked out a way to do that you can generate as many bits as you need to generate a number as large as you want. This has the advantage of involving even more mental arithmetic.

For example, if you wanted to generate a number between 0 and 59 you would need to generate 6 bits (this would technically give you a number up to 63).

How you generate the bits is the difficult part. I would suggest counting objects and assigning odd and even numbers to the ones and zeroes.
"I see three trees, so first bit is 1, two birds so second bit is 0, nine clouds so third bit is 1, seven people so fourth bit is 1, four potholes so fifth bit is 0 and five cars so sixth bit is 1."
101101 in binary = 45 in decimal
posted by alby at 9:08 AM on July 19, 2011 [1 favorite]

Multiply `x` by `a`, add `c`, divide by `m` and take the remainder. Then do it again and again and again.
posted by Rat Spatula at 9:12 AM on July 19, 2011

Pick a random large number, divide by 7, keep remainder for 0-6, choose a big enough number so you can take the remainder-eth digit of the quotient for 0-9.
posted by zengargoyle at 9:20 AM on July 19, 2011

Close your eyes for a moment. Open them. Name the first thing you see. Count the position in the alphabet of each letter in the name and add them all up. Divide the sum by the interval you want; the remainder is your pseudorandom number.
posted by A Thousand Baited Hooks at 9:29 AM on July 19, 2011 [1 favorite]

Sorry, that should of course be "the remainder plus one is your pseudorandom number".
posted by A Thousand Baited Hooks at 10:04 AM on July 19, 2011

advicepig: using the first 60 digits of pi don't give a uniform distribution: they are

14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944

and so contains 3 0s, 5 1s, 6 2s, 8 3s, 7 4s, 6 5s, 4 6s, 5 7s, 6 8s, 10 9s. This amount of deviation from uniformity is not unexpected but could be a bit of a problem.

medusa: the second hand on your watch works, as RustyBrooks has suggested. If you want random numbers between 0 and n-1 you can take the remainder upon dividing the number of seconds by n. You'll get a uniform distribution as long as n is a factor of 60 - and 60 has lots of factors.
posted by madcaptenor at 10:19 AM on July 19, 2011

advicepig: using the first 60 digits of pi don't give a uniform distribution

madcaptenor, the OP didn't ask for a uniform distribution. But if he wanted one, then the idea from A Thousand Baited Hooks fails as well:

Close your eyes for a moment. Open them. Name the first thing you see. Count the position in the alphabet of each letter in the name...

A-things are much more common than Q-things.

--

My solution (and I've used it in similar situations: build a text file of random numbers (previously on The Blue). Before you leave, memorize as many as you need. Unfortunately, this leaves out the math-in-your-head fun.
posted by IAmBroom at 11:09 AM on July 19, 2011

It's a hard thing to do with computers, never mind your squishy think-meats. The easiest one in use is the linear congruential generator. LCG's typically are poor though, with low periodicity and obvious flaws like odd/even toggles. Still, you could probably pick a random number 0-99 in your head, then use and add and multiply steps to get the next number, and use the tens digit of that to get a 0-9 reasonably random number.

Picking your constants for adding (c) and multiplying (a) is the only hard part. Pick two and stick with them, as they need to have certain properties to guarantee a complete cycle over m.

Let's try an easy example. c is 11, and a is 7 (primes usually work well with LCGs).

You pick 86 in your head. Add 11, get 97. Multiply by seven and you'd get 679. Modulo 100, your next number would be 79. As mentioned, the low digit tends to be utter crap so use the 7.

If you keep this up, you'll eventually memorize all the possible 100 outputs. So you can change a and/or c again.

This being the internet, I assume corrections and criticisms will quickly follow.
posted by chairface at 11:26 AM on July 19, 2011

Fibonacci sequences are an only-moderately-bad pseudorandom number generator. If you can remember the last two values across a matter of days (maybe write them down when your hands are free or something), then you can probably find a sequence with a long enough period that doing it in your head you will never get repeats.

I would not recommend doing anything where randomness is really important with this -- in software there are much better alternatives. But Fibonacci is relatively easy to do mentally, and gives decently random results.
posted by contrarian at 12:09 PM on July 19, 2011

A-things are much more common than Q-things.

Yes, but you're not just looking at the first letter.
posted by madcaptenor at 12:13 PM on July 19, 2011

Virtually all the algorithmic generators require modulo division, which you can do in your head, but most people really really suck at it.

I'd have to second the "second-hand"-as-an-index-to-Pi approach. It seems like the most realistic option (unless you really like division).
posted by pla at 5:01 PM on July 19, 2011

Close your eyes for a moment. Open them. Name the first thing you see. Count the position in the alphabet of each letter in the name and add them all up. Divide the sum by the interval you want; the remainder is your pseudorandom number.

I was curoius as to whether this would actually provide a uniform distribution, so I checked it out. I piped /etc/dictionaries-common/words into a little script that calculated the sum and took modulo 10, then plotted the results with R.

I'll be damned. This would actually work pretty darn well.
posted by chrisamiller at 8:40 PM on July 19, 2011 [2 favorites]

Heh. I was pretty sure there'd be enough entropy in the word samples to give a reasonably smooth distribution; thanks for confirming my hunch.
posted by A Thousand Baited Hooks at 11:46 PM on July 19, 2011

I was curoius as to whether this would actually provide a uniform distribution, so I checked it out. I piped /etc/dictionaries-common/words into a little script that calculated the sum and took modulo 10, then plotted the results with R.

I'll be damned. This would actually work pretty darn well.

I'll be damned, too, chrisamiller. Completely unexpected, as it doesn't matter how many letters you take: their distribution still isn't uniform.

I wonder if this is some odd by-product (no pun intended) of regression to the mean. Weird, at any rate.
posted by IAmBroom at 1:28 PM on July 20, 2011

« Older Which knob am I supposed to turn to 11?   |   Quit smoking feedback loop Newer »
This thread is closed to new comments.