Wednesday, April 6, 2011

When Random Isn't Random Enough

  For anyone who has done a bit of programming, there is a good chance that you've had to use random numbers. They can been very helpful for simulating the chaotic events we need for experiments or programs, such as basic AI for a game or natural events. But an important thing to know about most random numbers generated by a computer are not random at all but psudorandom.
  Random numbers generated by a computer require a seed, or some number put into an algorithm to produce the random number. This seed can come from many different places depending on the random number generator. The most basic generator uses a static number each time, producing the same exact string of random numbers each time, not very random. The next step up uses the system clock to pull the current time based in seconds from midnight of January 1, 1960. This is a decent way to to "randomize" the seed, but numbers generated within the same second pull from the same seed, which may cause problems. Other random number generators use chaotic inputs from the user to determine seeds, such as mouse movements or keyboard inputs. Mike Ash has a good blog post about the different tiers of random number generators here.
  If you desire true random numbers, then look no farther than Random.org. At Random.org, they use atmospheric noise to generate random numbers in a variety of forms, from simple integers to die rolls or cards drawn from a deck. They also have a very good piece detailing psudorandom number generators and how they compare with true random number generators. You could always use it to generate your passwords or choose your next vacation spot, probably better than throwing darts at a map.

2 comments:

  1. Huh. I never really thought about how random generators worked. I'm still not sure I understand from what you've said here, but thanks for the concept, anyway. I like that you link to another blog, and the Dilbert is great.

    I wonder if there is another way to explain the seed concept? Especially the connection to the system clock? Seems like there's some inherent metaphors there already. Would be interested in learning more.

    ReplyDelete
  2. Jen,

    Random number generators generally take the black box approach, implementation is generally obscured while more importance is placed on understanding how the input and output are related.

    As for the seed, you're correct, it's pretty much directly analogous to a plant seed. If you plant an apple seed, you'll always get an apple tree. Likewise for seeds placed into a random number generator, putting in a seed of '1234' into a RNG will always always generate that same string of "random" numbers.

    Using the system clock is mostly for convenience, an ever changing domain that is easy to pull from. It is simple to pull from without relying on the user for manual input while being reproducible.

    ReplyDelete