|
Java example source code file (RandomDataImpl.java)
The RandomDataImpl.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.random; import java.io.Serializable; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.util.Collection; import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Generates random deviates and other random data using a {@link RandomGenerator} * instance to generate non-secure data and a {@link java.security.SecureRandom} * instance to provide data for the <code>nextSecureXxx methods. If no * <code>RandomGenerator is provided in the constructor, the default is * to use a {@link Well19937c} generator. To plug in a different * implementation, either implement <code>RandomGenerator directly or * extend {@link AbstractRandomGenerator}. * <p> * Supports reseeding the underlying pseudo-random number generator (PRNG). The * <code>SecurityProvider and * Devroye, Luc. (1981).<i>The Computer Generation of Poisson Random Variables * <strong>Computing vol. 26 pp. 197-207. * 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: * 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}. * @since 2.2 */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { return delegate.nextGamma(shape, scale); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.HypergeometricDistribution Hypergeometric Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @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}. * @since 2.2 */ public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextHypergeometric(populationSize, numberOfSuccesses, sampleSize); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.PascalDistribution Pascal Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @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 * @since 2.2 * @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 delegate.nextPascal(r, p); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.TDistribution T Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the T distribution * @return random value from the T(df) distribution * @since 2.2 * @throws NotStrictlyPositiveException if {@code df <= 0} */ public double nextT(double df) throws NotStrictlyPositiveException { return delegate.nextT(df); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.WeibullDistribution Weibull Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @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 * @since 2.2 * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException { return delegate.nextWeibull(shape, scale); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.ZipfDistribution Zipf Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @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 * @since 2.2 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public int nextZipf(int numberOfElements, double exponent) throws NotStrictlyPositiveException { return delegate.nextZipf(numberOfElements, exponent); } /** * 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) { delegate.reSeed(seed); } /** * Reseeds the secure random number generator with the current time in * milliseconds. * <p> * Will create and initialize if null. * </p> */ public void reSeedSecure() { delegate.reSeedSecure(); } /** * Reseeds the secure random number generator with the supplied seed. * <p> * Will create and initialize if null. * </p> * * @param seed * the seed value to use */ public void reSeedSecure(long seed) { delegate.reSeedSecure(seed); } /** * Reseeds the random number generator with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ public void reSeed() { delegate.reSeed(); } /** * Sets the PRNG algorithm for the underlying SecureRandom instance using * the Security Provider API. The Security Provider API is defined in <a * href = * "http://java.sun.com/j2se/1.3/docs/guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference.</a> * <p> * <strong>USAGE NOTE: This method carries significant * overhead and may take several seconds to execute. * </p> * * @param algorithm * the name of the PRNG algorithm * @param provider * the name of the provider * @throws NoSuchAlgorithmException * if the specified algorithm is not available * @throws NoSuchProviderException * if the specified provider is not installed */ public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { delegate.setSecureAlgorithm(algorithm, provider); } /** * {@inheritDoc} * * <p> * Uses a 2-cycle permutation shuffle. The shuffling process is described <a * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html"> * here</a>. * </p> */ public int[] nextPermutation(int n, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextPermutation(n, k); } /** * {@inheritDoc} * * <p> * <strong>Algorithm Description: Uses a 2-cycle permutation * shuffle to generate a random permutation of <code>c.size() and * then returns the elements whose indexes correspond to the elements of the * generated permutation. This technique is described, and proven to * generate random samples <a * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html"> * here</a> * </p> */ public Object[] nextSample(Collection<?> c, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextSample(c, k); } /** * Generate a random deviate from the given distribution using the * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method. * * @param distribution Continuous distribution to generate a random value from * @return a random value sampled from the given distribution * @throws MathIllegalArgumentException if the underlynig distribution throws one * @since 2.2 * @deprecated use the distribution's sample() method */ @Deprecated public double nextInversionDeviate(RealDistribution distribution) throws MathIllegalArgumentException { return distribution.inverseCumulativeProbability(nextUniform(0, 1)); } /** * Generate a random deviate from the given distribution using the * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method. * * @param distribution Integer distribution to generate a random value from * @return a random value sampled from the given distribution * @throws MathIllegalArgumentException if the underlynig distribution throws one * @since 2.2 * @deprecated use the distribution's sample() method */ @Deprecated public int nextInversionDeviate(IntegerDistribution distribution) throws MathIllegalArgumentException { return distribution.inverseCumulativeProbability(nextUniform(0, 1)); } } Other Java examples (source code examples)Here is a short list of links related to this Java RandomDataImpl.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.