Class RandomDataGenerator

    • Constructor Summary

      Constructors 
      Constructor Description
      RandomDataGenerator()
      Construct a RandomDataGenerator with a default RandomGenerator as its source of random data.
      RandomDataGenerator​(long seed)
      Construct a RandomDataGenerator with a default RandomGenerator as its source of random data, initialized with the given seed value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected RandomGenerator delegate()
      Returns the backing delegate instance that methods are forwarded to.
      double nextBeta​(double alpha, double beta)
      Returns the next pseudo-random beta-distributed value with the given shape and scale parameters.
      boolean nextBoolean()
      Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
      void nextBytes​(byte[] bytes)
      Generates random bytes and places them into a user-supplied byte array.
      void nextBytes​(byte[] bytes, int offset, int length)
      Generates random bytes and places them into a user-supplied byte array.
      int nextDeviate​(IntegerDistribution dist)
      Returns a random deviate from the given distribution.
      double nextDeviate​(RealDistribution dist)
      Returns a random deviate from the given distribution.
      int[] nextDeviates​(IntegerDistribution dist, int size)
      Returns an array of random deviates from the given distribution.
      double[] nextDeviates​(RealDistribution dist, int size)
      Returns an array of random deviates from the given distribution.
      double nextDouble()
      Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
      double nextExponential​(double mean)
      Returns the next pseudo-random, exponentially distributed deviate.
      float nextFloat()
      Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
      double nextGamma​(double shape, double scale)
      Returns the next pseudo-random gamma-distributed value with the given shape and scale parameters.
      double nextGaussian()
      Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
      String nextHexString​(int len)
      Generates a random string of hex characters of length len.
      int nextInt()
      Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
      int nextInt​(int n)
      Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
      int nextInt​(int lower, int upper)
      Returns a uniformly distributed random integer between lower and upper (inclusive).
      double nextLogNormal​(double shape, double scale)
      Returns the next log-normally-distributed pseudo-random deviate.
      long nextLong()
      Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
      long nextLong​(long n)
      Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
      long nextLong​(long lower, long upper)
      Returns a uniformly distributed random long integer between lower and upper (inclusive).
      double nextNormal​(double mean, double standardDeviation)
      Returns the next normally-distributed pseudo-random deviate.
      int[] nextPermutation​(int n, int k)
      Generates an integer array of length k whose entries are selected randomly, without repetition, from the integers 0, ..., n - 1 (inclusive).
      int nextPoisson​(double mean)
      Returns a poisson-distributed deviate with the given mean.
      double[] nextSample​(double[] a, int k)
      Returns an array of k double values selected randomly from the double array a.
      Object[] nextSample​(Collection<?> c, int k)
      Returns an array of k objects selected randomly from the Collection c.
      int[] nextSampleWithReplacement​(int sampleSize, double[] weights)
      Generates a random sample of size sampleSize from {0, 1, ...
      double nextUniform​(double lower, double upper)
      Returns a double value uniformly distributed over [lower, upper]
      int nextZipf​(int numberOfElements, double exponent)
      Returns an integer value following a Zipf distribution with the given parameter.
      static RandomDataGenerator of​(RandomGenerator randomGenerator)
      Factory method to create a RandomData instance using the supplied RandomGenerator.
      void setSeed​(int seed)
      Sets the seed of the underlying random number generator using an int seed.
      void setSeed​(int[] seed)
      Sets the seed of the underlying random number generator using an int array seed.
      void setSeed​(long seed)
      Sets the seed of the underlying random number generator using a long seed.
    • Constructor Detail

      • RandomDataGenerator

        public RandomDataGenerator()
        Construct a RandomDataGenerator with a default RandomGenerator as its source of random data.
      • RandomDataGenerator

        public RandomDataGenerator​(long seed)
        Construct a RandomDataGenerator with a default RandomGenerator as its source of random data, initialized with the given seed value.
        Parameters:
        seed - seed value
    • Method Detail

      • of

        public static RandomDataGenerator of​(RandomGenerator randomGenerator)
        Factory method to create a RandomData instance using the supplied RandomGenerator.
        Parameters:
        randomGenerator - source of random bits
        Returns:
        a RandomData using the given RandomGenerator to source bits
        Throws:
        MathIllegalArgumentException - if randomGenerator is null
      • delegate

        protected RandomGenerator delegate()
        Returns the backing delegate instance that methods are forwarded to.

        Concrete subclasses override this method to supply the instance being decorated.

        Returns:
        the delegate instance
      • nextBeta

        public double nextBeta​(double alpha,
                               double beta)
        Returns the next pseudo-random beta-distributed value with the given shape and scale parameters.
        Parameters:
        alpha - First shape parameter (must be positive).
        beta - Second shape parameter (must be positive).
        Returns:
        beta-distributed random deviate
      • nextExponential

        public double nextExponential​(double mean)
        Returns the next pseudo-random, exponentially distributed deviate.
        Parameters:
        mean - mean of the exponential distribution
        Returns:
        exponentially distributed deviate about the given mean
      • nextGamma

        public double nextGamma​(double shape,
                                double scale)
        Returns the next pseudo-random gamma-distributed value with the given shape and scale parameters.
        Parameters:
        shape - shape parameter of the distribution
        scale - scale parameter of the distribution
        Returns:
        gamma-distributed random deviate
      • nextNormal

        public double nextNormal​(double mean,
                                 double standardDeviation)
        Returns the next normally-distributed pseudo-random deviate.
        Parameters:
        mean - mean of the normal distribution
        standardDeviation - standard deviation of the normal distribution
        Returns:
        a random value, normally distributed with the given mean and standard deviation
      • nextLogNormal

        public double nextLogNormal​(double shape,
                                    double scale)
        Returns the next log-normally-distributed pseudo-random deviate.
        Parameters:
        shape - shape parameter of the log-normal distribution
        scale - scale parameter of the log-normal distribution
        Returns:
        a random value, normally distributed with the given mean and standard deviation
      • nextPoisson

        public int nextPoisson​(double mean)
        Returns a poisson-distributed deviate with the given mean.
        Parameters:
        mean - expected value
        Returns:
        poisson deviate
        Throws:
        MathIllegalArgumentException - if mean is not strictly positive
      • nextDeviate

        public double nextDeviate​(RealDistribution dist)
        Returns a random deviate from the given distribution.
        Parameters:
        dist - the distribution to sample from
        Returns:
        a random value following the given distribution
      • nextDeviates

        public double[] nextDeviates​(RealDistribution dist,
                                     int size)
        Returns an array of random deviates from the given distribution.
        Parameters:
        dist - the distribution to sample from
        size - the number of values to return
        Returns:
        an array of size values following the given distribution
      • nextDeviate

        public int nextDeviate​(IntegerDistribution dist)
        Returns a random deviate from the given distribution.
        Parameters:
        dist - the distribution to sample from
        Returns:
        a random value following the given distribution
      • nextDeviates

        public int[] nextDeviates​(IntegerDistribution dist,
                                  int size)
        Returns an array of random deviates from the given distribution.
        Parameters:
        dist - the distribution to sample from
        size - the number of values to return
        Returns:
        an array of size values following the given distribution
      • nextInt

        public int nextInt​(int lower,
                           int upper)
        Returns a uniformly distributed random integer between lower and upper (inclusive).
        Parameters:
        lower - lower bound for the generated value
        upper - upper bound for the generated value
        Returns:
        a random integer value within the given bounds
        Throws:
        MathIllegalArgumentException - if lower is not strictly less than or equal to upper
      • nextLong

        public long nextLong​(long lower,
                             long upper)
                      throws MathIllegalArgumentException
        Returns a uniformly distributed random long integer between lower and upper (inclusive).
        Parameters:
        lower - lower bound for the generated value
        upper - upper bound for the generated value
        Returns:
        a random long integer value within the given bounds
        Throws:
        MathIllegalArgumentException - if lower is not strictly less than or equal to upper
      • nextUniform

        public double nextUniform​(double lower,
                                  double upper)
        Returns a double value uniformly distributed over [lower, upper]
        Parameters:
        lower - lower bound
        upper - upper bound
        Returns:
        uniform deviate
        Throws:
        MathIllegalArgumentException - if upper is less than or equal to upper
      • nextZipf

        public int nextZipf​(int numberOfElements,
                            double exponent)
        Returns an integer value following a Zipf distribution with the given parameter.
        Parameters:
        numberOfElements - number of elements of the distribution
        exponent - exponent of the distribution
        Returns:
        random Zipf value
      • nextHexString

        public String nextHexString​(int len)
                             throws MathIllegalArgumentException
        Generates a random string of hex characters of length len.

        The generated string will be random, but not cryptographically secure.

        Algorithm Description: hex strings are generated using a 2-step process.

        1. len / 2 + 1 binary bytes are generated using the underlying Random
        2. Each binary byte is translated into 2 hex digits

        Parameters:
        len - the desired string length.
        Returns:
        the random string.
        Throws:
        MathIllegalArgumentException - if len <= 0.
      • nextPermutation

        public int[] nextPermutation​(int n,
                                     int k)
                              throws MathIllegalArgumentException
        Generates an integer array of length k whose entries are selected randomly, without repetition, from the integers 0, ..., n - 1 (inclusive).

        Generated arrays represent permutations of n taken k at a time.

        This method calls MathArrays.shuffle in order to create a random shuffle of the set of natural numbers { 0, 1, ..., n - 1 }.
        Parameters:
        n - the domain of the permutation
        k - the size of the permutation
        Returns:
        a random k-permutation of n, as an array of integers
        Throws:
        MathIllegalArgumentException - if k > n.
        MathIllegalArgumentException - if k <= 0.
      • nextSample

        public double[] nextSample​(double[] a,
                                   int k)
                            throws MathIllegalArgumentException
        Returns an array of k double values selected randomly from the double array a.

        Sampling from a is without replacement; but if a contains identical objects, the sample may include repeats. If all elements of a are distinct, the resulting object array represents a Simple Random Sample of size k from the elements of a.

        Parameters:
        a - the array to be sampled
        k - the size of the sample
        Returns:
        a random sample of k elements from a
        Throws:
        MathIllegalArgumentException - if k > c.size().
        MathIllegalArgumentException - if k <= 0.
      • nextSampleWithReplacement

        public int[] nextSampleWithReplacement​(int sampleSize,
                                               double[] weights)
        Generates a random sample of size sampleSize from {0, 1, ... , weights.length - 1}, using weights as probabilities.

        For 0 < i < weights.length, the probability that i is selected (on any draw) is weights[i]. If necessary, the weights array is normalized to sum to 1 so that weights[i] is a probability and the array sums to 1.

        Weights can be 0, but must not be negative, infinite or NaN. At least one weight must be positive.

        Parameters:
        sampleSize - size of sample to generate
        weights - probability sampling weights
        Returns:
        an array of integers between 0 and weights.length - 1
        Throws:
        MathIllegalArgumentException - if weights contains negative, NaN or infinite values or only 0s or sampleSize is less than 0
      • setSeed

        public void setSeed​(int seed)
        Sets the seed of the underlying random number generator using an int seed.

        Sequences of values generated starting with the same seeds should be identical.

        Specified by:
        setSeed in interface RandomGenerator
        Parameters:
        seed - the seed value
      • setSeed

        public void setSeed​(int[] seed)
        Sets the seed of the underlying random number generator using an int array seed.

        Sequences of values generated starting with the same seeds should be identical.

        Specified by:
        setSeed in interface RandomGenerator
        Parameters:
        seed - the seed value
      • setSeed

        public void setSeed​(long seed)
        Sets the seed of the underlying random number generator using a long seed.

        Sequences of values generated starting with the same seeds should be identical.

        Specified by:
        setSeed in interface RandomGenerator
        Parameters:
        seed - the seed value
      • nextBytes

        public void nextBytes​(byte[] bytes)
        Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.
        Specified by:
        nextBytes in interface RandomGenerator
        Parameters:
        bytes - the non-null byte array in which to put the random bytes
      • nextBytes

        public void nextBytes​(byte[] bytes,
                              int offset,
                              int length)
        Generates random bytes and places them into a user-supplied byte array.
        Specified by:
        nextBytes in interface RandomGenerator
        Parameters:
        bytes - the non-null byte array in which to put the random bytes
        offset - the starting index for inserting the generated bytes into the array
        length - the number of bytes to generate
      • nextInt

        public int nextInt()
        Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

        All 232 possible int values should be produced with (approximately) equal probability.

        Specified by:
        nextInt in interface RandomGenerator
        Returns:
        the next pseudorandom, uniformly distributed int value from this random number generator's sequence
      • nextInt

        public int nextInt​(int n)
        Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
        Specified by:
        nextInt in interface RandomGenerator
        Parameters:
        n - the bound on the random number to be returned. Must be positive.
        Returns:
        a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
      • nextLong

        public long nextLong()
        Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. All 264 possible long values should be produced with (approximately) equal probability.
        Specified by:
        nextLong in interface RandomGenerator
        Returns:
        the next pseudorandom, uniformly distributed long value from this random number generator's sequence
      • nextLong

        public long nextLong​(long n)
        Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
        Specified by:
        nextLong in interface RandomGenerator
        Parameters:
        n - the bound on the random number to be returned. Must be positive.
        Returns:
        a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
      • nextBoolean

        public boolean nextBoolean()
        Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
        Specified by:
        nextBoolean in interface RandomGenerator
        Returns:
        the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence
      • nextFloat

        public float nextFloat()
        Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
        Specified by:
        nextFloat in interface RandomGenerator
        Returns:
        the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence
      • nextDouble

        public double nextDouble()
        Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
        Specified by:
        nextDouble in interface RandomGenerator
        Returns:
        the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence
      • nextGaussian

        public double nextGaussian()
        Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
        Specified by:
        nextGaussian in interface RandomGenerator
        Returns:
        the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence