Random number challenge

Stuart Jansen sjansen at buscaluz.org
Thu Jun 12 09:08:14 MDT 2008

On Wed, 2008-06-11 at 19:54 -0600, Andrew Jorgensen wrote:
> This comes up every month at PLUG (for door prizes) so I thought I'd
> ask here.  What's an easy to remember way to generate a random number
> between 1 and some given number.  Language doesn't matter so long as
> the code is easy enough for someone to remember the entire thing and
> type it up at the drop of a hat.

The problem with most solutions post so far is that they depend on a
pseudo-random number generator. That can be okay if the algorithm is
statistically random and properly seeded. Unfortunately, most
programmers that resort to something like rand() aren't able to evaluate
the quality of the random number generator used, not are they likely to
pick a good seed value.

For example, $PID is a poor choice because you're probably going to be
running on a laptop. If you follow more of less the same process every
time you boot your laptop for Plug, you'll very likely to end up with
the same $PID. Now one could argue that's acceptable because the number
will also be assigned randomly. I'm personally not comfortable with that
assumption, however. Numbers would probably be assigned based on
seating, and over time people tend to gravitate toward their favorite
spot. Worse yet, a determined observer could determine a pattern and
careful choose his seat to game the system.

Instead of compensating for these weaknesses, I propose abandoning the
psuedo-random number generators and instead using a cryptographically
appropriate true random number generator:

echo $[ $(od -t u4 /dev/random -N 4 -A n) % $CEILING + 1 ]

More information about the PLUG mailing list