Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.alicebot.server.core.util
Class MersenneTwister  view MersenneTwister download MersenneTwister.java

java.lang.Object
  extended byjava.util.Random
      extended byorg.alicebot.server.core.util.MersenneTwister
All Implemented Interfaces:
java.io.Serializable

public class MersenneTwister
extends java.util.Random
implements java.io.Serializable


Field Summary
private  boolean __haveNextNextGaussian
           
private  double __nextNextGaussian
           
private static long GOOD_SEED
           
private static int LOWER_MASK
           
private static int M
           
private  int[] mag01
           
private static int MATRIX_A
           
private  int[] mt
           
private  int mti
           
private static int N
           
private static int TEMPERING_MASK_B
           
private static int TEMPERING_MASK_C
           
private static int UPPER_MASK
           
 
Fields inherited from class java.util.Random
 
Constructor Summary
MersenneTwister()
           
MersenneTwister(long l)
           
 
Method Summary
static void main(java.lang.String[] args)
           
protected  int next(int i)
          Generates the next pseudorandom number.
 boolean nextBoolean()
          Generates the next pseudorandom boolean.
 boolean nextBoolean(double d)
           
 boolean nextBoolean(float f)
           
 byte nextByte()
           
 void nextBytes(byte[] abyte0)
          Fills an array of bytes with random numbers.
 char nextChar()
           
 double nextDouble()
          Generates the next pseudorandom double uniformly distributed between 0.0 (inclusive) and 1.0 (exclusive).
 float nextFloat()
          Generates the next pseudorandom float uniformly distributed between 0.0f (inclusive) and 1.0f (exclusive).
 double nextGaussian()
          Generates the next pseudorandom, Gaussian (normally) distributed double value, with mean 0.0 and standard deviation 1.0.
 int nextInt(int i)
          Generates the next pseudorandom number.
 short nextShort()
           
private  void readObject(java.io.ObjectInputStream objectinputstream)
           
 void setSeed(int[] ai)
           
 void setSeed(long l)
          Sets the seed for this pseudorandom number generator.
 void setSeedOld(long l)
           
private  void writeObject(java.io.ObjectOutputStream objectoutputstream)
           
 
Methods inherited from class java.util.Random
nextInt, nextLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

N

private static final int N
See Also:
Constant Field Values

M

private static final int M
See Also:
Constant Field Values

MATRIX_A

private static final int MATRIX_A
See Also:
Constant Field Values

UPPER_MASK

private static final int UPPER_MASK
See Also:
Constant Field Values

LOWER_MASK

private static final int LOWER_MASK
See Also:
Constant Field Values

TEMPERING_MASK_B

private static final int TEMPERING_MASK_B
See Also:
Constant Field Values

TEMPERING_MASK_C

private static final int TEMPERING_MASK_C
See Also:
Constant Field Values

mt

private int[] mt

mti

private int mti

mag01

private int[] mag01

GOOD_SEED

private static final long GOOD_SEED
See Also:
Constant Field Values

__nextNextGaussian

private double __nextNextGaussian

__haveNextNextGaussian

private boolean __haveNextNextGaussian
Constructor Detail

MersenneTwister

public MersenneTwister()

MersenneTwister

public MersenneTwister(long l)
Method Detail

setSeedOld

public void setSeedOld(long l)

setSeed

public void setSeed(int[] ai)

setSeed

public void setSeed(long l)
Description copied from class: java.util.Random
Sets the seed for this pseudorandom number generator. As described above, two instances of the same random class, starting with the same seed, should produce the same results, if the same methods are called. The implementation for java.util.Random is:
public synchronized void setSeed(long seed)
{
  this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
  haveNextNextGaussian = false;
}


next

protected int next(int i)
Description copied from class: java.util.Random
Generates the next pseudorandom number. This returns an int value whose bits low order bits are independent chosen random bits (0 and 1 are equally likely). The implementation for java.util.Random is:
protected synchronized int next(int bits)
{
  seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
  return (int) (seed >>> (48 - bits));
}


writeObject

private void writeObject(java.io.ObjectOutputStream objectoutputstream)
                  throws java.io.IOException

readObject

private void readObject(java.io.ObjectInputStream objectinputstream)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException

nextBoolean

public boolean nextBoolean()
Description copied from class: java.util.Random
Generates the next pseudorandom boolean. True and false have the same probability. The implementation is:
public boolean nextBoolean()
{
  return next(1) != 0;
}


nextBoolean

public boolean nextBoolean(float f)

nextBoolean

public boolean nextBoolean(double d)

nextInt

public int nextInt(int i)
Description copied from class: java.util.Random
Generates the next pseudorandom number. This returns a value between 0(inclusive) and n(exclusive), and each value has the same likelihodd (1/n). (0 and 1 are equally likely). The implementation for java.util.Random is:
public int nextInt(int n)
{
  if (n <= 0)
    throw new IllegalArgumentException("n must be positive");

  if ((n & -n) == n)  // i.e., n is a power of 2
    return (int)((n * (long) next(31)) >> 31);

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

  return val;
}

This algorithm would return every value with exactly the same probability, if the next()-method would be a perfect random number generator. The loop at the bottom only accepts a value, if the random number was between 0 and the highest number less then 1<<31, which is divisible by n. The probability for this is high for small n, and the worst case is 1/2 (for n=(1<<30)+1). The special treatment for n = power of 2, selects the high bits of the random number (the loop at the bottom would select the low order bits). This is done, because the low order bits of linear congruential number generators (like the one used in this class) are known to be ``less random'' than the high order bits.


nextDouble

public double nextDouble()
Description copied from class: java.util.Random
Generates the next pseudorandom double uniformly distributed between 0.0 (inclusive) and 1.0 (exclusive). The implementation is as follows.
public double nextDouble()
{
  return (((long) next(26) << 27) + next(27)) / (double)(1L << 53);
}


nextFloat

public float nextFloat()
Description copied from class: java.util.Random
Generates the next pseudorandom float uniformly distributed between 0.0f (inclusive) and 1.0f (exclusive). The implementation is as follows.
public float nextFloat()
{
  return next(24) / ((float)(1 << 24));
}


nextBytes

public void nextBytes(byte[] abyte0)
Description copied from class: java.util.Random
Fills an array of bytes with random numbers. All possible values are (approximately) equally likely. The JDK documentation gives no implementation, but it seems to be:
public void nextBytes(byte[] bytes)
{
  for (int i = 0; i < bytes.length; i += 4)
  {
    int random = next(32);
    for (int j = 0; i + j < bytes.length && j < 4; j++)
    {
      bytes[i+j] = (byte) (random & 0xff)
      random >>= 8;
    }
  }
}


nextChar

public char nextChar()

nextShort

public short nextShort()

nextByte

public byte nextByte()

nextGaussian

public double nextGaussian()
Description copied from class: java.util.Random
Generates the next pseudorandom, Gaussian (normally) distributed double value, with mean 0.0 and standard deviation 1.0. The algorithm is as follows.
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;
  }
}

This is described in section 3.4.1 of The Art of Computer Programming, Volume 2 by Donald Knuth.


main

public static void main(java.lang.String[] args)