[freenet-cvs] r13851 - trunk/freenet/src/org/spaceroots/mantissa/random

nextgens at freenetproject.org nextgens at freenetproject.org
Sat Jun 30 23:18:55 UTC 2007


Author: nextgens
Date: 2007-06-30 23:18:55 +0000 (Sat, 30 Jun 2007)
New Revision: 13851

Modified:
   trunk/freenet/src/org/spaceroots/mantissa/random/MersenneTwister.java
Log:
indent

Modified: trunk/freenet/src/org/spaceroots/mantissa/random/MersenneTwister.java
===================================================================
--- trunk/freenet/src/org/spaceroots/mantissa/random/MersenneTwister.java	2007-06-30 16:52:34 UTC (rev 13850)
+++ trunk/freenet/src/org/spaceroots/mantissa/random/MersenneTwister.java	2007-06-30 23:18:55 UTC (rev 13851)
@@ -68,193 +68,187 @@
  * @version $Id: MersenneTwister.java,v 1.1 2005/02/07 23:03:14 amphibian Exp $
 
  */
-public class MersenneTwister
-  extends Random {
-
+public class MersenneTwister extends Random {
 	private static final long serialVersionUID = -1;
-	
-  /** Creates a new random number generator.
-   * <p>The instance is initialized using the current time as the
-   * seed.</p>
-   */
-  public MersenneTwister() {
-    mt = new int[N];
-    setSeed(System.currentTimeMillis());
-  }
 
-  /** Creates a new random number generator using a single int seed.
-   * @param seed the initial seed (32 bits integer)
-   */
-  public MersenneTwister(int seed) {
-    mt = new int[N];
-    setSeed(seed);
-  }
+	private static final int   N     = 624;
+	private static final int   M     = 397;
+	private static final int[] MAG01 = { 0x0, 0x9908b0df };
 
-  /** Creates a new random number generator using an int array seed.
-   * @param seed the initial seed (32 bits integers array), if null
-   * the seed of the generator will be related to the current time
-   */
-  public MersenneTwister(int[] seed) {
-    mt = new int[N];
-    setSeed(seed);
-  }
+	private int[] mt;
+	private int   mti;
 
-  /** Seed from byte[] */
-  public MersenneTwister(byte[] seed) {
-	mt = new int[N];
-	setSeed(seed);
-  }
-  
-  /** Creates a new random number generator using a single long seed.
-   * @param seed the initial seed (64 bits integer)
-   */
-  public MersenneTwister(long seed) {
-    mt = new int[N];
-    setSeed(seed);
-  }
+	/** Creates a new random number generator.
+	 * <p>The instance is initialized using the current time as the
+	 * seed.</p>
+	 */
+	public MersenneTwister() {
+		mt = new int[N];
+		setSeed(System.currentTimeMillis());
+	}
 
-  /** Reinitialize the generator as if just built with the given int seed.
-   * <p>The state of the generator is exactly the same as a new
-   * generator built with the same seed.</p>
-   * @param seed the initial seed (32 bits integer)
-   */
-  public void setSeed(int seed) {
-    // we use a long masked by 0xffffffffL as a poor man unsigned int
-    long longMT = seed;
-    mt[0]= (int) longMT;
-    for (mti = 1; mti < N; ++mti) {
-      // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
-      // initializer from the 2002-01-09 C version by Makoto Matsumoto
-      longMT = (1812433253l * (longMT ^ (longMT >> 30)) + mti) & 0xffffffffL; 
-      mt[mti]= (int) longMT;
-    }
-  }
+	/** Creates a new random number generator using a single int seed.
+	 * @param seed the initial seed (32 bits integer)
+	 */
+	public MersenneTwister(int seed) {
+		mt = new int[N];
+		setSeed(seed);
+	}
 
-  /**
-   * Seed from byte[].
-   */
-  public void setSeed(byte[] seed) {
-	  int[] seeds = new int[seed.length/4];
-	  for(int i=0;i<seeds.length;i+=4) {
-		  seeds[i] = Fields.bytesToInt(seed, i);
-	  }
-	  setSeed(seeds);
-  }
-  
-  /** Reinitialize the generator as if just built with the given int array seed.
-   * <p>The state of the generator is exactly the same as a new
-   * generator built with the same seed.</p>
-   * @param seed the initial seed (32 bits integers array), if null
-   * the seed of the generator will be related to the current time
-   */
-  public void setSeed(int[] seed) {
+	/** Creates a new random number generator using an int array seed.
+	 * @param seed the initial seed (32 bits integers array), if null
+	 * the seed of the generator will be related to the current time
+	 */
+	public MersenneTwister(int[] seed) {
+		mt = new int[N];
+		setSeed(seed);
+	}
 
-    if (seed == null) {
-      setSeed(System.currentTimeMillis());
-      return;
-    }
+	/** Seed from byte[] */
+	public MersenneTwister(byte[] seed) {
+		mt = new int[N];
+		setSeed(seed);
+	}
 
-    setSeed(19650218);
-    int i = 1;
-    int j = 0;
+	/** Creates a new random number generator using a single long seed.
+	 * @param seed the initial seed (64 bits integer)
+	 */
+	public MersenneTwister(long seed) {
+		mt = new int[N];
+		setSeed(seed);
+	}
 
-    for (int k = Math.max(N, seed.length); k != 0; k--) {
-      long l0 = (mt[i] & 0x7fffffffl)   | ((mt[i]   < 0) ? 0x80000000l : 0x0l);
-      long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l);
-      long l  = (l0 ^ ((l1 ^ (l1 >> 30)) * 1664525l)) + seed[j] + j; // non linear
-      mt[i]   = (int) (l & 0xffffffffl);
-      i++; j++;
-      if (i >= N) {
-        mt[0] = mt[N - 1];
-        i = 1;
-      }
-      if (j >= seed.length) {
-        j = 0;
-      }
-    }
+	/** Reinitialize the generator as if just built with the given int seed.
+	 * <p>The state of the generator is exactly the same as a new
+	 * generator built with the same seed.</p>
+	 * @param seed the initial seed (32 bits integer)
+	 */
+	public void setSeed(int seed) {
+		// we use a long masked by 0xffffffffL as a poor man unsigned int
+		long longMT = seed;
+		mt[0]= (int) longMT;
+		for (mti = 1; mti < N; ++mti) {
+			// See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
+			// initializer from the 2002-01-09 C version by Makoto Matsumoto
+			longMT = (1812433253l * (longMT ^ (longMT >> 30)) + mti) & 0xffffffffL; 
+			mt[mti]= (int) longMT;
+		}
+	}
 
-    for (int k = N - 1; k != 0; k--) {
-      long l0 = (mt[i] & 0x7fffffffl)   | ((mt[i]   < 0) ? 0x80000000l : 0x0l);
-      long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l);
-      long l  = (l0 ^ ((l1 ^ (l1 >> 30)) * 1566083941l)) - i; // non linear
-      mt[i]   = (int) (l & 0xffffffffL);
-      i++;
-      if (i >= N) {
-        mt[0] = mt[N - 1];
-        i = 1;
-      }
-    }
+	/**
+	 * Seed from byte[].
+	 */
+	public void setSeed(byte[] seed) {
+		int[] seeds = new int[seed.length/4];
+		for(int i=0;i<seeds.length;i+=4) {
+			seeds[i] = Fields.bytesToInt(seed, i);
+		}
+		setSeed(seeds);
+	}
 
-    mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array
+	/** Reinitialize the generator as if just built with the given int array seed.
+	 * <p>The state of the generator is exactly the same as a new
+	 * generator built with the same seed.</p>
+	 * @param seed the initial seed (32 bits integers array), if null
+	 * the seed of the generator will be related to the current time
+	 */
+	public void setSeed(int[] seed) {
+		if (seed == null) {
+			setSeed(System.currentTimeMillis());
+			return;
+		}
 
-  }
+		setSeed(19650218);
+		int i = 1;
+		int j = 0;
 
-  /** Reinitialize the generator as if just built with the given long seed.
-   * <p>The state of the generator is exactly the same as a new
-   * generator built with the same seed.</p>
-   * @param seed the initial seed (64 bits integer)
-   */
-  public void setSeed(long seed) {
-    if (mt == null) {
-      // this is probably a spurious call from base class constructor,
-      // we do nothing and wait for the setSeed in our own
-      // constructors after array allocation
-      return;
-    }
-    setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
-  }
+		for (int k = Math.max(N, seed.length); k != 0; k--) {
+			long l0 = (mt[i] & 0x7fffffffl)   | ((mt[i]   < 0) ? 0x80000000l : 0x0l);
+			long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l);
+			long l  = (l0 ^ ((l1 ^ (l1 >> 30)) * 1664525l)) + seed[j] + j; // non linear
+			mt[i]   = (int) (l & 0xffffffffl);
+			i++; j++;
+			if (i >= N) {
+				mt[0] = mt[N - 1];
+				i = 1;
+			}
+			if (j >= seed.length) {
+				j = 0;
+			}
+		}
 
