public final class Randomness
extends java.lang.Object
Random
.
It also supports logging, for debugging of apparently nondeterministic behavior.
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_SEED
The default initial seed for the random number generator.
|
private static java.util.Random |
random
The random generator that makes random choices.
|
private static int |
totalCallsToRandom
Number of calls to the underlying Random instance that this wraps.
|
static int |
verbosity
0 = no output, 1 = brief output, 2 = verbose output.
|
Modifier | Constructor and Description |
---|---|
private |
Randomness() |
Modifier and Type | Method and Description |
---|---|
private static void |
incrementCallsToRandom(java.lang.String caller)
Call this before every use of Randomness.random.
|
private static void |
logSelection(java.lang.Object returnValue,
java.lang.String methodName,
java.lang.Object argument)
Logs the value that was randomly selected, along with the calling method and its argument.
|
static double |
nextRandomGaussian(double mean,
double stdDev)
Returns a gaussianly-distributed random number.
|
static int |
nextRandomInt(int i)
Uniformly random int from [0, i) = from [0, i-1].
|
static <T> T |
nthMember(java.lang.Iterable<T> ible,
int n)
Returns the nth element (0-indexed) from the iterable.
|
static boolean |
randomBoolFromDistribution(double falseProb,
double trueProb)
Return true or false with the given relative probabilites, which need not add to 1.
|
static <T> T |
randomMember(java.util.Collection<T> c)
Returns a randomly-chosen member of the collection.
|
static <T> T |
randomMember(SimpleList<T> list)
Returns a randomly-chosen member of the list.
|
static <T> T |
randomMemberWeighted(SimpleList<T> list,
java.util.Map<T,java.lang.Double> weights)
Randomly selects an element from a weighted distribution of elements.
|
static <T> T |
randomMemberWeighted(SimpleList<T> list,
java.util.Map<T,java.lang.Double> weights,
double totalWeight)
Randomly selects an element from a weighted distribution of elements.
|
static <T> T |
randomSetMember(java.util.Collection<T> set)
Return a random member of the set, selected uniformly at random.
|
static void |
setSeed(long seed)
Sets the seed of this random number generator.
|
private static java.lang.String |
toString(java.lang.Object o)
Produces a printed representation of the object, depending on the verbosity level.
|
static boolean |
weightedCoinFlip(double trueProb)
Return true with probability
trueProb , otherwise false. |
public static int verbosity
public static final long DEFAULT_SEED
private static java.util.Random random
private static int totalCallsToRandom
public static void setSeed(long seed)
seed
- the initial seedprivate static void incrementCallsToRandom(java.lang.String caller)
caller
- the name of the method that called Randomness.randompublic static int nextRandomInt(int i)
i
- upper bound on range for generated valuespublic static double nextRandomGaussian(double mean, double stdDev)
mean
- the mean of the distributionstdDev
- the standard deviation of the distributionpublic static <T> T randomMember(SimpleList<T> list)
T
- the type of list elementslist
- the list from which to choose a random memberpublic static <T> T randomMember(java.util.Collection<T> c)
T
- the type of collection elementsc
- the collection from which to choose a random memberpublic static <T> T nthMember(java.lang.Iterable<T> ible, int n)
T
- the type of elements in the iterableible
- an iterable that has at least n
+1 elementsn
- the 0-based index of the member to returnpublic static <T> T randomMemberWeighted(SimpleList<T> list, java.util.Map<T,java.lang.Double> weights)
T
- the type of the elements in the listlist
- the list of elements to select fromweights
- the map of elements to their weights. Each element's weight must be
non-negative. An element with a weight of zero will never be selected.list
public static <T> T randomMemberWeighted(SimpleList<T> list, java.util.Map<T,java.lang.Double> weights, double totalWeight)
T
- the type of the elements in the listlist
- the list of elements to select fromweights
- the map of elements to their weights. Each element's weight must be
non-negative. An element with a weight of zero will never be selected.totalWeight
- the total weight of the elements of the listlist
public static <T> T randomSetMember(java.util.Collection<T> set)
T
- the type of elements of the set param set the collection from which to choose an
elementset
- the collection from which to select an elementpublic static boolean weightedCoinFlip(double trueProb)
trueProb
, otherwise false.trueProb
- the likelihood that true is returned; must be within [0..1]trueProb
; otherwise falsepublic static boolean randomBoolFromDistribution(double falseProb, double trueProb)
falseProb
- the likelihood that true is returned; an arbitrary non-negative numbertrueProb
- the likelihood that true is returned; an arbitrary non-negative numberprivate static void logSelection(java.lang.Object returnValue, java.lang.String methodName, java.lang.Object argument)
returnValue
- the value randomly selectedmethodName
- the name of the method calledargument
- the method argumentprivate static java.lang.String toString(java.lang.Object o)
o
- the object to produce a printed representation ofverbosity