/*
 * Copyright 2014 Aurélien Broszniowski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.rainfall.utils;

/**
 * Concurrent deterministic random bit generator
 * Based on the XORSHIFT function
 *
 * We want do avoid using a linear congruential generator in order to get a standardized test.
 *
 * @author Aurelien Broszniowski
 */

public class ConcurrentPseudoRandom {

  private final RandomFunction randomFunction = new ThreadLocal<RandomFunction>() {
    protected RandomFunction initialValue() {
      return new RandomFunction();
    }
  }.get();

  public long nextLong() {
    return this.randomFunction.nextLong();
  }

  public long nextLong(final long seed) {
    return this.randomFunction.nextLong(seed);
  }

  public float nextFloat() {
    return this.randomFunction.nextFloat();
  }

  public float nextFloat(final long seed) {
    return this.randomFunction.nextFloat(seed);
  }

  public float nextFloat(float max) {
    return max * this.randomFunction.nextFloat();
  }

  private class RandomFunction {

    long seed = 8682522807148012L ^ System.nanoTime();

    public long nextLong() {
      long nb = nextLong(this.seed);
      this.seed = this.seed * 181783497276652981L;
      return nb;
    }

    public long nextLong(long seed) {
      seed ^= (seed << 21);
      seed ^= (seed >>> 35);
      seed ^= (seed << 4);
      return seed;
    }

    public float nextFloat() {
      float nb = nextFloat(this.seed);
      this.seed = this.seed * 181783497276652981L;
      return nb;
    }

    //  Float.intBitsToFloat(nextInt()) ???
    public float nextFloat(final long next) {
      return Math.abs(((nextLong(next)) % 100000) / 100000f);
    }
  }
}

@@ -908,16 +908,28 @@
 e final 
+ThreadLocal%3C
 RandomFu
@@ -934,16 +934,17 @@
 Function
+%3E
  randomF
@@ -1083,15 +1083,94 @@
 %0A  %7D
+;%0A%0A  private RandomFunction getRandomFunction() %7B%0A    return randomFunction
 .get();
+%0A  %7D
 %0A%0A  
@@ -1197,38 +1197,36 @@
 () %7B%0A    return 
-this.r
+getR
 andomFunction.ne
@@ -1214,32 +1214,34 @@
 etRandomFunction
+()
 .nextLong();%0A  %7D
@@ -1287,38 +1287,36 @@
 d) %7B%0A    return 
-this.r
+getR
 andomFunction.ne
@@ -1308,24 +1308,26 @@
 ndomFunction
+()
 .nextLong(se
@@ -1368,38 +1368,36 @@
 () %7B%0A    return 
-this.r
+getR
 andomFunction.ne
@@ -1385,32 +1385,34 @@
 etRandomFunction
+()
 .nextFloat();%0A  
@@ -1469,22 +1469,20 @@
  return 
-this.r
+getR
 andomFun
@@ -1478,32 +1478,34 @@
 etRandomFunction
+()
 .nextFloat(seed)
@@ -1570,14 +1570,12 @@
 x * 
-this.r
+getR
 ando
@@ -1583,16 +1583,18 @@
 Function
+()
 .nextFlo

Make the ConcurrentPseudoRandom class truly concurrent by really having a thread-local random number generator