-  /** Generate next pseudorandom number.
-   * <p>This method is the core generation algorithm. As per
-   * <code>java.util.Random</code> contract, it is used by all the
-   * public generation methods for the various primitive types {@link
-   * #nextBoolean nextBoolean}, {@link #nextBytes nextBytes}, {@link
-   * #nextDouble nextDouble}, {@link #nextFloat nextFloat}, {@link
-   * #nextGaussian nextGaussian}, {@link #nextInt nextInt} and {@link
-   * #nextLong nextLong}.</p>
-   * @param bits number of random bits to produce
-   */
-  protected int next(int bits) {
+		for (int k = N - 1; k != 0; k--) {
+			long l0 = (mt[i] & 0x7fffffffl)   | ((mt[i]   < 0) ? 0x80000000l : 0x0l);
+			long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l);
+			long l  = (l0 ^ ((l1 ^ (l1 >> 30)) * 1566083941l)) - i; // non linear
+			mt[i]   = (int) (l & 0xffffffffL);
+			i++;
+			if (i >= N) {
+				mt[0] = mt[N - 1];
+				i = 1;
+			}
+		}
 
-    int y;
+		mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array
 
-    if (mti >= N) { // generate N words at one time
-      int mtNext = mt[0];
-      for (int k = 0; k < N - M; ++k) {
-        int mtCurr = mtNext;
-        mtNext = mt[k + 1];
-        y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
-        mt[k] = mt[k + M] ^ (y >>> 1) ^ MAG01[y & 0x1];
-      }
-      for (int k = N - M; k < N - 1; ++k) {
-        int mtCurr = mtNext;
-        mtNext = mt[k + 1];
-        y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
-        mt[k] = mt[k + (M - N)] ^ (y >>> 1) ^ MAG01[y & 0x1];
-      }
-      y = (mtNext & 0x80000000) | (mt[0] & 0x7fffffff);
-      mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAG01[y & 0x1];
+	}
 
