The randomX package for Java


The randomX package is an upward compatible superset of the standard Java API class java.util.Random. Unlike the standard Random class, randomX defines an abstract superclass which supports derived classes based on a variety of random and pseudorandom byte stream generators. The derived class need only supply methods to construct a new generator and return successive bytes; randomX provides all the higher level functionality of the standard Java Random class. (A derived class is, of course, free to override randomX's implementation of the higher level methods, where appropriate.)

When performing Monte Carlo calculations or simulations driven by pseudorandom sequences, there's always a lingering worry that the results may have been biased by subtle regularities in the generator's output. One excellent way to assuage such anxiety is to reproduce the results using a different generator, also reputed to be of high quality, but based on a totally different underlying algorithm. Even if one or even both of the generators exhibits some significant regularity in their output, is it unlikely in the extreme they would both depart from randomness in the same manner as to yield the same bias in the result.

randomX makes switching among different randomisation sources as easy as changing a single declaration in a program. Further, the randomX framework minimises the work involved in adding a new generator to the library. The high-level access to the Internet provided by the java.net package makes it simple to provide a randomX-derived object which returns genuine random data from the HotBits server. This allows you to develop your program using repeatable pseudorandom sequences which permit repeatable operation during the debugging phase, then switch to true random numbers after the program is working correctly.

The randomX Classes

The following classes are included in the current version of randomX. Contributions of classes based on other generators are welcome, and will be included in subsequent updates to randomX. Each class name is linked to the javadoc-generated page describing it in more detail.

randomX
This is the abstract superclass from which all randomX generators are derived. A derived class need only implement nextByte() and whatever mechanisms for seed specification (if appropriate) it requires.
randomJava
This is a randomX wrapper for the standard java.util.Random 48-bit linear congruential generator. It is provided so that this generator can be easily selected by a program using randomX. Even though the fundamental generator is the same, the results from the various methods differ from those of java.util.Random for a given seed because the algorithms used by randomX to assemble the various return types from the low level generator differ from those in the built-in class (which are not documented).
randomLCG
This class is based upon the linear congruential generator specified in the ANSI C standard for the rand() function:
Ij+1 = (Ij × 1103515245 + 12345) & 0x7FFFFFFF
Note: this is a rather weak generator and is not recommended for serious work. It is included for completeness, and to serve as a minimal template for developing new generators.
randomMCG
This generator is an implementation of the “Minimal Standard” multiplicative congruential generator of Park and Miller. [Park, S.K. and K.W. Miller, Communications of the ACM 31, 1192-1201 (1988).] The algorithm is:
Ij+1 = (Ij × 16807) & 0x7FFFFFFF
randomLEcuyer
This class employs L'Ecuyer's technique of combining the output of two multiplicative congruential generators, with a Bays-Durham shuffle at the end. This is considered an extremely good pseudorandom sequence generator.
randomHotBits
Real random numbers at last! This class obtains random bytes from the HotBits server over the Internet. Since the data stream consists of genuinely random data rather than an algorithmically generated sequence, no method for specifying an initial seed exists in this class.

Download randomX source code (gzipped tar)

HotBits Main Page

How HotBits Works

HotBits Hardware Description


Valid XHTML 1.0
by John Walker
July, 1996