Class XltRandom


  • public class XltRandom
    extends java.lang.Object
    Utility class for random numbers and strings.

    Note that this class maintains a separate random number generator instance per thread.

    • Constructor Summary

      Constructors 
      Constructor Description
      XltRandom()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Random getRandom()
      Returns the random number generator singleton.
      static int getRandom​(int[] data)
      Returns a random number based on a given array of integers.
      static long getSeed()
      Returns the seed that was used to initialize the current thread's random number generator.
      static boolean nextBoolean()  
      static boolean nextBoolean​(int trueCaseProbability)
      Returns a random boolean value where the probability that true is returned is given as parameter.
      static void nextBytes​(byte[] bytes)  
      static double nextDouble()  
      static float nextFloat()  
      static double nextGaussian()  
      static int nextInt()  
      static int nextInt​(int n)  
      static int nextInt​(int minimum, int maximum)
      Returns a pseudo-random, uniformly distributed number that lies within the range from [minimum, maximum].
      static int nextIntWithDeviation​(int base, int deviation)
      Returns a pseudo-random, uniformly distributed number that lies within the range from [base - deviation, base + deviation].
      static long nextLong()  
      static void reseed()
      Reinitializes the current thread's random number generator with a new seed value that is derived from the current seed.
      static void setSeed​(long seed)
      Reinitializes the current thread's random number generator with the given seed value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XltRandom

        public XltRandom()
    • Method Detail

      • setSeed

        public static void setSeed​(long seed)
        Reinitializes the current thread's random number generator with the given seed value. Use this method together with getSeed() to reset the random number generator to a defined state in which it will produce the same sequence of random numbers.
        Parameters:
        seed - the seed
      • getSeed

        public static long getSeed()
        Returns the seed that was used to initialize the current thread's random number generator. Use this method together with setSeed(long) to reset the random number generator to a defined state in which it will produce the same sequence of random numbers.
        Returns:
        the seed
      • reseed

        public static void reseed()
        Reinitializes the current thread's random number generator with a new seed value that is derived from the current seed.
      • nextBoolean

        public static boolean nextBoolean()
        See Also:
        Random.nextBoolean()
      • nextBoolean

        public static boolean nextBoolean​(int trueCaseProbability)
        Returns a random boolean value where the probability that true is returned is given as parameter. The probability value has to be specified in the range of 0-100.
        • ≤ 0 - never returns true
        • 1..99 - the probability of true being returned
        • ≥ 100 - always returns true
        Parameters:
        trueCaseProbability - the probability of true being returned
        Returns:
        a random boolean value
      • nextBytes

        public static void nextBytes​(byte[] bytes)
        See Also:
        Random.nextBytes(byte[])
      • nextDouble

        public static double nextDouble()
        See Also:
        Random.nextDouble()
      • nextFloat

        public static float nextFloat()
        See Also:
        Random.nextFloat()
      • nextGaussian

        public static double nextGaussian()
        See Also:
        Random.nextGaussian()
      • nextInt

        public static int nextInt()
        See Also:
        Random.nextInt()
      • nextInt

        public static int nextInt​(int n)
        See Also:
        Random.nextInt(int)
      • nextLong

        public static long nextLong()
        See Also:
        Random.nextLong()
      • getRandom

        public static java.util.Random getRandom()
        Returns the random number generator singleton.
        Returns:
        the random number generator
      • getRandom

        public static int getRandom​(int[] data)
        Returns a random number based on a given array of integers.
        Parameters:
        data - an array with integers to choose from
        Returns:
        a random number from the array
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - will be thrown when an empty array is given
      • nextIntWithDeviation

        public static int nextIntWithDeviation​(int base,
                                               int deviation)
        Returns a pseudo-random, uniformly distributed number that lies within the range from [base - deviation, base + deviation].
        Parameters:
        base - base integer for the number
        deviation - the maximum deviation from base
        Returns:
        a random number
      • nextInt

        public static int nextInt​(int minimum,
                                  int maximum)
        Returns a pseudo-random, uniformly distributed number that lies within the range from [minimum, maximum].
        Parameters:
        minimum - the minimum value (inclusive)
        maximum - the maximum value (inclusive)
        Returns:
        a random number