Package randoop.util
Class Randomness
- java.lang.Object
-
- randoop.util.Randomness
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_SEED
The default initial seed for the random number generator.private static 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.
-
Constructor Summary
Constructors Modifier Constructor Description private
Randomness()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
incrementCallsToRandom(String caller)
Call this before every use of Randomness.random.private static void
logSelection(Object returnValue, String methodName, Object argument)
Logs the value that was randomly selected, along with the calling method and its argument.static int
nextRandomInt(int i)
Uniformly random int from [0, i)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(List<T> list)
Returns a randomly-chosen member of the list.static <T> T
randomMember(SimpleList<T> list)
Returns a randomly-chosen member of the list.static <T> T
randomMemberWeighted(SimpleList<T> list, Map<T,Double> weights)
Randomly selects an element from a weighted distribution of elements.static <T> T
randomMemberWeighted(SimpleList<T> list, Map<T,Double> weights, double totalWeight)
Randomly selects an element from a weighted distribution of elements.static <T> T
randomSetMember(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 String
toString(Object o)
Produces a printed representation of the object, depending on the verbosity level.static boolean
weightedCoinFlip(double trueProb)
Return true with probabilitytrueProb
, otherwise false.
-
-
-
Field Detail
-
verbosity
public static int verbosity
0 = no output, 1 = brief output, 2 = verbose output.
-
DEFAULT_SEED
public static final long DEFAULT_SEED
The default initial seed for the random number generator.- See Also:
- Constant Field Values
-
random
private static Random random
The random generator that makes random choices. (Developer note: do not declare new Random objects; use this one instead).
-
totalCallsToRandom
private static int totalCallsToRandom
Number of calls to the underlying Random instance that this wraps.
-
-
Method Detail
-
setSeed
public static void setSeed(long seed)
Sets the seed of this random number generator.- Parameters:
seed
- the initial seed
-
incrementCallsToRandom
private static void incrementCallsToRandom(String caller)
Call this before every use of Randomness.random.- Parameters:
caller
- the name of the method that called Randomness.random
-
nextRandomInt
public static int nextRandomInt(int i)
Uniformly random int from [0, i)- Parameters:
i
- upper bound on range for generated values- Returns:
- a value selected from range [0, i)
-
randomMember
public static <T> T randomMember(List<T> list)
Returns a randomly-chosen member of the list.- Type Parameters:
T
- the type of list elements- Parameters:
list
- the list from which to choose a random member- Returns:
- a randomly-chosen member of the list
-
randomMember
public static <T> T randomMember(SimpleList<T> list)
Returns a randomly-chosen member of the list.- Type Parameters:
T
- the type of list elements- Parameters:
list
- the list from which to choose a random member- Returns:
- a randomly-chosen member of the list
-
randomMemberWeighted
public static <T> T randomMemberWeighted(SimpleList<T> list, Map<T,Double> weights)
Randomly selects an element from a weighted distribution of elements. These weights are with respect to each other. They are not normalized (they might add up to any value).- Type Parameters:
T
- the type of the elements in the list- Parameters:
list
- 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.- Returns:
- a randomly selected element from
list
-
randomMemberWeighted
public static <T> T randomMemberWeighted(SimpleList<T> list, Map<T,Double> weights, double totalWeight)
Randomly selects an element from a weighted distribution of elements. These weights are with respect to each other. They are not normalized (they might add up to any value).- Type Parameters:
T
- the type of the elements in the list- Parameters:
list
- 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 list- Returns:
- a randomly selected element from
list
-
randomSetMember
public static <T> T randomSetMember(Collection<T> set)
Return a random member of the set, selected uniformly at random.- Type Parameters:
T
- the type of elements of the set param set the collection from which to choose an element- Parameters:
set
- the collection from which to select an element- Returns:
- a randomly-selected member of the set
-
weightedCoinFlip
public static boolean weightedCoinFlip(double trueProb)
Return true with probabilitytrueProb
, otherwise false.- Parameters:
trueProb
- the likelihood that true is returned; must be within [0..1]- Returns:
- true with likelihood
trueProb
; otherwise false
-
randomBoolFromDistribution
public static boolean randomBoolFromDistribution(double falseProb, double trueProb)
Return true or false with the given relative probabilites, which need not add to 1.- Parameters:
falseProb
- the likelihood that true is returned; an arbitrary non-negative numbertrueProb
- the likelihood that true is returned; an arbitrary non-negative number- Returns:
- true or false, with the given probabilities
-
logSelection
private static void logSelection(Object returnValue, String methodName, Object argument)
Logs the value that was randomly selected, along with the calling method and its argument.- Parameters:
returnValue
- the value randomly selectedmethodName
- the name of the method calledargument
- the method argument
-
-