A linear random method based on xor shifts : Random « Development Class « Java






A linear random method based on xor shifts

  

//package com.studiofortress.sf.util;

import java.util.Random;

/**
 * a linear random method based on xor shifts--which is a fast way to do LFSR
 * --ie one clock per bit is slow. This is faster per step that java Random.
 *
 * This does better than LCC generators (ie passes the monkey tests and DNA
 * tests where LCG dont). In other words it does not have the hyperplane
 * problem.
 *
 * This has a period of 2**128-1. This is quite easy to prove as follows.
 *
 * the counter can be shown to be a LFSR with period 2**64-1. However we have a
 * simple counter in the stepped variable. That is after 2**64 counts stepped
 * mod 2**64 == 0. Hence the phase is shifted by one and the period of stepped
 * and counter are relatively prime. We combine them with Addition, which is
 * slightly nonlinear due to carry. Of course we could just use a simple ++
 * counter. But thats boring.
 *
 * We could use * for this as well and have a stronger condition for non
 * lineararity.
 *
 * We speed up the nextDouble function as well.
 *
 * @author bob - http://www.javagaming.org/index.php/topic,18426.0.html
 */
final class Random64 extends Random
{
  private static final double LONG_2_DOUBLE =1.0 / (double)(1L<<53);
  private static final long MASK_53=(1l<<53)-1;
  private static final long serialVersionUID =-6678124822567014769L;

  private static final long PRIME =0xd4d6712ee634312dl;
  private long counter ;
  private long stepped ;

  public Random64() {
    super();
                setSeed(System.nanoTime());
  }

  public Random64(long seed) {
    super(seed);
    setSeed(seed);
  }

  private void step(){
    counter ^=(counter << 21);
    counter ^=(counter >>> 35);
    counter ^=(counter << 4);
    stepped +=PRIME;
  }
  /**
   * could use all 64 bits over 2 calls?
   */
  @Override
  protected int next(int bits) {
    step();
    // Hate the dumb mask
    return (int) (((counter + stepped) >>> 31) & ((1l << bits) - 1));
  }

  @Override
  public void setSeed(long seed) {
    counter =seed;
    if (counter == 0)
      counter =1;
                stepped=0;
                step();
                step();
                step();
                stepped=0;
  }

  /**
   * uses only 32 bits of precision.
   */
  @Override
  public double nextDouble() {
    step();
    return ((counter+stepped) & MASK_53)*LONG_2_DOUBLE;
  }


  @Override
  public long nextLong() {
    step();
    return counter+stepped;
  }
}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
32.A random choice maker
33.Memory-efficient map of keys to values with list-style random-access semantics.
34.Generates a random integer inside the lo and hi interval
35.Randomizer
36.RandomGUID generates truly random GUIDs
37.A random access text file allows a text file to be accessed randomly starting at any position.
38.A garbage-free randomised quicksort
39.Mersenne Twister Random
40.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
41.Randomly permutes the elements of the specified array
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice