/** * Reseeds the random number generator with the supplied seed. * <p> * Will create and initialize if null. * </p> * * @param seed the seed value to use */ public void reSeed(long seed) { getRandomGenerator().setSeed(seed); }
/** * Reseeds the random number generator with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ public void reSeed() { getRandomGenerator().setSeed(System.currentTimeMillis() + System.identityHashCode(this)); }
/** * Private constructor to allow lazy initialisation of the RNG contained * in the {@link #randomData} instance variable. * * @param binCount number of bins. Must be strictly positive. * @param randomData Random data generator. * @throws NotStrictlyPositiveException if {@code binCount <= 0}. */ private EmpiricalDistribution(int binCount, RandomDataGenerator randomData) { super(randomData.getRandomGenerator()); if (binCount <= 0) { throw new NotStrictlyPositiveException(binCount); } this.binCount = binCount; this.randomData = randomData; binStats = new ArrayList<SummaryStatistics>(); }
/** * Generates a random value from the {@link ZipfDistribution Zipf Distribution}. * * @param numberOfElements the number of elements of the ZipfDistribution * @param exponent the exponent of the ZipfDistribution * @return random value sampled from the Zipf(numberOfElements, exponent) distribution * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public int nextZipf(int numberOfElements, double exponent) throws NotStrictlyPositiveException { return new ZipfDistribution(getRandomGenerator(), numberOfElements, exponent).sample(); }
/** * Generates a random value from the {@link BetaDistribution Beta Distribution}. * * @param alpha first distribution shape parameter * @param beta second distribution shape parameter * @return random value sampled from the beta(alpha, beta) distribution */ public double nextBeta(double alpha, double beta) { return new BetaDistribution(getRandomGenerator(), alpha, beta, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Generates a random value from the {@link ChiSquaredDistribution ChiSquare Distribution}. * * @param df the degrees of freedom of the ChiSquare distribution * @return random value sampled from the ChiSquare(df) distribution */ public double nextChiSquare(double df) { return new ChiSquaredDistribution(getRandomGenerator(), df, ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Generates a random value from the {@link FDistribution F Distribution}. * * @param numeratorDf the numerator degrees of freedom of the F distribution * @param denominatorDf the denominator degrees of freedom of the F distribution * @return random value sampled from the F(numeratorDf, denominatorDf) distribution * @throws NotStrictlyPositiveException if * {@code numeratorDf <= 0} or {@code denominatorDf <= 0}. */ public double nextF(double numeratorDf, double denominatorDf) throws NotStrictlyPositiveException { return new FDistribution(getRandomGenerator(), numeratorDf, denominatorDf, FDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** {@inheritDoc} */ public double nextGaussian(double mu, double sigma) throws NotStrictlyPositiveException { if (sigma <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sigma); } return sigma * getRandomGenerator().nextGaussian() + mu; }
/** * Generates a random value from the {@link TDistribution T Distribution}. * * @param df the degrees of freedom of the T distribution * @return random value from the T(df) distribution * @throws NotStrictlyPositiveException if {@code df <= 0} */ public double nextT(double df) throws NotStrictlyPositiveException { return new TDistribution(getRandomGenerator(), df, TDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Generates a random value from the {@link BinomialDistribution Binomial Distribution}. * * @param numberOfTrials number of trials of the Binomial distribution * @param probabilityOfSuccess probability of success of the Binomial distribution * @return random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution */ public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) { return new BinomialDistribution(getRandomGenerator(), numberOfTrials, probabilityOfSuccess).sample(); }
/** * Generates a random value from the {@link CauchyDistribution Cauchy Distribution}. * * @param median the median of the Cauchy distribution * @param scale the scale parameter of the Cauchy distribution * @return random value sampled from the Cauchy(median, scale) distribution */ public double nextCauchy(double median, double scale) { return new CauchyDistribution(getRandomGenerator(), median, scale, CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** {@inheritDoc} */ public int nextInt(final int lower, final int upper) throws NumberIsTooLargeException { return new UniformIntegerDistribution(getRandomGenerator(), lower, upper).sample(); }
/** * Generates a random value from the {@link WeibullDistribution Weibull Distribution}. * * @param shape the shape parameter of the Weibull distribution * @param scale the scale parameter of the Weibull distribution * @return random value sampled from the Weibull(shape, size) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException { return new WeibullDistribution(getRandomGenerator(), shape, scale, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Generates a random value from the {@link PascalDistribution Pascal Distribution}. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return new PascalDistribution(getRandomGenerator(), r, p).sample(); }
/** * {@inheritDoc} * * <p> * <strong>Algorithm Description</strong>: Uses the Algorithm SA (Ahrens) * from p. 876 in: * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for * sampling from the exponential and normal distributions. * Communications of the ACM, 15, 873-882. * </p> */ public double nextExponential(double mean) throws NotStrictlyPositiveException { return new ExponentialDistribution(getRandomGenerator(), mean, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}. * * @param populationSize the population size of the Hypergeometric distribution * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution * @param sampleSize the sample size of the Hypergeometric distribution * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, * or {@code sampleSize > populationSize}. * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. * @throws NotPositiveException if {@code numberOfSuccesses < 0}. */ public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { return new HypergeometricDistribution(getRandomGenerator(),populationSize, numberOfSuccesses, sampleSize).sample(); }
/** * {@inheritDoc} * <p> * <strong>Algorithm Description</strong>: * <ul><li> For small means, uses simulation of a Poisson process * using Uniform deviates, as described * <a href="http://irmi.epfl.ch/cmos/Pmmi/interactive/rng7.htm"> here.</a> * The Poisson process (and hence value returned) is bounded by 1000 * mean.</li> * * <li> For large means, uses the rejection algorithm described in <br/> * Devroye, Luc. (1981).<i>The Computer Generation of Poisson Random Variables</i> * <strong>Computing</strong> vol. 26 pp. 197-207.</li></ul></p> * @throws NotStrictlyPositiveException if {@code len <= 0} */ public long nextPoisson(double mean) throws NotStrictlyPositiveException { return new PoissonDistribution(getRandomGenerator(), mean, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS).sample(); }
/** * <p>Generates a random value from the * {@link org.apache.commons.math3.distribution.GammaDistribution Gamma Distribution}.</p> * * <p>This implementation uses the following algorithms: </p> * * <p>For 0 < shape < 1: <br/> * Ahrens, J. H. and Dieter, U., <i>Computer methods for * sampling from gamma, beta, Poisson and binomial distributions.</i> * Computing, 12, 223-246, 1974.</p> * * <p>For shape >= 1: <br/> * Marsaglia and Tsang, <i>A Simple Method for Generating * Gamma Variables.</i> ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000.</p> * * @param shape the median of the Gamma distribution * @param scale the scale parameter of the Gamma distribution * @return random value sampled from the Gamma(shape, scale) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { return new GammaDistribution(getRandomGenerator(),shape, scale, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * Computes the empirical distribution using values from the file * in <code>valuesFileURL</code> and <code>binCount</code> bins. * <p> * <code>valuesFileURL</code> must exist and be readable by this process * at runtime.</p> * <p> * This method must be called before using <code>getNext()</code> * with <code>mode = DIGEST_MODE</code></p> * * @param binCount the number of bins used in computing the empirical * distribution * @throws NullArgumentException if the {@code valuesFileURL} has not been set * @throws IOException if an error occurs reading the input file * @throws ZeroException if URL contains no data */ public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException { empiricalDistribution = new EmpiricalDistribution(binCount, randomData.getRandomGenerator()); empiricalDistribution.load(valuesFileURL); mu = empiricalDistribution.getSampleStats().getMean(); sigma = empiricalDistribution.getSampleStats().getStandardDeviation(); }
/** * The within-bin smoothing kernel. Returns a Gaussian distribution * parameterized by {@code bStats}, unless the bin contains only one * observation, in which case a constant distribution is returned. * * @param bStats summary statistics for the bin * @return within-bin kernel parameterized by bStats */ protected RealDistribution getKernel(SummaryStatistics bStats) { if (bStats.getN() == 1 || bStats.getVariance() == 0) { return new ConstantRealDistribution(bStats.getMean()); } else { return new NormalDistribution(randomData.getRandomGenerator(), bStats.getMean(), bStats.getStandardDeviation(), NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } } }