Skip to content

Instantly share code, notes, and snippets.

@jkeesh
Created July 16, 2015 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeesh/86ce56d1f8751f1cd345 to your computer and use it in GitHub Desktop.
Save jkeesh/86ce56d1f8751f1cd345 to your computer and use it in GitHub Desktop.
Randomizer - Java
import java.util.*;
public class Randomizer{
public static Random theInstance = null;
public Randomizer(){
}
public static Random getInstance(){
if(theInstance == null){
theInstance = new Random();
}
return theInstance;
}
public static boolean nextBoolean(){
return Randomizer.getInstance().nextBoolean();
}
public static boolean nextBoolean(double probability){
return Randomizer.nextDouble() < probability;
}
public static int nextInt(){
return Randomizer.getInstance().nextInt();
}
public static int nextInt(int n){
return Randomizer.getInstance().nextInt(n);
}
/* Return a nubmer between min and max, inclusive. */
public static int nextInt(int min, int max){
return min + Randomizer.nextInt(max - min + 1);
}
public static double nextDouble(){
return Randomizer.getInstance().nextDouble();
}
public static double nextDouble(double min, double max){
return min + (max - min) * Randomizer.nextDouble();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment