Class Random

java.lang.Object
   |
   +----Random

public class Random
extends Object
Random number generator. This is a plug-in replacement for the built-in generator in Java. This is used instead to ensure it is repeatable, won't change in future versions of Java, and has known good security. The algorithms used come from Numerical Recipies in C (2nd ed), but the actual code is original, and so not subject to their copyright. The code here is often simpler than the C code, since it can be written using 64-bit integers.

This code is (c) 1996,1997 Leemon Baird <leemon@cs.cmu.edu>, http://www.cs.cmu.edu/~baird
The source and object code may be redistributed freely. If the code is modified, please state so in the comments.

Version:
1.1, 22 July 97
Author:
Leemon Baird

Constructor Index

 o Random()
creates a new random number generator and sets its seed randomly according to the current time.
 o Random(long)
creates a new random number generator and sets its seed

Method Index

 o clone()
Create a copy of this generator which will generate the same sequence.
 o copyInto(Random)
Make the existing Random rnd into an exact clone of this Random.
 o maxLong()
return the maximum value ever returned by nextLong()
 o minLong()
return the minimum value ever returned by nextLong()
 o nextDouble()
Generates a pseudorandom, uniformly-distributed, double value in the range [0.0,1.0)
 o nextFloat()
Generates a pseudorandom, uniformly-distributed, float value in the range [0.0,1.0)
 o nextInt()
Generates a pseudorandom, uniformly-distributed, int value.
 o nextInt(int, int)
Generates a pseudorandom, uniformly-distributed, int value in [min,max].
 o nextLong()
Generates a pseudorandom, uniformly-distributed, long value.
 o setAlgorithm(int)
Sets which algorithm to use henceforth.
 o setSeed()
Sets the random number generator seed from the system clock
 o setSeed(long)
Sets the random number generator seed

Constructors

 o Random
 public Random()
creates a new random number generator and sets its seed randomly according to the current time.

 o Random
 public Random(long s)
creates a new random number generator and sets its seed

Methods

 o clone
 public Object clone()
Create a copy of this generator which will generate the same sequence.

Overrides:
clone in class Object
 o copyInto
 public void copyInto(Random rnd)
Make the existing Random rnd into an exact clone of this Random. It will then generate the same sequence as this.

 o setAlgorithm
 public final void setAlgorithm(int alg)
Sets which algorithm to use henceforth. Compared to algorithm zero, 1 takes 30% longer to run, 2 takes twice as long, and 3 takes 40% less time. Numerical Recipes in C recommends using 1 in most cases, or 2 for extremely good numbers, and offers a $1000 prize for anyone finding flaws in 2. If more than 100 million numbers are needed, it recommends 2 rather than 1, even though it takes 50% longer. Note that for 1, 2, and 3, if the random number generator is seeded with a given number and then run, it will always generate the same sequence. But, if one looks at the seed halfway through the sequence, reseeding with that seed in the future will *not* make it generate the second half of the sequence. This is because the true "seed" is actually a large table which is regenerated from scratch every time setSeed() is called. To store the state of a sequence so it can be regenerated later, use clone().
 -1 = ANSI C "example" algorithm (is bad, and only creates 16-bit rnd #s)
  0 = Park and Miller Minimum Standard (CACM, 88, from Lewis,Goodman,Miller, 69)
  1 = alg 0 plus random shuffling
  2 = two LCGs combined with a shuffle
  3 = Knuth's subtractive algorithm (probably a good algorithm)     

 o setSeed
 public final void setSeed(long s)
Sets the random number generator seed

 o setSeed
 public final void setSeed()
Sets the random number generator seed from the system clock

 o minLong
 public final long minLong()
return the minimum value ever returned by nextLong()

 o maxLong
 public final long maxLong()
return the maximum value ever returned by nextLong()

 o nextLong
 public final long nextLong()
Generates a pseudorandom, uniformly-distributed, long value. The number is between minLong() and maxLong() inclusive, which depends on which algorithm is currently selected.

 o nextDouble
 public final double nextDouble()
Generates a pseudorandom, uniformly-distributed, double value in the range [0.0,1.0)

 o nextFloat
 public final float nextFloat()
Generates a pseudorandom, uniformly-distributed, float value in the range [0.0,1.0)

 o nextInt
 public final int nextInt(int min,
                          int max)
Generates a pseudorandom, uniformly-distributed, int value in [min,max].

 o nextInt
 public final int nextInt()
Generates a pseudorandom, uniformly-distributed, int value.