home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 4.0 KB | 141 lines |
-
- package simula.random;
-
- /**
- * The abstract class RandomEngine is the common parent of the entire hierarchy
- * of the pseudo-random number generators. It riproduces the same interface
- * as <code>java.util.Random</code>. The only thing required to have an effective
- * pseudo-random generator is the implementation of the <code>nextByte</code>
- * method.
- * This hierarchy is widely inspired to that realized in July 1996 by
- * <a href="http://www.fourmilab.ch/">John Walker</a>,
- * <a href="mailto:kelvin@fourmilab.ch">kelvin@fourmilab.ch</a>.
- * @see java.util.Random
- */
- public abstract class RandomEngine {
-
- /**
- * Useful to random bits generation.
- * @see nextBit
- */
- private int nbits = 0;
-
- private boolean iset = false;
-
- private double gset;
-
- // Estensione all'interfaccia di java.util.Random
- private byte b;
-
-
- /**
- * Returns the next pseudo-random bit.
- * @return the next pseudo-random bit.
- */
- public boolean nextBit() {
- boolean bit;
-
- if (nbits <= 0) {
- b = nextByte();
- nbits = 8;
- }
- bit = (b & 0x80) != 0;
- b <<= 1;
- nbits--;
- return bit;
- }
- /**
- * Returns the next pseudo-random byte.
- * @return the next pseudo-random byte.
- */
- public abstract byte nextByte();
- /**
- * Fills the entire given array with pseudo-random generated bytes.
- * @param buf Array to fill.
- */
- public void nextByte(byte buf[]) {
- nextByte(buf, buf.length);
- }
- /**
- * Fills the given array with pseudo-random generated bytes.
- * @param buf Array of at least <tt>byte</tt> to fill.
- * @param buflen Number of bytes to store.
- */
- public void nextByte(byte buf[], int buflen) {
- int i = 0;
-
- while (buflen-- > 0) {
- buf[i++] = nextByte();
- }
- }
- /**
- * Returns the next pseudo-random double precision floating point
- * value (according to a uniform distribution).
- * @return the next pseudo-random double precision floating point
- * value (according to a uniform distribution).
- */
- public double nextDouble() {
- return (double) ((nextLong() & 0x7FFFFFFFFFFFFFFFl) /
- (0x7FFFFFFFFFFFFFFFl * 1.0));
- }
- /**
- * Returns the next pseudo-random floating point value (according to a uniform distribution).
- * @return the next pseudo-random floating point value (according to a uniform distribution).
- */
- public float nextFloat() {
- return (float) ((nextInt() & 0x7FFFFFFF) / (0x7FFFFFFF * 1.0));
- }
- /**
- * Returns the next pseudo-random value according to a normal distribution.
- * @return the next pseudo-random value according to a normal distribution.
- */
- public double nextGaussian() {
- double fac, rsq, v1, v2;
-
- if (!iset) {
- do {
- v1 = 2 * nextDouble() - 1;
- v2 = 2 * nextDouble() - 1;
- rsq = v1 * v1 + v2 * v2;
- } while (rsq > 1.0 || rsq == 0.0);
- fac = Math.sqrt(-2.0 * Math.log(rsq) / rsq);
- gset = v1 * fac;
- iset = true;
- return v2 * fac;
- } else {
- iset = false;
- return gset;
- }
- }
- // Metodi che riproducono l'interfaccia di java.util.Random
-
- /**
- * Returns the next pseudo-random integer value (according to a uniform distribution).
- * @return the next pseudo-random integer value (according to a uniform distribution).
- */
- public int nextInt() {
- return (int) ((((int) nextShort()) << 16) | (((int) nextShort()) & 0xFFFF));
- }
- /**
- * Returns the next pseudo-random long integer value (according to a uniform distribution).
- * @return the next pseudo-random long integer value (according to a uniform distribution).
- */
- public long nextLong() {
- return (long) ((((long) nextInt()) << 32) | (((long) nextInt()) & 0xFFFFFFFFl));
- }
- /**
- * Returns the next pseudo-random short integer value (according to a uniform distribution).
- * @return the next pseudo-random short integer value (according to a uniform distribution).
- */
- public short nextShort() {
- return (short) ((((short) nextByte()) << 8) | ((short) (nextByte() & 0xFF)));
- }
- /**
- * The change of seed must be notified to the super class from every
- * child of RandomEngine.
- */
- public void setSeed() {
- nbits = 0;
- iset = false;
- }
- }