Random.java 3.78 KB
Newer Older
1
/* Copyright (C) 1998, 1999  Free Software Foundation
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

import java.io.Serializable;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date August 25, 1998.
 */
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
 * Status:  Believed complete and correct
 */

/* This class is completely specified by the spec to ensure absolute
 * portability between all implementations of Java 
 */
public class Random implements Serializable
{
  /* Used by next() to hold the state of the pseudorandom number generator */
29
  private long seed;
Tom Tromey committed
30 31 32

  /* Used by nextGaussian() to hold a precomputed value */
  /* to be delivered by that method the next time it is called */
33
  private double nextNextGaussian;
Tom Tromey committed
34 35 36

  /* Used by nextGaussian() to keep track of whether it is has precomputed */
  /* and stashed away the next value to be delivered by that method */
37 38 39
  private boolean haveNextNextGaussian = false;

  private static final long serialVersionUID = 3905348978240129619L;
Tom Tromey committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

  public Random()
  {
    this(System.currentTimeMillis());
  }

  public Random(long seed)
  {
    setSeed(seed);
  }

  protected synchronized int next(int bits)
  {
    seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
    return (int)(seed >>> (48 - bits));
  }

  // JDK1.2
  public boolean nextBoolean()
  {
    return next(1) != 0;
  }

  /* The method nextBytes() is not fully specified in the published specs.
   * At first I implemented it simply via:
   *	for (int i = 0; i < buf.length; i++)
   *	  buf[i] = (byte)next(8);
   * but a simple test did not yield the same results as the std implementation.
   * There seemed to be a relationship where each i byte above was at pos 4*i+3
   * in the std.  For efficiency, by reducing calls to the expensive math
   * routines, the std probably was calling next(32) once rather than next(8)
   * 4 times.  Changing the algorithm to the one below based on that assumption
   * then yielded identical results to the std.
   */
  public void nextBytes(byte[] buf)
  {
    int randInt = 0;

    for (int i = 0;  i < buf.length;  i++)
      {
	int shift = (i % 4) * 8;
        if (shift == 0)
            randInt = next(32);
        buf[i] = (byte) (randInt >> shift);
      }
  }

  public double nextDouble()
  {
    return (((long)next(26) << 27) + next(27)) / (double)(1L << 53);
  }

  public float nextFloat()
  {
    return next(24) / ((float)(1 << 24));
  }

  public synchronized double nextGaussian()
  {
    if (haveNextNextGaussian)
      {
        haveNextNextGaussian = false;
        return nextNextGaussian;
      }
    else
      {
        double v1, v2, s;
        do
	  { 
            v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
            v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
            s = v1 * v1 + v2 * v2;
          } while (s >= 1);
        double norm = Math.sqrt(-2 * Math.log(s)/s);
        nextNextGaussian = v2 * norm;
        haveNextNextGaussian = true;
        return v1 * norm;
      }
  }

  public int nextInt()
  {
    return next(32);
  }

  // JDK1.2
  public int nextInt(int n)
  {
    if (n <= 0)
      throw new IllegalArgumentException("n must be positive");

    int bits, val;
    do
      {
        bits = next(31);
        val = bits % n;
      } while (bits - val + (n-1) < 0);
    return val;
  }

  public long nextLong()
  {
    return ((long)next(32) << 32) + next(32);
  }

  public synchronized void setSeed(long seed)
  {
    this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
    haveNextNextGaussian = false;
  }
}