-      mti = 0;
-    }
-  
-    y = mt[mti++];
+	/** Reinitialize the generator as if just built with the given long seed.
+	 * <p>The state of the generator is exactly the same as a new
+	 * generator built with the same seed.</p>
+	 * @param seed the initial seed (64 bits integer)
+	 */
+	public void setSeed(long seed) {
+		if (mt == null) {
+			// this is probably a spurious call from base class constructor,
+			// we do nothing and wait for the setSeed in our own
+			// constructors after array allocation
+			return;
+		}
+		setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) });
+	}
 
-    // tempering
-    y ^= (y >>> 11);
-    y ^= (y <<   7) & 0x9d2c5680;
-    y ^= (y <<  15) & 0xefc60000;
-    y ^= (y >>> 18);
+	/** Generate next pseudorandom number.
+	 * <p>This method is the core generation algorithm. As per
+	 * <code>java.util.Random</code> contract, it is used by all the
+	 * public generation methods for the various primitive types {@link
+	 * #nextBoolean nextBoolean}, {@link #nextBytes nextBytes}, {@link
+	 * #nextDouble nextDouble}, {@link #nextFloat nextFloat}, {@link
+	 * #nextGaussian nextGaussian}, {@link #nextInt nextInt} and {@link
+	 * #nextLong nextLong}.</p>
+	 * @param bits number of random bits to produce
+	 */
+	protected int next(int bits) {
+		int y;
 
-    return y >>> (32 - bits);
+		if (mti >= N) { // generate N words at one time
+			int mtNext = mt[0];
+			for (int k = 0; k < N - M; ++k) {
+				int mtCurr = mtNext;
+				mtNext = mt[k + 1];
+				y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
+				mt[k] = mt[k + M] ^ (y >>> 1) ^ MAG01[y & 0x1];
+			}
+			for (int k = N - M; k < N - 1; ++k) {
+				int mtCurr = mtNext;
+				mtNext = mt[k + 1];
+				y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
+				mt[k] = mt[k + (M - N)] ^ (y >>> 1) ^ MAG01[y & 0x1];
+			}
+			y = (mtNext & 0x80000000) | (mt[0] & 0x7fffffff);
+			mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAG01[y & 0x1];
 
-  }
+			mti = 0;
+		}
 
-  private static final int   N     = 624;
-  private static final int   M     = 397;
-  private static final int[] MAG01 = { 0x0, 0x9908b0df };
+		y = mt[mti++];
 
-  private int[] mt;
-  private int   mti;
+		// tempering
+		y ^= (y >>> 11);
+		y ^= (y <<   7) & 0x9d2c5680;
+		y ^= (y <<  15) & 0xefc60000;
+		y ^= (y >>> 18);
 
+		return y >>> (32 - bits);
+	}
 }




More information about the cvs mailing list