Random number challenge

Hans Fugal hans at fugal.net
Thu Jun 12 15:56:56 MDT 2008

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.
> Go, lazylist, go! 

$ irb
 >> rand <your number here>
<your random number here>

Add 1 in your head if you want between 1 and N instead of 0 and N-1. Or 
do you mean a way without a computer? In that case grab lg(N) people and 
have them each flip a coin, repeat if you get a number bigger than N. 
Expected number of trials is between 1 and 2.

Hans Fugal ; http://hans.fugal.net

There's nothing remarkable about it. All one has to do is hit the
right keys at the right time and the instrument plays itself.
     -- Johann Sebastian Bach

More information about the PLUG mailing list