v0.8.0: adjusted Random to be more flexible, now uses a generator stack
This commit is contained in:
@@ -21,6 +21,9 @@
|
||||
|
||||
package com.watabou.utils;
|
||||
|
||||
import com.watabou.noosa.Game;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -28,19 +31,38 @@ import java.util.List;
|
||||
|
||||
public class Random {
|
||||
|
||||
private static java.util.Random rand = new java.util.Random();
|
||||
|
||||
public static void seed( ){
|
||||
rand = new java.util.Random();
|
||||
//we store a stack of random number generators, which may be seeded deliberately or randomly.
|
||||
//top of the stack is what is currently being used to generate new numbers.
|
||||
//the base generator is always created with no seed, and cannot be popped.
|
||||
private static ArrayDeque<java.util.Random> generators;
|
||||
static {
|
||||
resetGenerators();
|
||||
}
|
||||
|
||||
public static void seed( long seed ){
|
||||
rand.setSeed(seed);
|
||||
public static void resetGenerators(){
|
||||
generators = new ArrayDeque<>();
|
||||
generators.push(new java.util.Random());
|
||||
}
|
||||
|
||||
public static void pushGenerator(){
|
||||
generators.push( new java.util.Random() );
|
||||
}
|
||||
|
||||
public static void pushGenerator( long seed ){
|
||||
generators.push( new java.util.Random( seed ) );
|
||||
}
|
||||
|
||||
public static void popGenerator(){
|
||||
if (generators.size() == 1){
|
||||
Game.reportException( new RuntimeException("tried to pop the last random number generator!"));
|
||||
} else {
|
||||
generators.pop();
|
||||
}
|
||||
}
|
||||
|
||||
//returns a uniformly distributed float in the range [0, 1)
|
||||
public static float Float() {
|
||||
return rand.nextFloat();
|
||||
return generators.peek().nextFloat();
|
||||
}
|
||||
|
||||
//returns a uniformly distributed float in the range [0, max)
|
||||
@@ -60,7 +82,7 @@ public class Random {
|
||||
|
||||
//returns a uniformly distributed int in the range [0, max)
|
||||
public static int Int( int max ) {
|
||||
return max > 0 ? rand.nextInt(max) : 0;
|
||||
return max > 0 ? generators.peek().nextInt(max) : 0;
|
||||
}
|
||||
|
||||
//returns a uniformly distributed int in the range [min, max)
|
||||
@@ -80,7 +102,7 @@ public class Random {
|
||||
|
||||
//returns a uniformly distributed long in the range [-2^63, 2^63)
|
||||
public static long Long() {
|
||||
return rand.nextLong();
|
||||
return generators.peek().nextLong();
|
||||
}
|
||||
|
||||
//returns a uniformly distributed long in the range [0, max)
|
||||
@@ -169,7 +191,7 @@ public class Random {
|
||||
}
|
||||
|
||||
public static<T> void shuffle( List<?extends T> list){
|
||||
Collections.shuffle(list, rand);
|
||||
Collections.shuffle(list, generators.peek());
|
||||
}
|
||||
|
||||
public static<T> void shuffle( T[] array ) {
|
||||
|
||||
Reference in New Issue
Block a user