


Random values can be obtained from the Math class using: public static double random() which produces pseudorandom double values in the range 0.0 <= r < 1.0 The first time it is invoked, it initializes with the current time. The java.util.Random class provide a more extensive set of random generators. Two constructors Random() and Random(long seed) offer the options of initialization from the current time or from a specific seed value. The methods in the Random class include:
Random Number Algorithm The Random class uses a linear congruential algorithm (descriptions in Knuth and Press) with a 48bit seed. If the constructor Random(long) or the setSeed(long) method is invoked, the algorithm will use only the lower 48 bits of the seed value. Random number generator formulas actually produce a sequence of numbers that eventually repeat. For the same seed value a formula will produce the same sequence. A seed simply selects where in the sequence to start. The generator will eventually repeat that seed value and start the same sequence again. Compared to the randomness of physical fluctuations, such as in radio noise, these formulas are said to produce pseudorandom numbers. To insure that applications ported to different platforms give the same results, all implementations of Java must use the same algorithm so that the same seed returns the same sequence regardless of the platform. The linear congruential formula in Java goes as x_{i+1} = (a * x_{i} + c) mod m As discussed in Knuth and Press, you should use such formulas with care. They can produce random number sequences of a length up to m but not necessarily that long. The length depends on the set of a, c, and m values chosen. Also, if you grab consecutive sequences of numbers of K length, and plot them as points in K dimensional space, they will not fully populate the volume randomly but instead lie on K1 dimnsional planes. There will be no more than m^{1/k} planes and possible less. If you need to create points in a space this way, you should shuffle the values obtained from the generator. (See Press for suggested shuffle algorithms.) In Java the values in the linear congruential formula are a
= 0x5DEECE66DL The actual code in next(int bits) goes as synchronized
protected int next(int bits) { Here the mod operation comes via the AND operation since m in this case has all 47 bits set to one. This method is protected (see Chapter 5: Java : Access Rules). The public random number methods accessible by all classes invoke this method. For example, nextInt() simply includes the statement return next(32); The nextLong() method invokes next(32) shifts the result by 32 bits to the left, invokes next(32) again and then OR's the two values together to obtain a 64 bit random number: ((long)next(32) << 32) + next(32); The nextFloat() method provides 224 values in the range range 0.0f (inclusive) to 1.0f (exclusive). The next(int bits) method provides a value up to 24 bits long and then this is divided by 224. next(24) / ((float)(1 << 24)); The nextDouble() method provdides 253 values in the range 0.0d (inclusive) to 1.0d (exclusive) using the statement (((long)next(26) << 27) + next(27)) / (double)(1L << 53) The nextBoolean() method uses the statement return next(1) != 0; See the java.util.Random class specification for more detailed descriptions for each of the next{type} methods. Note: we will discuss the Gaussian (or "normally") distributed generation algorithm used for the nextGaussian() method in Chapter 6 : Tech : Nonuniform Distributions.



