Created
July 16, 2015 23:21
-
-
Save jkeesh/86ce56d1f8751f1cd345 to your computer and use it in GitHub Desktop.
Randomizer - Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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