[ThinAir Home] [Table of Contents] [Characteristics of Random Number Sequences] [The Class Interfaces]

4 Choosing a Random Number Generator

What you need in a PRNG depends on what you will use it for. If you need three unrelated numbers, you would probably not use the same generator as someone building a world-class crypto-system. Likewise, a solitare game has different requirements than a numeric integration package.

If you need a random sequence of numbers for a cryptographic system, almost none of the generators in the ThinAir library are useful. The same applies to a password-generation system. It is possible to use a PRNG in either of these applications, but the PRNG is only a small part of the overall system. The selection of a cryptographically secure random number source and its use in cryptography or password generation is far beyond the scope of this document. See [8] for more info.

4.1 Instant Gratification

In many cases, you don't want to know everything (or maybe anything) about the theory of these PRNGs. You just need a generator, now. This section should help to steer you in the direction of generators you can use right away.

If all you need is a generator to solve a problem right now, see Section 2 for six good, simple generators. All of these generators have good characteristics. They are also relatively fast and require no special knowledge to use. If you need a more powerful generator and don't need blinding speed, try Knuth28RandGen or LinMultWCarryRandGen. These generators are slightly slower than the common linear congruential PRNG, but have somewhat better characteristics.

If you need an extremely long period, try the MitchellMooreRandGen generator. It has been well studied and has a period on the order of 285-1. If you requested one number from this generator every nanosecond, this PRNG would repeat after 1.2 billion years! At a similar sampling rate, most of the PRNGs in this library would repeat in less than 5 seconds. In actuality, you can't use any of the PRNGs in this library at this speed, but it is something to think about.

If you don't mind a slower generator and want practically no correlation between successive numbers, you should consider the Secure Hash Algorithm based generators, SHACtrRandGen and SHARngRandGen. These generators require more seed information than the simpler generators. These generators use the Secure Hash Algorithm (see [9]) to generate the numbers in the sequence. This one-way function should make it practically impossible to determine a number in the sequence, even given a large number of preceeding values.

If you want to test your code with a truly bad generator to see how that would affect your output, look in the file randnot.h. (Section 10) All of these generators are really bad. For a torture test of your code, use the ConstantRandGen or CounterRandGen classes. These generators are worst-case scenarios for most programs.

4.2 A little bit of work

If you don't mind a little learning curve, many more generators are available with only a minor amount of configuration. One downside of this approach is that you can easily come up with a bad generator if you don't have some background in PRNG theory. Those of you who are familiar with the various issues that may arise in customizing PRNGs might be interested in some of these.

If you are looking for the classics, maybe I could interest you in variations of the linear congruential PRNG. The classes LinConRandGen, LinConModRandGen, MultConRandGen, and MultConModRandGen supply various versions of this generator. The most configurable of these is the LinConModRandGen class, which allows selection of multiplier, constant term, and modulus. The least configurable is MultConRandGen which only allows you to specify a multiplier.

Choosing the various parameters for a the linear congruential PRNGs is not a trivial exercise. Many sets of parameters have been tested over time and yield good results. You can find lists of some of these in [4] and [8]. In addition, Knuth gives good rules to use in choosing your own parameters in Section 3.2 of [4].

What if you're worried about the linear artifacts produced by the linear congruential PRNG, try some of the nonlinear congruential generators. For instance, SecConRandGen, SecConModRandGen, InvConRandGen, InvConModRandGen, and even QuadConRandGen replace the obvious linear artifacts with more subtle effects.

If you would like to use a table of random numbers from another source, look at TextTableRandGen, BinaryTableRandGen, and their children. One particularly interesting use of these generator types involves the DecimalTableRandGen and the file rand.rnt. This is the well-known Rand table of random digits (published in 1955). This combination allows you to supply this standard set of random numbers to your code while still using the ThinAir interfaces.

4.3 Willing to work for it

If you have a particular kind of generator in mind that ThinAir doesn't immediately provide, the library is extensible enough to suit almost anyone. It is often possible to combine a few simple PRNGs to create a better one. The CombineRandGen and its children simplify this task. Using the DifferenceRandGen or ShuffledMRandGen classes, requires more setup than most of the other generators. However, careful choices in their configuration may result in a fast generator with good characteristics. Then again, there's always the possibility of building your own generator based on ThinAir.

People with a method of generating random sequences from hardware might want to take a look at the file-based PRNGs, TextTableRandGen, BinaryTableRandGen, and their children. You could write the output of a hardware sequence generator to a file and use these classes to feed them to your code. Then again, you might want to look at Section 13.6 to see how to create a new generator that hooks directly to your hardware. Of course, a hardware-based generator may not support tokens since there is no way to replay a sequence.


For further information, contact G. Wade Johnson (gwadej@anomaly.org).

© 1997 G. Wade Johnson. All rights reserved.
http://www.anomaly.org/ThinAir/choosing.html