Package randoop.util

Class Randomness


  • public final class Randomness
    extends Object
    A simple-to-use wrapper around Random.

    It also supports logging, for debugging of apparently nondeterministic behavior.

    • 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.
    • Constructor Detail

      • Randomness

        private Randomness()
    • 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 from
        weights - 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 from
        weights - 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 probability trueProb, 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 number
        trueProb - 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 selected
        methodName - the name of the method called
        argument - the method argument
      • toString

        private static String toString​(Object o)
        Produces a printed representation of the object, depending on the verbosity level.
        Parameters:
        o - the object to produce a printed representation of
        Returns:
        a printed representation of the argument
        See Also:
        verbosity