index
int64
0
0
repo_id
stringlengths
9
205
file_path
stringlengths
31
246
content
stringlengths
1
12.2M
__index_level_0__
int64
0
10k
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.distribution.InternalUtils.FactorialLog; /** * Sampler for the <a href="http://mathworld.wolfram.com/PoissonDistribution.html">Poisson distribution</a>. * * <ul> * <li> * For large means, we use the rejection algorithm described in * <blockquote> * Devroye, Luc. (1981).<i>The Computer Generation of Poisson Random Variables</i><br> * <strong>Computing</strong> vol. 26 pp. 197-207. * </blockquote> * </li> * </ul> * * <p>This sampler is suitable for {@code mean >= 40}.</p> * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextLong()} * <li>{@link UniformRandomProvider#nextDouble()} * </ul> * * @since 1.1 */ public class LargeMeanPoissonSampler implements SharedStateDiscreteSampler { /** Upper bound to avoid truncation. */ private static final double MAX_MEAN = 0.5 * Integer.MAX_VALUE; /** Class to compute {@code log(n!)}. This has no cached values. */ private static final InternalUtils.FactorialLog NO_CACHE_FACTORIAL_LOG; /** Used when there is no requirement for a small mean Poisson sampler. */ private static final SharedStateDiscreteSampler NO_SMALL_MEAN_POISSON_SAMPLER = new SharedStateDiscreteSampler() { @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { // No requirement for RNG return this; } @Override public int sample() { // No Poisson sample return 0; } }; static { // Create without a cache. NO_CACHE_FACTORIAL_LOG = FactorialLog.create(); } /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** Exponential. */ private final SharedStateContinuousSampler exponential; /** Gaussian. */ private final SharedStateContinuousSampler gaussian; /** Local class to compute {@code log(n!)}. This may have cached values. */ private final InternalUtils.FactorialLog factorialLog; // Working values /** Algorithm constant: {@code Math.floor(mean)}. */ private final double lambda; /** Algorithm constant: {@code Math.log(lambda)}. */ private final double logLambda; /** Algorithm constant: {@code factorialLog((int) lambda)}. */ private final double logLambdaFactorial; /** Algorithm constant: {@code Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1))}. */ private final double delta; /** Algorithm constant: {@code delta / 2}. */ private final double halfDelta; /** Algorithm constant: {@code Math.sqrt(lambda + halfDelta)}. */ private final double sqrtLambdaPlusHalfDelta; /** Algorithm constant: {@code 2 * lambda + delta}. */ private final double twolpd; /** * Algorithm constant: {@code a1 / aSum}. * <ul> * <li>{@code a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1)}</li> * <li>{@code aSum = a1 + a2 + 1}</li> * </ul> */ private final double p1; /** * Algorithm constant: {@code a2 / aSum}. * <ul> * <li>{@code a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd)}</li> * <li>{@code aSum = a1 + a2 + 1}</li> * </ul> */ private final double p2; /** Algorithm constant: {@code 1 / (8 * lambda)}. */ private final double c1; /** The internal Poisson sampler for the lambda fraction. */ private final SharedStateDiscreteSampler smallMeanPoissonSampler; /** * @param rng Generator of uniformly distributed random numbers. * @param mean Mean. * @throws IllegalArgumentException if {@code mean < 1} or * {@code mean > 0.5 *} {@link Integer#MAX_VALUE}. */ public LargeMeanPoissonSampler(UniformRandomProvider rng, double mean) { if (mean < 1) { throw new IllegalArgumentException("mean is not >= 1: " + mean); } // The algorithm is not valid if Math.floor(mean) is not an integer. if (mean > MAX_MEAN) { throw new IllegalArgumentException("mean " + mean + " > " + MAX_MEAN); } this.rng = rng; gaussian = ZigguratSampler.NormalizedGaussian.of(rng); exponential = ZigguratSampler.Exponential.of(rng); // Plain constructor uses the uncached function. factorialLog = NO_CACHE_FACTORIAL_LOG; // Cache values used in the algorithm lambda = Math.floor(mean); logLambda = Math.log(lambda); logLambdaFactorial = getFactorialLog((int) lambda); delta = Math.sqrt(lambda * Math.log(32 * lambda / Math.PI + 1)); halfDelta = delta / 2; sqrtLambdaPlusHalfDelta = Math.sqrt(lambda + halfDelta); twolpd = 2 * lambda + delta; c1 = 1 / (8 * lambda); final double a1 = Math.sqrt(Math.PI * twolpd) * Math.exp(c1); final double a2 = (twolpd / delta) * Math.exp(-delta * (1 + delta) / twolpd); final double aSum = a1 + a2 + 1; p1 = a1 / aSum; p2 = a2 / aSum; // The algorithm requires a Poisson sample from the remaining lambda fraction. final double lambdaFractional = mean - lambda; smallMeanPoissonSampler = (lambdaFractional < Double.MIN_VALUE) ? NO_SMALL_MEAN_POISSON_SAMPLER : // Not used. KempSmallMeanPoissonSampler.of(rng, lambdaFractional); } /** * Instantiates a sampler using a precomputed state. * * @param rng Generator of uniformly distributed random numbers. * @param state The state for {@code lambda = (int)Math.floor(mean)}. * @param lambdaFractional The lambda fractional value * ({@code mean - (int)Math.floor(mean))}. * @throws IllegalArgumentException * if {@code lambdaFractional < 0 || lambdaFractional >= 1}. */ LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSamplerState state, double lambdaFractional) { if (lambdaFractional < 0 || lambdaFractional >= 1) { throw new IllegalArgumentException( "lambdaFractional must be in the range 0 (inclusive) to 1 (exclusive): " + lambdaFractional); } this.rng = rng; gaussian = ZigguratSampler.NormalizedGaussian.of(rng); exponential = ZigguratSampler.Exponential.of(rng); // Plain constructor uses the uncached function. factorialLog = NO_CACHE_FACTORIAL_LOG; // Use the state to initialize the algorithm lambda = state.getLambdaRaw(); logLambda = state.getLogLambda(); logLambdaFactorial = state.getLogLambdaFactorial(); delta = state.getDelta(); halfDelta = state.getHalfDelta(); sqrtLambdaPlusHalfDelta = state.getSqrtLambdaPlusHalfDelta(); twolpd = state.getTwolpd(); p1 = state.getP1(); p2 = state.getP2(); c1 = state.getC1(); // The algorithm requires a Poisson sample from the remaining lambda fraction. smallMeanPoissonSampler = (lambdaFractional < Double.MIN_VALUE) ? NO_SMALL_MEAN_POISSON_SAMPLER : // Not used. KempSmallMeanPoissonSampler.of(rng, lambdaFractional); } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ private LargeMeanPoissonSampler(UniformRandomProvider rng, LargeMeanPoissonSampler source) { this.rng = rng; gaussian = source.gaussian.withUniformRandomProvider(rng); exponential = source.exponential.withUniformRandomProvider(rng); // Reuse the cache factorialLog = source.factorialLog; lambda = source.lambda; logLambda = source.logLambda; logLambdaFactorial = source.logLambdaFactorial; delta = source.delta; halfDelta = source.halfDelta; sqrtLambdaPlusHalfDelta = source.sqrtLambdaPlusHalfDelta; twolpd = source.twolpd; p1 = source.p1; p2 = source.p2; c1 = source.c1; // Share the state of the small sampler smallMeanPoissonSampler = source.smallMeanPoissonSampler.withUniformRandomProvider(rng); } /** {@inheritDoc} */ @Override public int sample() { // This will never be null. It may be a no-op delegate that returns zero. final int y2 = smallMeanPoissonSampler.sample(); double x; double y; double v; int a; double t; double qr; double qa; while (true) { // Step 1: final double u = rng.nextDouble(); if (u <= p1) { // Step 2: final double n = gaussian.sample(); x = n * sqrtLambdaPlusHalfDelta - 0.5d; if (x > delta || x < -lambda) { continue; } y = x < 0 ? Math.floor(x) : Math.ceil(x); final double e = exponential.sample(); v = -e - 0.5 * n * n + c1; } else { // Step 3: if (u > p1 + p2) { y = lambda; break; } x = delta + (twolpd / delta) * exponential.sample(); y = Math.ceil(x); v = -exponential.sample() - delta * (x + 1) / twolpd; } // The Squeeze Principle // Step 4.1: a = x < 0 ? 1 : 0; t = y * (y + 1) / (2 * lambda); // Step 4.2 if (v < -t && a == 0) { y = lambda + y; break; } // Step 4.3: qr = t * ((2 * y + 1) / (6 * lambda) - 1); qa = qr - (t * t) / (3 * (lambda + a * (y + 1))); // Step 4.4: if (v < qa) { y = lambda + y; break; } // Step 4.5: if (v > qr) { continue; } // Step 4.6: if (v < y * logLambda - getFactorialLog((int) (y + lambda)) + logLambdaFactorial) { y = lambda + y; break; } } return (int) Math.min(y2 + (long) y, Integer.MAX_VALUE); } /** * Compute the natural logarithm of the factorial of {@code n}. * * @param n Argument. * @return {@code log(n!)} * @throws IllegalArgumentException if {@code n < 0}. */ private double getFactorialLog(int n) { return factorialLog.value(n); } /** {@inheritDoc} */ @Override public String toString() { return "Large Mean Poisson deviate [" + rng.toString() + "]"; } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new LargeMeanPoissonSampler(rng, this); } /** * Creates a new Poisson distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param mean Mean. * @return the sampler * @throws IllegalArgumentException if {@code mean < 1} or {@code mean > 0.5 *} * {@link Integer#MAX_VALUE}. * @since 1.3 */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double mean) { return new LargeMeanPoissonSampler(rng, mean); } /** * Gets the initialisation state of the sampler. * * <p>The state is computed using an integer {@code lambda} value of * {@code lambda = (int)Math.floor(mean)}. * * <p>The state will be suitable for reconstructing a new sampler with a mean * in the range {@code lambda <= mean < lambda+1} using * {@link #LargeMeanPoissonSampler(UniformRandomProvider, LargeMeanPoissonSamplerState, double)}. * * @return the state */ LargeMeanPoissonSamplerState getState() { return new LargeMeanPoissonSamplerState(lambda, logLambda, logLambdaFactorial, delta, halfDelta, sqrtLambdaPlusHalfDelta, twolpd, p1, p2, c1); } /** * Encapsulate the state of the sampler. The state is valid for construction of * a sampler in the range {@code lambda <= mean < lambda+1}. * * <p>This class is immutable. * * @see #getLambda() */ static final class LargeMeanPoissonSamplerState { /** Algorithm constant {@code lambda}. */ private final double lambda; /** Algorithm constant {@code logLambda}. */ private final double logLambda; /** Algorithm constant {@code logLambdaFactorial}. */ private final double logLambdaFactorial; /** Algorithm constant {@code delta}. */ private final double delta; /** Algorithm constant {@code halfDelta}. */ private final double halfDelta; /** Algorithm constant {@code sqrtLambdaPlusHalfDelta}. */ private final double sqrtLambdaPlusHalfDelta; /** Algorithm constant {@code twolpd}. */ private final double twolpd; /** Algorithm constant {@code p1}. */ private final double p1; /** Algorithm constant {@code p2}. */ private final double p2; /** Algorithm constant {@code c1}. */ private final double c1; /** * Creates the state. * * <p>The state is valid for construction of a sampler in the range * {@code lambda <= mean < lambda+1} where {@code lambda} is an integer. * * @param lambda the lambda * @param logLambda the log lambda * @param logLambdaFactorial the log lambda factorial * @param delta the delta * @param halfDelta the half delta * @param sqrtLambdaPlusHalfDelta the sqrt(lambda+half delta) * @param twolpd the two lambda plus delta * @param p1 the p1 constant * @param p2 the p2 constant * @param c1 the c1 constant */ LargeMeanPoissonSamplerState(double lambda, double logLambda, double logLambdaFactorial, double delta, double halfDelta, double sqrtLambdaPlusHalfDelta, double twolpd, double p1, double p2, double c1) { this.lambda = lambda; this.logLambda = logLambda; this.logLambdaFactorial = logLambdaFactorial; this.delta = delta; this.halfDelta = halfDelta; this.sqrtLambdaPlusHalfDelta = sqrtLambdaPlusHalfDelta; this.twolpd = twolpd; this.p1 = p1; this.p2 = p2; this.c1 = c1; } /** * Get the lambda value for the state. * * <p>Equal to {@code floor(mean)} for a Poisson sampler. * @return the lambda value */ int getLambda() { return (int) getLambdaRaw(); } /** * @return algorithm constant {@code lambda} */ double getLambdaRaw() { return lambda; } /** * @return algorithm constant {@code logLambda} */ double getLogLambda() { return logLambda; } /** * @return algorithm constant {@code logLambdaFactorial} */ double getLogLambdaFactorial() { return logLambdaFactorial; } /** * @return algorithm constant {@code delta} */ double getDelta() { return delta; } /** * @return algorithm constant {@code halfDelta} */ double getHalfDelta() { return halfDelta; } /** * @return algorithm constant {@code sqrtLambdaPlusHalfDelta} */ double getSqrtLambdaPlusHalfDelta() { return sqrtLambdaPlusHalfDelta; } /** * @return algorithm constant {@code twolpd} */ double getTwolpd() { return twolpd; } /** * @return algorithm constant {@code p1} */ double getP1() { return p1; } /** * @return algorithm constant {@code p2} */ double getP2() { return p2; } /** * @return algorithm constant {@code c1} */ double getC1() { return c1; } } }
3,000
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteInverseCumulativeProbabilityFunction.java
/* * 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.rng.sampling.distribution; /** * Interface for a discrete distribution that can be sampled using * the <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. * * @since 1.0 */ public interface DiscreteInverseCumulativeProbabilityFunction { /** * Computes the quantile function of the distribution. * For a random variable {@code X} distributed according to this distribution, * the returned value is * <ul> * <li>\( \inf_{x \in \mathcal{Z}} P(X \le x) \ge p \) for \( 0 \lt p \le 1 \)</li> * <li>\( \inf_{x \in \mathcal{Z}} P(X \le x) \gt 0 \) for \( p = 0 \)</li> * </ul> * * @param p Cumulative probability. * @return the smallest {@code p}-quantile of the distribution * (largest 0-quantile for {@code p = 0}). * @throws IllegalArgumentException if {@code p < 0} or {@code p > 1}. */ int inverseCumulativeProbability(double p); }
3,001
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DirichletSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.SharedStateObjectSampler; /** * Sampling from a <a href="https://en.wikipedia.org/wiki/Dirichlet_distribution">Dirichlet * distribution</a>. * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextLong()} * <li>{@link UniformRandomProvider#nextDouble()} * </ul> * * @since 1.4 */ public abstract class DirichletSampler implements SharedStateObjectSampler<double[]> { /** The minimum number of categories. */ private static final int MIN_CATGEORIES = 2; /** RNG (used for the toString() method). */ private final UniformRandomProvider rng; /** * Sample from a Dirichlet distribution with different concentration parameters * for each category. */ private static final class GeneralDirichletSampler extends DirichletSampler { /** Samplers for each category. */ private final SharedStateContinuousSampler[] samplers; /** * @param rng Generator of uniformly distributed random numbers. * @param samplers Samplers for each category. */ GeneralDirichletSampler(UniformRandomProvider rng, SharedStateContinuousSampler[] samplers) { super(rng); // Array is stored directly as it is generated within the DirichletSampler class this.samplers = samplers; } @Override protected int getK() { return samplers.length; } @Override protected double nextGamma(int i) { return samplers[i].sample(); } @Override public GeneralDirichletSampler withUniformRandomProvider(UniformRandomProvider rng) { final SharedStateContinuousSampler[] newSamplers = new SharedStateContinuousSampler[samplers.length]; for (int i = 0; i < newSamplers.length; i++) { newSamplers[i] = samplers[i].withUniformRandomProvider(rng); } return new GeneralDirichletSampler(rng, newSamplers); } } /** * Sample from a symmetric Dirichlet distribution with the same concentration parameter * for each category. */ private static final class SymmetricDirichletSampler extends DirichletSampler { /** Number of categories. */ private final int k; /** Sampler for the categories. */ private final SharedStateContinuousSampler sampler; /** * @param rng Generator of uniformly distributed random numbers. * @param k Number of categories. * @param sampler Sampler for the categories. */ SymmetricDirichletSampler(UniformRandomProvider rng, int k, SharedStateContinuousSampler sampler) { super(rng); this.k = k; this.sampler = sampler; } @Override protected int getK() { return k; } @Override protected double nextGamma(int i) { return sampler.sample(); } @Override public SymmetricDirichletSampler withUniformRandomProvider(UniformRandomProvider rng) { return new SymmetricDirichletSampler(rng, k, sampler.withUniformRandomProvider(rng)); } } /** * @param rng Generator of uniformly distributed random numbers. */ private DirichletSampler(UniformRandomProvider rng) { this.rng = rng; } /** {@inheritDoc} */ @Override public String toString() { return "Dirichlet deviate [" + rng.toString() + "]"; } @Override public double[] sample() { // Create Gamma(alpha_i, 1) deviates for all alpha final double[] y = new double[getK()]; double norm = 0; for (int i = 0; i < y.length; i++) { final double yi = nextGamma(i); norm += yi; y[i] = yi; } // Normalize by dividing by the sum of the samples norm = 1.0 / norm; // Detect an invalid normalization, e.g. cases of all zero samples if (!isNonZeroPositiveFinite(norm)) { // Sample again using recursion. // A stack overflow due to a broken RNG will eventually occur // rather than the alternative which is an infinite loop. return sample(); } // Normalise for (int i = 0; i < y.length; i++) { y[i] *= norm; } return y; } /** * Gets the number of categories. * * @return k */ protected abstract int getK(); /** * Create a gamma sample for the given category. * * @param category Category. * @return the sample */ protected abstract double nextGamma(int category); /** {@inheritDoc} */ // Redeclare the signature to return a DirichletSampler not a SharedStateObjectSampler<double[]> @Override public abstract DirichletSampler withUniformRandomProvider(UniformRandomProvider rng); /** * Creates a new Dirichlet distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Concentration parameters. * @return the sampler * @throws IllegalArgumentException if the number of concentration parameters * is less than 2; or if any concentration parameter is not strictly positive. */ public static DirichletSampler of(UniformRandomProvider rng, double... alpha) { validateNumberOfCategories(alpha.length); final SharedStateContinuousSampler[] samplers = new SharedStateContinuousSampler[alpha.length]; for (int i = 0; i < samplers.length; i++) { samplers[i] = createSampler(rng, alpha[i]); } return new GeneralDirichletSampler(rng, samplers); } /** * Creates a new symmetric Dirichlet distribution sampler using the same concentration * parameter for each category. * * @param rng Generator of uniformly distributed random numbers. * @param k Number of categories. * @param alpha Concentration parameter. * @return the sampler * @throws IllegalArgumentException if the number of categories is * less than 2; or if the concentration parameter is not strictly positive. */ public static DirichletSampler symmetric(UniformRandomProvider rng, int k, double alpha) { validateNumberOfCategories(k); final SharedStateContinuousSampler sampler = createSampler(rng, alpha); return new SymmetricDirichletSampler(rng, k, sampler); } /** * Validate the number of categories. * * @param k Number of categories. * @throws IllegalArgumentException if the number of categories is * less than 2. */ private static void validateNumberOfCategories(int k) { if (k < MIN_CATGEORIES) { throw new IllegalArgumentException("Invalid number of categories: " + k); } } /** * Creates a gamma sampler for a category with the given concentration parameter. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Concentration parameter. * @return the sampler * @throws IllegalArgumentException if the concentration parameter is not strictly positive. */ private static SharedStateContinuousSampler createSampler(UniformRandomProvider rng, double alpha) { // Negation of logic will detect NaN if (!isNonZeroPositiveFinite(alpha)) { throw new IllegalArgumentException("Invalid concentration: " + alpha); } // Create a Gamma(shape=alpha, scale=1) sampler. if (alpha == 1) { // Special case // Gamma(shape=1, scale=1) == Exponential(mean=1) return ZigguratSampler.Exponential.of(rng); } return AhrensDieterMarsagliaTsangGammaSampler.of(rng, alpha, 1); } /** * Return true if the value is non-zero, positive and finite. * * @param x Value. * @return true if non-zero positive finite */ private static boolean isNonZeroPositiveFinite(double x) { return x > 0 && x < Double.POSITIVE_INFINITY; } }
3,002
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/TSampler.java
/* * 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.rng.sampling.distribution; import java.util.function.DoubleUnaryOperator; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from a T distribution. * * <p>Uses Bailey's algorithm for t-distribution sampling:</p> * * <blockquote> * <pre> * Bailey, R. W. (1994) * "Polar Generation of Random Variates with the t-Distribution." * Mathematics of Computation 62, 779-781. * </pre> * </blockquote> * * <p>Sampling uses {@link UniformRandomProvider#nextLong()}.</p> * * @see <a href="https://en.wikipedia.org/wiki/Student%27s_t-distribution">Student&#39;s T distribution (wikipedia)</a> * @see <a href="https://doi.org/10.2307/2153537">Mathematics of Computation, 62, 779-781</a> * @since 1.5 */ public abstract class TSampler implements SharedStateContinuousSampler { /** Threshold for huge degrees of freedom. Above this value the CDF of the t distribution * matches the normal distribution. Value is 2/eps (where eps is the machine epsilon) * or approximately 9.0e15. */ private static final double HUGE_DF = 0x1.0p53; /** Source of randomness. */ private final UniformRandomProvider rng; /** * Sample from a t-distribution using Bailey's algorithm. */ private static final class StudentsTSampler extends TSampler { /** Threshold for large degrees of freedom. */ private static final double LARGE_DF = 25; /** The multiplier to convert the least significant 53-bits of a {@code long} to a * uniform {@code double}. */ private static final double DOUBLE_MULTIPLIER = 0x1.0p-53; /** Degrees of freedom. */ private final double df; /** Function to compute pow(x, -2/v) - 1, where v = degrees of freedom. */ private final DoubleUnaryOperator powm1; /** * @param rng Generator of uniformly distributed random numbers. * @param v Degrees of freedom. */ StudentsTSampler(UniformRandomProvider rng, double v) { super(rng); df = v; // The sampler requires pow(w, -2/v) - 1 with // 0 <= w <= 1; Expected(w) = sqrt(0.5). // When the exponent is small then pow(x, y) -> 1. // This affects large degrees of freedom. final double exponent = -2 / v; powm1 = v > LARGE_DF ? x -> Math.expm1(Math.log(x) * exponent) : x -> Math.pow(x, exponent) - 1; } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ private StudentsTSampler(UniformRandomProvider rng, StudentsTSampler source) { super(rng); df = source.df; powm1 = source.powm1; } /** {@inheritDoc} */ @Override public double sample() { // Require u and v in [0, 1] and a random sign. // Create u in (0, 1] to avoid generating nan // from u*u/w (0/0) or r2*c2 (inf*0). final double u = InternalUtils.makeNonZeroDouble(nextLong()); final double v = makeSignedDouble(nextLong()); final double w = u * u + v * v; if (w > 1) { // Rejection frequency = 1 - pi/4 = 0.215. // Recursion will generate stack overflow given a broken RNG // and avoids an infinite loop. return sample(); } // Sidestep a square-root calculation. final double c2 = u * u / w; final double r2 = df * powm1.applyAsDouble(w); // Choose sign at random from the sign of v. return Math.copySign(Math.sqrt(r2 * c2), v); } /** {@inheritDoc} */ @Override public StudentsTSampler withUniformRandomProvider(UniformRandomProvider rng) { return new StudentsTSampler(rng, this); } /** * Creates a signed double in the range {@code [-1, 1)}. The magnitude is sampled evenly * from the 2<sup>54</sup> dyadic rationals in the range. * * <p>Note: This method will not return samples for both -0.0 and 0.0. * * @param bits the bits * @return the double */ private static double makeSignedDouble(long bits) { // As per o.a.c.rng.core.utils.NumberFactory.makeDouble(long) but using a signed // shift of 10 in place of an unsigned shift of 11. // Use the upper 54 bits on the assumption they are more random. // The sign bit is maintained by the signed shift. // The next 53 bits generates a magnitude in the range [0, 2^53) or [-2^53, 0). return (bits >> 10) * DOUBLE_MULTIPLIER; } } /** * Sample from a t-distribution using a normal distribution. * This is used when the degrees of freedom is extremely large (e.g. {@code > 1e16}). */ private static final class NormalTSampler extends TSampler { /** Underlying normalized Gaussian sampler. */ private final NormalizedGaussianSampler sampler; /** * @param rng Generator of uniformly distributed random numbers. */ NormalTSampler(UniformRandomProvider rng) { super(rng); this.sampler = ZigguratSampler.NormalizedGaussian.of(rng); } /** {@inheritDoc} */ @Override public double sample() { return sampler.sample(); } /** {@inheritDoc} */ @Override public NormalTSampler withUniformRandomProvider(UniformRandomProvider rng) { return new NormalTSampler(rng); } } /** * @param rng Generator of uniformly distributed random numbers. */ TSampler(UniformRandomProvider rng) { this.rng = rng; } /** {@inheritDoc} */ // Redeclare the signature to return a TSampler not a SharedStateContinuousSampler @Override public abstract TSampler withUniformRandomProvider(UniformRandomProvider rng); /** * Generates a {@code long} value. * Used by algorithm implementations without exposing access to the RNG. * * @return the next random value */ long nextLong() { return rng.nextLong(); } /** {@inheritDoc} */ @Override public String toString() { return "Student's t deviate [" + rng.toString() + "]"; } /** * Create a new t distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param degreesOfFreedom Degrees of freedom. * @return the sampler * @throws IllegalArgumentException if {@code degreesOfFreedom <= 0} */ public static TSampler of(UniformRandomProvider rng, double degreesOfFreedom) { if (degreesOfFreedom > HUGE_DF) { return new NormalTSampler(rng); } else if (degreesOfFreedom > 0) { return new StudentsTSampler(rng, degreesOfFreedom); } else { // df <= 0 or nan throw new IllegalArgumentException( "degrees of freedom is not strictly positive: " + degreesOfFreedom); } } }
3,003
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Sampler for the <a href="http://mathworld.wolfram.com/PoissonDistribution.html">Poisson distribution</a>. * * <ul> * <li> * For small means, a Poisson process is simulated using uniform deviates, as described in * <blockquote> * Knuth (1969). <i>Seminumerical Algorithms</i>. The Art of Computer Programming, * Volume 2. Chapter 3.4.1.F.3 Important integer-valued distributions: The Poisson distribution. * Addison Wesley. * </blockquote> * The Poisson process (and hence, the returned value) is bounded by {@code 1000 * mean}. * </li> * <li> * For large means, we use the rejection algorithm described in * <blockquote> * Devroye, Luc. (1981). <i>The Computer Generation of Poisson Random Variables</i><br> * <strong>Computing</strong> vol. 26 pp. 197-207. * </blockquote> * </li> * </ul> * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextDouble()} * <li>{@link UniformRandomProvider#nextLong()} (large means only) * </ul> * * @since 1.0 */ public class PoissonSampler extends SamplerBase implements SharedStateDiscreteSampler { /** * Value for switching sampling algorithm. * * <p>Package scope for the {@link PoissonSamplerCache}. */ static final double PIVOT = 40; /** The internal Poisson sampler. */ private final SharedStateDiscreteSampler poissonSamplerDelegate; /** * This instance delegates sampling. Use the factory method * {@link #of(UniformRandomProvider, double)} to create an optimal sampler. * * @param rng Generator of uniformly distributed random numbers. * @param mean Mean. * @throws IllegalArgumentException if {@code mean <= 0} or {@code mean > 0.5 *} * {@link Integer#MAX_VALUE}. */ public PoissonSampler(UniformRandomProvider rng, double mean) { super(null); // Delegate all work to specialised samplers. poissonSamplerDelegate = of(rng, mean); } /** {@inheritDoc} */ @Override public int sample() { return poissonSamplerDelegate.sample(); } /** {@inheritDoc} */ @Override public String toString() { return poissonSamplerDelegate.toString(); } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { // Direct return of the optimised sampler return poissonSamplerDelegate.withUniformRandomProvider(rng); } /** * Creates a new Poisson distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param mean Mean. * @return the sampler * @throws IllegalArgumentException if {@code mean <= 0} or {@code mean > 0.5 *} * {@link Integer#MAX_VALUE}. * @since 1.3 */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double mean) { // Each sampler should check the input arguments. return mean < PIVOT ? SmallMeanPoissonSampler.of(rng, mean) : LargeMeanPoissonSampler.of(rng, mean); } }
3,004
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Implementation of the <a href="https://en.wikipedia.org/wiki/Zipf's_law">Zipf distribution</a>. * * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> * * @since 1.0 */ public class RejectionInversionZipfSampler extends SamplerBase implements SharedStateDiscreteSampler { /** The implementation of the sample method. */ private final SharedStateDiscreteSampler delegate; /** * Implements the rejection-inversion method for the Zipf distribution. */ private static class RejectionInversionZipfSamplerImpl implements SharedStateDiscreteSampler { /** Threshold below which Taylor series will be used. */ private static final double TAYLOR_THRESHOLD = 1e-8; /** 1/2. */ private static final double F_1_2 = 0.5; /** 1/3. */ private static final double F_1_3 = 1d / 3; /** 1/4. */ private static final double F_1_4 = 0.25; /** Number of elements. */ private final int numberOfElements; /** Exponent parameter of the distribution. */ private final double exponent; /** {@code hIntegral(1.5) - 1}. */ private final double hIntegralX1; /** {@code hIntegral(numberOfElements + 0.5)}. */ private final double hIntegralNumberOfElements; /** {@code hIntegralX1 - hIntegralNumberOfElements}. */ private final double r; /** {@code 2 - hIntegralInverse(hIntegral(2.5) - h(2)}. */ private final double s; /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** * @param rng Generator of uniformly distributed random numbers. * @param numberOfElements Number of elements (must be {@code > 0}). * @param exponent Exponent (must be {@code > 0}). */ RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, int numberOfElements, double exponent) { this.rng = rng; this.numberOfElements = numberOfElements; this.exponent = exponent; this.hIntegralX1 = hIntegral(1.5) - 1; this.hIntegralNumberOfElements = hIntegral(numberOfElements + F_1_2); this.r = hIntegralX1 - hIntegralNumberOfElements; this.s = 2 - hIntegralInverse(hIntegral(2.5) - h(2)); } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ private RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, RejectionInversionZipfSamplerImpl source) { this.rng = rng; this.numberOfElements = source.numberOfElements; this.exponent = source.exponent; this.hIntegralX1 = source.hIntegralX1; this.hIntegralNumberOfElements = source.hIntegralNumberOfElements; this.r = source.r; this.s = source.s; } @Override public int sample() { // The paper describes an algorithm for exponents larger than 1 // (Algorithm ZRI). // The original method uses // H(x) = (v + x)^(1 - q) / (1 - q) // as the integral of the hat function. // This function is undefined for q = 1, which is the reason for // the limitation of the exponent. // If instead the integral function // H(x) = ((v + x)^(1 - q) - 1) / (1 - q) // is used, // for which a meaningful limit exists for q = 1, the method works // for all positive exponents. // The following implementation uses v = 0 and generates integral // number in the range [1, numberOfElements]. // This is different to the original method where v is defined to // be positive and numbers are taken from [0, i_max]. // This explains why the implementation looks slightly different. while (true) { final double u = hIntegralNumberOfElements + rng.nextDouble() * r; // u is uniformly distributed in (hIntegralX1, hIntegralNumberOfElements] final double x = hIntegralInverse(u); int k = (int) (x + F_1_2); // Limit k to the range [1, numberOfElements] if it would be outside // due to numerical inaccuracies. if (k < 1) { k = 1; } else if (k > numberOfElements) { k = numberOfElements; } // Here, the distribution of k is given by: // // P(k = 1) = C * (hIntegral(1.5) - hIntegralX1) = C // P(k = m) = C * (hIntegral(m + 1/2) - hIntegral(m - 1/2)) for m >= 2 // // where C = 1 / (hIntegralNumberOfElements - hIntegralX1) if (k - x <= s || u >= hIntegral(k + F_1_2) - h(k)) { // Case k = 1: // // The right inequality is always true, because replacing k by 1 gives // u >= hIntegral(1.5) - h(1) = hIntegralX1 and u is taken from // (hIntegralX1, hIntegralNumberOfElements]. // // Therefore, the acceptance rate for k = 1 is P(accepted | k = 1) = 1 // and the probability that 1 is returned as random value is // P(k = 1 and accepted) = P(accepted | k = 1) * P(k = 1) = C = C / 1^exponent // // Case k >= 2: // // The left inequality (k - x <= s) is just a short cut // to avoid the more expensive evaluation of the right inequality // (u >= hIntegral(k + 0.5) - h(k)) in many cases. // // If the left inequality is true, the right inequality is also true: // Theorem 2 in the paper is valid for all positive exponents, because // the requirements h'(x) = -exponent/x^(exponent + 1) < 0 and // (-1/hInverse'(x))'' = (1+1/exponent) * x^(1/exponent-1) >= 0 // are both fulfilled. // Therefore, f(x) = x - hIntegralInverse(hIntegral(x + 0.5) - h(x)) // is a non-decreasing function. If k - x <= s holds, // k - x <= s + f(k) - f(2) is obviously also true which is equivalent to // -x <= -hIntegralInverse(hIntegral(k + 0.5) - h(k)), // -hIntegralInverse(u) <= -hIntegralInverse(hIntegral(k + 0.5) - h(k)), // and finally u >= hIntegral(k + 0.5) - h(k). // // Hence, the right inequality determines the acceptance rate: // P(accepted | k = m) = h(m) / (hIntegrated(m+1/2) - hIntegrated(m-1/2)) // The probability that m is returned is given by // P(k = m and accepted) = P(accepted | k = m) * P(k = m) = C * h(m) = C / m^exponent. // // In both cases the probabilities are proportional to the probability mass function // of the Zipf distribution. return k; } } } /** {@inheritDoc} */ @Override public String toString() { return "Rejection inversion Zipf deviate [" + rng.toString() + "]"; } @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new RejectionInversionZipfSamplerImpl(rng, this); } /** * {@code H(x)} is defined as * <ul> * <li>{@code (x^(1 - exponent) - 1) / (1 - exponent)}, if {@code exponent != 1}</li> * <li>{@code log(x)}, if {@code exponent == 1}</li> * </ul> * H(x) is an integral function of h(x), the derivative of H(x) is h(x). * * @param x Free parameter. * @return {@code H(x)}. */ private double hIntegral(final double x) { final double logX = Math.log(x); return helper2((1 - exponent) * logX) * logX; } /** * {@code h(x) = 1 / x^exponent}. * * @param x Free parameter. * @return {@code h(x)}. */ private double h(final double x) { return Math.exp(-exponent * Math.log(x)); } /** * The inverse function of {@code H(x)}. * * @param x Free parameter * @return y for which {@code H(y) = x}. */ private double hIntegralInverse(final double x) { double t = x * (1 - exponent); if (t < -1) { // Limit value to the range [-1, +inf). // t could be smaller than -1 in some rare cases due to numerical errors. t = -1; } return Math.exp(helper1(t) * x); } /** * Helper function that calculates {@code log(1 + x) / x}. * <p> * A Taylor series expansion is used, if x is close to 0. * </p> * * @param x A value larger than or equal to -1. * @return {@code log(1 + x) / x}. */ private static double helper1(final double x) { if (Math.abs(x) > TAYLOR_THRESHOLD) { return Math.log1p(x) / x; } return 1 - x * (F_1_2 - x * (F_1_3 - F_1_4 * x)); } /** * Helper function to calculate {@code (exp(x) - 1) / x}. * <p> * A Taylor series expansion is used, if x is close to 0. * </p> * * @param x Free parameter. * @return {@code (exp(x) - 1) / x} if x is non-zero, or 1 if x = 0. */ private static double helper2(final double x) { if (Math.abs(x) > TAYLOR_THRESHOLD) { return Math.expm1(x) / x; } return 1 + x * F_1_2 * (1 + x * F_1_3 * (1 + F_1_4 * x)); } } /** * This instance delegates sampling. Use the factory method * {@link #of(UniformRandomProvider, int, double)} to create an optimal sampler. * * @param rng Generator of uniformly distributed random numbers. * @param numberOfElements Number of elements. * @param exponent Exponent. * @throws IllegalArgumentException if {@code numberOfElements <= 0} * or {@code exponent < 0}. */ public RejectionInversionZipfSampler(UniformRandomProvider rng, int numberOfElements, double exponent) { super(null); // Delegate all work to specialised samplers. this.delegate = of(rng, numberOfElements, exponent); } /** * Rejection inversion sampling method for a discrete, bounded Zipf * distribution that is based on the method described in * <blockquote> * Wolfgang Hörmann and Gerhard Derflinger. * <i>"Rejection-inversion to generate variates from monotone discrete * distributions",</i><br> * <strong>ACM Transactions on Modeling and Computer Simulation</strong> (TOMACS) 6.3 (1996): 169-184. * </blockquote> */ @Override public int sample() { return delegate.sample(); } /** {@inheritDoc} */ @Override public String toString() { return delegate.toString(); } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return delegate.withUniformRandomProvider(rng); } /** * Creates a new Zipf distribution sampler. * * <p>Note when {@code exponent = 0} the Zipf distribution reduces to a * discrete uniform distribution over the interval {@code [1, n]} with {@code n} * the number of elements. * * @param rng Generator of uniformly distributed random numbers. * @param numberOfElements Number of elements. * @param exponent Exponent. * @return the sampler * @throws IllegalArgumentException if {@code numberOfElements <= 0} or * {@code exponent < 0}. * @since 1.3 */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, int numberOfElements, double exponent) { if (numberOfElements <= 0) { throw new IllegalArgumentException("number of elements is not strictly positive: " + numberOfElements); } if (exponent < 0) { throw new IllegalArgumentException("exponent is not positive: " + exponent); } // When the exponent is at the limit of 0 the distribution PMF reduces to 1 / n // and sampling can use a discrete uniform sampler. return exponent > 0 ? new RejectionInversionZipfSamplerImpl(rng, numberOfElements, exponent) : DiscreteUniformSampler.of(rng, 1, numberOfElements); } }
3,005
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from an <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">exponential distribution</a>. * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextLong()} * <li>{@link UniformRandomProvider#nextDouble()} * </ul> * * @since 1.0 */ public class AhrensDieterExponentialSampler extends SamplerBase implements SharedStateContinuousSampler { /** * Table containing the constants * \( q_i = sum_{j=1}^i (\ln 2)^j / j! = \ln 2 + (\ln 2)^2 / 2 + ... + (\ln 2)^i / i! \) * until the largest representable fraction below 1 is exceeded. * * Note that * \( 1 = 2 - 1 = \exp(\ln 2) - 1 = sum_{n=1}^\infinity (\ln 2)^n / n! \) * thus \( q_i \rightarrow 1 as i \rightarrow +\infinity \), * so the higher \( i \), the closer we get to 1 (the series is not alternating). * * By trying, n = 16 in Java is enough to reach 1. */ private static final double[] EXPONENTIAL_SA_QI = new double[16]; /** The mean of this distribution. */ private final double mean; /** Underlying source of randomness. */ private final UniformRandomProvider rng; // // Initialize tables. // static { // // Filling EXPONENTIAL_SA_QI table. // Note that we don't want qi = 0 in the table. // final double ln2 = Math.log(2); double qi = 0; for (int i = 0; i < EXPONENTIAL_SA_QI.length; i++) { qi += Math.pow(ln2, i + 1.0) / InternalUtils.factorial(i + 1); EXPONENTIAL_SA_QI[i] = qi; } } /** * @param rng Generator of uniformly distributed random numbers. * @param mean Mean of this distribution. * @throws IllegalArgumentException if {@code mean <= 0} */ public AhrensDieterExponentialSampler(UniformRandomProvider rng, double mean) { super(null); if (mean <= 0) { throw new IllegalArgumentException("mean is not strictly positive: " + mean); } this.rng = rng; this.mean = mean; } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ private AhrensDieterExponentialSampler(UniformRandomProvider rng, AhrensDieterExponentialSampler source) { super(null); this.rng = rng; this.mean = source.mean; } /** {@inheritDoc} */ @Override public double sample() { // Step 1: double a = 0; // Avoid u=0 which creates an infinite loop double u = InternalUtils.makeNonZeroDouble(rng.nextLong()); // Step 2 and 3: while (u < 0.5) { a += EXPONENTIAL_SA_QI[0]; u *= 2; } // Step 4 (now u >= 0.5): u += u - 1; // Step 5: if (u <= EXPONENTIAL_SA_QI[0]) { return mean * (a + u); } // Step 6: int i = 0; // Should be 1, be we iterate before it in while using 0. double u2 = rng.nextDouble(); double umin = u2; // Step 7 and 8: do { ++i; u2 = rng.nextDouble(); if (u2 < umin) { umin = u2; } // Step 8: } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1. return mean * (a + umin * EXPONENTIAL_SA_QI[0]); } /** {@inheritDoc} */ @Override public String toString() { return "Ahrens-Dieter Exponential deviate [" + rng.toString() + "]"; } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new AhrensDieterExponentialSampler(rng, this); } /** * Create a new exponential distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param mean Mean of the distribution. * @return the sampler * @throws IllegalArgumentException if {@code mean <= 0} * @since 1.3 */ public static SharedStateContinuousSampler of(UniformRandomProvider rng, double mean) { return new AhrensDieterExponentialSampler(rng, mean); } }
3,006
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; import java.util.Arrays; /** * Distribution sampler that uses the <a * href="https://en.wikipedia.org/wiki/Alias_method">Alias method</a>. It can be used to * sample from {@code n} values each with an associated probability. If all unique items * are assigned the same probability it is more efficient to use the {@link DiscreteUniformSampler}. * * <p>This implementation is based on the detailed explanation of the alias method by * Keith Schartz and implements Vose's algorithm.</p> * * <ul> * <li> * <blockquote> * Vose, M.D., * <i>A linear algorithm for generating random numbers with a given distribution,</i> * IEEE Transactions on Software Engineering, 17, 972-975, 1991. * </blockquote> * </li> * </ul> * * <p>The algorithm will sample values in {@code O(1)} time after a pre-processing step of * {@code O(n)} time.</p> * * <p>The alias tables are constructed using fraction probabilities with an assumed denominator * of 2<sup>53</sup>. In the generic case sampling uses {@link UniformRandomProvider#nextInt(int)} * and the upper 53-bits from {@link UniformRandomProvider#nextLong()}.</p> * * <p>Zero padding the input probabilities can be used to make more sampling more efficient. * Any zero entry will always be aliased removing the requirement to compute a {@code long}. * Increased sampling speed comes at the cost of increased storage space. The algorithm requires * approximately 12 bytes of storage per input probability, that is {@code n * 12} for size * {@code n}. Zero-padding only requires 4 bytes of storage per padded value as the probability is * known to be zero. A table can be padded to a power of 2 using the utility function * {@link #of(UniformRandomProvider, double[], int)} to construct the sampler.</p> * * <p>An optimisation is performed for small table sizes that are a power of 2. In this case the * sampling uses 1 or 2 calls from {@link UniformRandomProvider#nextInt()} to generate up to * 64-bits for creation of an 11-bit index and 53-bits for the {@code long}. This optimisation * requires a generator with a high cycle length for the lower order bits.</p> * * <p>Larger table sizes that are a power of 2 will benefit from fast algorithms for * {@link UniformRandomProvider#nextInt(int)} that exploit the power of 2.</p> * * @see <a href="https://en.wikipedia.org/wiki/Alias_method">Alias Method</a> * @see <a href="http://www.keithschwarz.com/darts-dice-coins/">Darts, Dice, and Coins: * Sampling from a Discrete Distribution by Keith Schwartz</a> * @see <a href="https://ieeexplore.ieee.org/document/92917">Vose (1991) IEEE Transactions * on Software Engineering 17, 972-975.</a> * @since 1.3 */ public class AliasMethodDiscreteSampler implements SharedStateDiscreteSampler { /** * The default alpha factor for zero-padding an input probability table. The default * value will pad the probabilities by to the next power-of-2. */ private static final int DEFAULT_ALPHA = 0; /** The value zero for a {@code double}. */ private static final double ZERO = 0.0; /** The value 1.0 represented as the numerator of a fraction with denominator 2<sup>53</sup>. */ private static final long ONE_AS_NUMERATOR = 1L << 53; /** * The multiplier to convert a {@code double} probability in the range {@code [0, 1]} * to the numerator of a fraction with denominator 2<sup>53</sup>. */ private static final double CONVERT_TO_NUMERATOR = ONE_AS_NUMERATOR; /** * The maximum size of the small alias table. This is 2<sup>11</sup>. */ private static final int MAX_SMALL_POWER_2_SIZE = 1 << 11; /** Underlying source of randomness. */ protected final UniformRandomProvider rng; /** * The probability table. During sampling a random index into this table is selected. * A random probability is compared to the value at this index: if lower then the sample is the * index; if higher then the sample uses the corresponding entry in the alias table. * * <p>This has entries up to the last non-zero element since there is no need to store * probabilities of zero. This is an optimisation for zero-padded input. Any zero value will * always be aliased so any look-up index outside this table always uses the alias.</p> * * <p>Note that a uniform double in the range [0,1) can be generated using 53-bits from a long * to sample all the dyadic rationals with a denominator of 2<sup>53</sup> * (e.g. see org.apache.commons.rng.core.utils.NumberFactory.makeDouble(long)). To avoid * computation of a double and comparison to the probability as a double the probabilities are * stored as 53-bit longs to use integer arithmetic. This is the equivalent of storing the * numerator of a fraction with the denominator of 2<sup>53</sup>.</p> * * <p>During conversion of the probability to a double it is rounded up to the next integer * value. This ensures the functionality of comparing a uniform deviate distributed evenly on * the interval 1/2^53 to the unevenly distributed probability is equivalent, i.e. a uniform * deviate is either below the probability or above it: * * <pre> * Uniform deviate * 1/2^53 2/2^53 3/2^53 4/2^53 * --|---------|---------|---------|--- * ^ * | * probability * ^ * | * rounded up * </pre> * * <p>Round-up ensures a non-zero probability is always non-zero and zero probability remains * zero. Thus any item with a non-zero input probability can always be sampled, and a zero * input probability cannot be sampled.</p> * * @see <a href="https://en.wikipedia.org/wiki/Dyadic_rational">Dyadic rational</a> */ protected final long[] probability; /** * The alias table. During sampling if the random probability is not below the entry in the * probability table then the sample is the alias. */ protected final int[] alias; /** * Sample from the computed tables exploiting the small power-of-two table size. * This implements a variant of the optimised algorithm as per Vose (1991): * * <pre> * bits = obtained required number of random bits * v = (some of the bits) * constant1 * j = (rest of the bits) * constant2 * if v &lt; prob[j] then * return j * else * return alias[j] * </pre> * * <p>This is a variant because the bits are not multiplied by constants. In the case of * {@code v} the constant is a scale that is pre-applied to the probability table. In the * case of {@code j} the constant is not used to scale a deviate to an index; the index is * from a power-of-2 range and so the bits are used directly.</p> * * <p>This is implemented using up to 64 bits from the random generator. * The index for the table is computed using a mask to extract up to 11 of the lower bits * from an integer. The probability is computed using a second integer combined with the * remaining bits to create 53-bits for the numerator of a fraction with denominator * 2<sup>53</sup>. This is only computed on demand.</p> * * <p>Note: This supports a table size of up to 2^11, or 2048, exclusive. Any larger requires * consuming more than 64-bits and the algorithm is not more efficient than the * {@link AliasMethodDiscreteSampler}.</p> * * <p>Sampling uses 1 or 2 calls to {@link UniformRandomProvider#nextInt()}.</p> */ private static class SmallTableAliasMethodDiscreteSampler extends AliasMethodDiscreteSampler { /** The mask to isolate the lower bits. */ private final int mask; /** * Create a new instance. * * @param rng Generator of uniformly distributed random numbers. * @param probability Probability table. * @param alias Alias table. */ SmallTableAliasMethodDiscreteSampler(final UniformRandomProvider rng, final long[] probability, final int[] alias) { super(rng, probability, alias); // Assume the table size is a power of 2 and create the mask mask = alias.length - 1; } @Override public int sample() { final int bits = rng.nextInt(); // Isolate lower bits final int j = bits & mask; // Optimisation for zero-padded input tables if (j >= probability.length) { // No probability must use the alias return alias[j]; } // Create a uniform random deviate as a long. // This replicates functionality from the o.a.c.rng.core.utils.NumberFactory.makeLong final long longBits = (((long) rng.nextInt()) << 32) | (bits & 0xffffffffL); // Choose between the two. Use a 53-bit long for the probability. return (longBits >>> 11) < probability[j] ? j : alias[j]; } /** {@inheritDoc} */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new SmallTableAliasMethodDiscreteSampler(rng, probability, alias); } } /** * Creates a sampler. * * <p>The input parameters are not validated and must be correctly computed alias tables.</p> * * @param rng Generator of uniformly distributed random numbers. * @param probability Probability table. * @param alias Alias table. */ private AliasMethodDiscreteSampler(final UniformRandomProvider rng, final long[] probability, final int[] alias) { this.rng = rng; // Deliberate direct storage of input arrays this.probability = probability; this.alias = alias; } /** {@inheritDoc} */ @Override public int sample() { // This implements the algorithm as per Vose (1991): // v = uniform() in [0, 1) // j = uniform(n) in [0, n) // if v < prob[j] then // return j // else // return alias[j] final int j = rng.nextInt(alias.length); // Optimisation for zero-padded input tables if (j >= probability.length) { // No probability must use the alias return alias[j]; } // Note: We could check the probability before computing a deviate. // p(j) == 0 => alias[j] // p(j) == 1 => j // However it is assumed these edge cases are rare: // // The probability table will be 1 for approximately 1/n samples, i.e. only the // last unpaired probability. This is only worth checking for when the table size (n) // is small. But in that case the user should zero-pad the table for performance. // // The probability table will be 0 when an input probability was zero. We // will assume this is also rare if modelling a discrete distribution where // all samples are possible. The edge case for zero-padded tables is handled above. // Choose between the two. Use a 53-bit long for the probability. return (rng.nextLong() >>> 11) < probability[j] ? j : alias[j]; } /** {@inheritDoc} */ @Override public String toString() { return "Alias method [" + rng.toString() + "]"; } /** {@inheritDoc} */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new AliasMethodDiscreteSampler(rng, probability, alias); } /** * Creates a sampler. * * <p>The probabilities will be normalised using their sum. The only requirement * is the sum is strictly positive.</p> * * <p>Where possible this method zero-pads the probabilities so the length is the next * power-of-two. Padding is bounded by the upper limit on the size of an array.</p> * * <p>To avoid zero-padding use the * {@link #of(UniformRandomProvider, double[], int)} method with a negative * {@code alpha} factor.</p> * * @param rng Generator of uniformly distributed random numbers. * @param probabilities The list of probabilities. * @return the sampler * @throws IllegalArgumentException if {@code probabilities} is null or empty, a * probability is negative, infinite or {@code NaN}, or the sum of all * probabilities is not strictly positive. * @see #of(UniformRandomProvider, double[], int) */ public static SharedStateDiscreteSampler of(final UniformRandomProvider rng, final double[] probabilities) { return of(rng, probabilities, DEFAULT_ALPHA); } /** * Creates a sampler. * * <p>The probabilities will be normalised using their sum. The only requirement * is the sum is strictly positive.</p> * * <p>Where possible this method zero-pads the probabilities to improve sampling * efficiency. Padding is bounded by the upper limit on the size of an array and * controlled by the {@code alpha} argument. Set to negative to disable * padding.</p> * * <p>For each zero padded value an entry is added to the tables which is always * aliased. This can be sampled with fewer bits required from the * {@link UniformRandomProvider}. Increasing the padding of zeros increases the * chance of using this fast path to selecting a sample. The penalty is * two-fold: initialisation is bounded by {@code O(n)} time with {@code n} the * size <strong>after</strong> padding; an additional memory cost of 4 bytes per * padded value.</p> * * <p>Zero padding to any length improves performance; using a power of 2 allows * the index into the tables to be more efficiently generated. The argument * {@code alpha} controls the level of padding. Positive values of {@code alpha} * represent a scale factor in powers of 2. The size of the input array will be * increased by a factor of 2<sup>alpha</sup> and then rounded-up to the next * power of 2. Padding is bounded by the upper limit on the size of an * array.</p> * * <p>The chance of executing the slow path is upper bounded at * 2<sup>-alpha</sup> when padding is enabled. Each successive doubling of * padding will have diminishing performance gains.</p> * * @param rng Generator of uniformly distributed random numbers. * @param probabilities The list of probabilities. * @param alpha The alpha factor controlling the zero padding. * @return the sampler * @throws IllegalArgumentException if {@code probabilities} is null or empty, a * probability is negative, infinite or {@code NaN}, or the sum of all * probabilities is not strictly positive. */ public static SharedStateDiscreteSampler of(final UniformRandomProvider rng, final double[] probabilities, int alpha) { // The Alias method balances N categories with counts around the mean into N sections, // each allocated 'mean' observations. // // Consider 4 categories with counts 6,3,2,1. The histogram can be balanced into a // 2D array as 4 sections with a height of the mean: // // 6 // 6 // 6 // 63 => 6366 -- // 632 6326 |-- mean // 6321 6321 -- // // section abcd // // Each section is divided as: // a: 6=1/1 // b: 3=1/1 // c: 2=2/3; 6=1/3 (6 is the alias) // d: 1=1/3; 6=2/3 (6 is the alias) // // The sample is obtained by randomly selecting a section, then choosing which category // from the pair based on a uniform random deviate. final double sumProb = InternalUtils.validateProbabilities(probabilities); // Allow zero-padding final int n = computeSize(probabilities.length, alpha); // Partition into small and large by splitting on the average. final double mean = sumProb / n; // The cardinality of smallSize + largeSize = n. // So fill the same array from either end. final int[] indices = new int[n]; int large = n; int small = 0; for (int i = 0; i < probabilities.length; i++) { if (probabilities[i] >= mean) { indices[--large] = i; } else { indices[small++] = i; } } small = fillRemainingIndices(probabilities.length, indices, small); // This may be smaller than the input length if the probabilities were already padded. final int nonZeroIndex = findLastNonZeroIndex(probabilities); // The probabilities are modified so use a copy. // Note: probabilities are required only up to last nonZeroIndex final double[] remainingProbabilities = Arrays.copyOf(probabilities, nonZeroIndex + 1); // Allocate the final tables. // Probability table may be truncated (when zero padded). // The alias table is full length. final long[] probability = new long[remainingProbabilities.length]; final int[] alias = new int[n]; // This loop uses each large in turn to fill the alias table for small probabilities that // do not reach the requirement to fill an entire section alone (i.e. p < mean). // Since the sum of the small should be less than the sum of the large it should use up // all the small first. However floating point round-off can result in // misclassification of items as small or large. The Vose algorithm handles this using // a while loop conditioned on the size of both sets and a subsequent loop to use // unpaired items. while (large != n && small != 0) { // Index of the small and the large probabilities. final int j = indices[--small]; final int k = indices[large++]; // Optimisation for zero-padded input: // p(j) = 0 above the last nonZeroIndex if (j > nonZeroIndex) { // The entire amount for the section is taken from the alias. remainingProbabilities[k] -= mean; } else { final double pj = remainingProbabilities[j]; // Item j is a small probability that is below the mean. // Compute the weight of the section for item j: pj / mean. // This is scaled by 2^53 and the ceiling function used to round-up // the probability to a numerator of a fraction in the range [1,2^53]. // Ceiling ensures non-zero values. probability[j] = (long) Math.ceil(CONVERT_TO_NUMERATOR * (pj / mean)); // The remaining amount for the section is taken from the alias. // Effectively: probabilities[k] -= (mean - pj) remainingProbabilities[k] += pj - mean; } // If not j then the alias is k alias[j] = k; // Add the remaining probability from large to the appropriate list. if (remainingProbabilities[k] >= mean) { indices[--large] = k; } else { indices[small++] = k; } } // Final loop conditions to consume unpaired items. // Note: The large set should never be non-empty but this can occur due to round-off // error so consume from both. fillTable(probability, alias, indices, 0, small); fillTable(probability, alias, indices, large, n); // Change the algorithm for small power of 2 sized tables return isSmallPowerOf2(n) ? new SmallTableAliasMethodDiscreteSampler(rng, probability, alias) : new AliasMethodDiscreteSampler(rng, probability, alias); } /** * Allocate the remaining indices from zero padding as small probabilities. The * number to add is from the length of the probability array to the length of * the padded probability array (which is the same length as the indices array). * * @param length Length of probability array. * @param indices Indices. * @param small Number of small indices. * @return the updated number of small indices */ private static int fillRemainingIndices(final int length, final int[] indices, int small) { int updatedSmall = small; for (int i = length; i < indices.length; i++) { indices[updatedSmall++] = i; } return updatedSmall; } /** * Find the last non-zero index in the probabilities. This may be smaller than * the input length if the probabilities were already padded. * * @param probabilities The list of probabilities. * @return the index */ private static int findLastNonZeroIndex(final double[] probabilities) { // No bounds check is performed when decrementing as the array contains at least one // value above zero. int nonZeroIndex = probabilities.length - 1; while (probabilities[nonZeroIndex] == ZERO) { nonZeroIndex--; } return nonZeroIndex; } /** * Compute the size after padding. A value of {@code alpha < 0} disables * padding. Otherwise the length will be increased by 2<sup>alpha</sup> * rounded-up to the next power of 2. * * @param length Length of probability array. * @param alpha The alpha factor controlling the zero padding. * @return the padded size */ private static int computeSize(int length, int alpha) { if (alpha < 0) { // No padding return length; } // Use the number of leading zeros function to find the next power of 2, // i.e. ceil(log2(x)) int pow2 = 32 - Integer.numberOfLeadingZeros(length - 1); // Increase by the alpha. Clip this to limit to a positive integer (2^30) pow2 = Math.min(30, pow2 + alpha); // Use max to handle a length above the highest possible power of 2 return Math.max(length, 1 << pow2); } /** * Fill the tables using unpaired items that are in the range between {@code start} inclusive * and {@code end} exclusive. * * <p>Anything left must fill the entire section so the probability table is set * to 1 and there is no alias. This will occur for 1/n samples, i.e. the last * remaining unpaired probability. Note: When the tables are zero-padded the * remaining indices are from an input probability that is above zero so the * index will be allowed in the truncated probability array and no * index-out-of-bounds exception will occur. * * @param probability Probability table. * @param alias Alias table. * @param indices Unpaired indices. * @param start Start position. * @param end End position. */ private static void fillTable(long[] probability, int[] alias, int[] indices, int start, int end) { for (int i = start; i < end; i++) { final int index = indices[i]; probability[index] = ONE_AS_NUMERATOR; alias[index] = index; } } /** * Checks if the size is a small power of 2 so can be supported by the * {@link SmallTableAliasMethodDiscreteSampler}. * * @param n Size of the alias table. * @return true if supported by {@link SmallTableAliasMethodDiscreteSampler} */ private static boolean isSmallPowerOf2(int n) { return n <= MAX_SMALL_POWER_2_SIZE && (n & (n - 1)) == 0; } }
3,007
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformContinuousSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Distribution sampler that uses the * <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. * * It can be used to sample any distribution that provides access to its * <em>inverse cumulative probability function</em>. * * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> * * <p>Example:</p> * <pre><code> * import org.apache.commons.math3.distribution.RealDistribution; * import org.apache.commons.math3.distribution.ChiSquaredDistribution; * * import org.apache.commons.rng.simple.RandomSource; * import org.apache.commons.rng.sampling.distribution.ContinuousSampler; * import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler; * import org.apache.commons.rng.sampling.distribution.ContinuousInverseCumulativeProbabilityFunction; * * // Distribution to sample. * final RealDistribution dist = new ChiSquaredDistribution(9); * // Create the sampler. * final ContinuousSampler chiSquareSampler = * InverseTransformContinuousSampler.of(RandomSource.XO_RO_SHI_RO_128_PP.create(), * new ContinuousInverseCumulativeProbabilityFunction() { * public double inverseCumulativeProbability(double p) { * return dist.inverseCumulativeProbability(p); * } * }); * * // Generate random deviate. * double random = chiSquareSampler.sample(); * </code></pre> * * @since 1.0 */ public class InverseTransformContinuousSampler extends SamplerBase implements SharedStateContinuousSampler { /** Inverse cumulative probability function. */ private final ContinuousInverseCumulativeProbabilityFunction function; /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** * @param rng Generator of uniformly distributed random numbers. * @param function Inverse cumulative probability function. */ public InverseTransformContinuousSampler(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function) { super(null); this.rng = rng; this.function = function; } /** {@inheritDoc} */ @Override public double sample() { return function.inverseCumulativeProbability(rng.nextDouble()); } /** {@inheritDoc} */ @Override public String toString() { return function.toString() + " (inverse method) [" + rng.toString() + "]"; } /** * {@inheritDoc} * * <p>Note: The new sampler will share the inverse cumulative probability function. This * must be suitable for concurrent use to ensure thread safety.</p> * * @since 1.3 */ @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new InverseTransformContinuousSampler(rng, function); } /** * Create a new inverse-transform continuous sampler. * * <p>To use the sampler to * {@link org.apache.commons.rng.sampling.SharedStateSampler share state} the function must be * suitable for concurrent use.</p> * * @param rng Generator of uniformly distributed random numbers. * @param function Inverse cumulative probability function. * @return the sampler * @see #withUniformRandomProvider(UniformRandomProvider) * @since 1.3 */ public static SharedStateContinuousSampler of(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function) { return new InverseTransformContinuousSampler(rng, function); } }
3,008
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformParetoSampler.java
/* * 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.rng.sampling.distribution; import java.util.function.LongToDoubleFunction; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from a <a href="https://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a>. * * <p>Sampling uses {@link UniformRandomProvider#nextLong()}.</p> * * @since 1.0 */ public class InverseTransformParetoSampler extends SamplerBase implements SharedStateContinuousSampler { /** Scale. */ private final double scale; /** 1 / Shape. */ private final double oneOverShape; /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** Method to generate the (1 - p) value. */ private final LongToDoubleFunction nextDouble; /** * @param rng Generator of uniformly distributed random numbers. * @param scale Scale of the distribution. * @param shape Shape of the distribution. * @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0} */ public InverseTransformParetoSampler(UniformRandomProvider rng, double scale, double shape) { super(null); if (scale <= 0) { throw new IllegalArgumentException("scale is not strictly positive: " + scale); } if (shape <= 0) { throw new IllegalArgumentException("shape is not strictly positive: " + shape); } this.rng = rng; this.scale = scale; this.oneOverShape = 1 / shape; // Generate (1 - p) so that samples are concentrated to the lower/upper bound: // large shape samples from p in [0, 1) (lower bound) // small shape samples from p in (0, 1] (upper bound) // Note that the method used is logically reversed as it generates (1 - p). nextDouble = shape >= 1 ? InternalUtils::makeNonZeroDouble : InternalUtils::makeDouble; } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ private InverseTransformParetoSampler(UniformRandomProvider rng, InverseTransformParetoSampler source) { super(null); this.rng = rng; scale = source.scale; oneOverShape = source.oneOverShape; nextDouble = source.nextDouble; } /** {@inheritDoc} */ @Override public double sample() { return scale / Math.pow(nextDouble.applyAsDouble(rng.nextLong()), oneOverShape); } /** {@inheritDoc} */ @Override public String toString() { return "[Inverse method for Pareto distribution " + rng.toString() + "]"; } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new InverseTransformParetoSampler(rng, this); } /** * Creates a new Pareto distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param scale Scale of the distribution. * @param shape Shape of the distribution. * @return the sampler * @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0} * @since 1.3 */ public static SharedStateContinuousSampler of(UniformRandomProvider rng, double scale, double shape) { return new InverseTransformParetoSampler(rng, scale, shape); } }
3,009
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * <a href="https://en.wikipedia.org/wiki/Ziggurat_algorithm"> * Marsaglia and Tsang "Ziggurat" method</a> for sampling from a Gaussian * distribution with mean 0 and standard deviation 1. * * <p>The algorithm is explained in this * <a href="http://www.jstatsoft.org/article/view/v005i08/ziggurat.pdf">paper</a> * and this implementation has been adapted from the C code provided therein.</p> * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextLong()} * <li>{@link UniformRandomProvider#nextDouble()} * </ul> * * @since 1.1 */ public class ZigguratNormalizedGaussianSampler implements NormalizedGaussianSampler, SharedStateContinuousSampler { /** Start of tail. */ private static final double R = 3.6541528853610088; /** Inverse of R. */ private static final double ONE_OVER_R = 1 / R; /** Index of last entry in the tables (which have a size that is a power of 2). */ private static final int LAST = 255; /** Auxiliary table. */ private static final long[] K; /** Auxiliary table. */ private static final double[] W; /** Auxiliary table. */ private static final double[] F; /** Underlying source of randomness. */ private final UniformRandomProvider rng; static { // Filling the tables. // Rectangle area. final double v = 0.00492867323399; // Direction support uses the sign bit so the maximum magnitude from the long is 2^63 final double max = Math.pow(2, 63); final double oneOverMax = 1d / max; K = new long[LAST + 1]; W = new double[LAST + 1]; F = new double[LAST + 1]; double d = R; double t = d; double fd = pdf(d); final double q = v / fd; K[0] = (long) ((d / q) * max); K[1] = 0; W[0] = q * oneOverMax; W[LAST] = d * oneOverMax; F[0] = 1; F[LAST] = fd; for (int i = LAST - 1; i >= 1; i--) { d = Math.sqrt(-2 * Math.log(v / d + fd)); fd = pdf(d); K[i + 1] = (long) ((d / t) * max); t = d; F[i] = fd; W[i] = d * oneOverMax; } } /** * @param rng Generator of uniformly distributed random numbers. */ public ZigguratNormalizedGaussianSampler(UniformRandomProvider rng) { this.rng = rng; } /** {@inheritDoc} */ @Override public double sample() { final long j = rng.nextLong(); final int i = ((int) j) & LAST; if (Math.abs(j) < K[i]) { // This branch is called about 0.985086 times per sample. return j * W[i]; } return fix(j, i); } /** {@inheritDoc} */ @Override public String toString() { return "Ziggurat normalized Gaussian deviate [" + rng.toString() + "]"; } /** * Gets the value from the tail of the distribution. * * @param hz Start random integer. * @param iz Index of cell corresponding to {@code hz}. * @return the requested random value. */ private double fix(long hz, int iz) { if (iz == 0) { // Base strip. // This branch is called about 2.55224E-4 times per sample. double y; double x; do { // Avoid infinity by creating a non-zero double. // Note: The extreme value y from -Math.log(2^-53) is (to 4 sf): // y = 36.74 // The largest value x where 2y < x^2 is false is sqrt(2*36.74): // x = 8.571 // The extreme tail is: // out = +/- 12.01 // To generate this requires longs of 0 and then (1377 << 11). y = -Math.log(InternalUtils.makeNonZeroDouble(rng.nextLong())); x = -Math.log(InternalUtils.makeNonZeroDouble(rng.nextLong())) * ONE_OVER_R; } while (y + y < x * x); final double out = R + x; return hz > 0 ? out : -out; } // Wedge of other strips. // This branch is called about 0.0146584 times per sample. final double x = hz * W[iz]; if (F[iz] + rng.nextDouble() * (F[iz - 1] - F[iz]) < pdf(x)) { // This branch is called about 0.00797887 times per sample. return x; } // Try again. // This branch is called about 0.00667957 times per sample. return sample(); } /** * Compute the Gaussian probability density function {@code f(x) = e^-0.5x^2}. * * @param x Argument. * @return \( e^{-\frac{x^2}{2}} \) */ private static double pdf(double x) { return Math.exp(-0.5 * x * x); } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new ZigguratNormalizedGaussianSampler(rng); } /** * Create a new normalised Gaussian sampler. * * @param <S> Sampler type. * @param rng Generator of uniformly distributed random numbers. * @return the sampler * @since 1.3 */ @SuppressWarnings("unchecked") public static <S extends NormalizedGaussianSampler & SharedStateContinuousSampler> S of(UniformRandomProvider rng) { return (S) new ZigguratNormalizedGaussianSampler(rng); } }
3,010
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from the <a href="http://mathworld.wolfram.com/GammaDistribution.html">gamma distribution</a>. * <ul> * <li> * For {@code 0 < alpha < 1}: * <blockquote> * Ahrens, J. H. and Dieter, U., * <i>Computer methods for sampling from gamma, beta, Poisson and binomial distributions,</i> * Computing, 12, 223-246, 1974. * </blockquote> * </li> * <li> * For {@code alpha >= 1}: * <blockquote> * Marsaglia and Tsang, <i>A Simple Method for Generating * Gamma Variables.</i> ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000. * </blockquote> * </li> * </ul> * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextDouble()} (both algorithms) * <li>{@link UniformRandomProvider#nextLong()} (only for {@code alpha >= 1}) * </ul> * * @see <a href="http://mathworld.wolfram.com/GammaDistribution.html">MathWorld Gamma distribution</a> * @see <a href="https://en.wikipedia.org/wiki/Gamma_distribution">Wikipedia Gamma distribution</a> * @since 1.0 */ public class AhrensDieterMarsagliaTsangGammaSampler extends SamplerBase implements SharedStateContinuousSampler { /** The appropriate gamma sampler for the parameters. */ private final SharedStateContinuousSampler delegate; /** * Base class for a sampler from the Gamma distribution. */ private abstract static class BaseGammaSampler implements SharedStateContinuousSampler { /** Underlying source of randomness. */ protected final UniformRandomProvider rng; /** The alpha parameter. This is a shape parameter. */ protected final double alpha; /** The theta parameter. This is a scale parameter. */ protected final double theta; /** * @param rng Generator of uniformly distributed random numbers. * @param alpha Alpha parameter of the distribution. * @param theta Theta parameter of the distribution. * @throws IllegalArgumentException if {@code alpha <= 0} or {@code theta <= 0} */ BaseGammaSampler(UniformRandomProvider rng, double alpha, double theta) { if (alpha <= 0) { throw new IllegalArgumentException("alpha is not strictly positive: " + alpha); } if (theta <= 0) { throw new IllegalArgumentException("theta is not strictly positive: " + theta); } this.rng = rng; this.alpha = alpha; this.theta = theta; } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ BaseGammaSampler(UniformRandomProvider rng, BaseGammaSampler source) { this.rng = rng; this.alpha = source.alpha; this.theta = source.theta; } /** {@inheritDoc} */ @Override public String toString() { return "Ahrens-Dieter-Marsaglia-Tsang Gamma deviate [" + rng.toString() + "]"; } } /** * Class to sample from the Gamma distribution when {@code 0 < alpha < 1}. * * <blockquote> * Ahrens, J. H. and Dieter, U., * <i>Computer methods for sampling from gamma, beta, Poisson and binomial distributions,</i> * Computing, 12, 223-246, 1974. * </blockquote> */ private static class AhrensDieterGammaSampler extends BaseGammaSampler { /** Inverse of "alpha". */ private final double oneOverAlpha; /** Optimization (see code). */ private final double bGSOptim; /** * @param rng Generator of uniformly distributed random numbers. * @param alpha Alpha parameter of the distribution. * @param theta Theta parameter of the distribution. * @throws IllegalArgumentException if {@code alpha <= 0} or {@code theta <= 0} */ AhrensDieterGammaSampler(UniformRandomProvider rng, double alpha, double theta) { super(rng, alpha, theta); oneOverAlpha = 1 / alpha; bGSOptim = 1 + alpha / Math.E; } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ AhrensDieterGammaSampler(UniformRandomProvider rng, AhrensDieterGammaSampler source) { super(rng, source); oneOverAlpha = source.oneOverAlpha; bGSOptim = source.bGSOptim; } @Override public double sample() { // [1]: p. 228, Algorithm GS. while (true) { // Step 1: final double u = rng.nextDouble(); final double p = bGSOptim * u; if (p <= 1) { // Step 2: final double x = Math.pow(p, oneOverAlpha); final double u2 = rng.nextDouble(); if (u2 > Math.exp(-x)) { // Reject. continue; } return theta * x; } // Step 3: final double x = -Math.log((bGSOptim - p) * oneOverAlpha); final double u2 = rng.nextDouble(); if (u2 <= Math.pow(x, alpha - 1)) { return theta * x; } // Reject and continue. } } @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new AhrensDieterGammaSampler(rng, this); } } /** * Class to sample from the Gamma distribution when the {@code alpha >= 1}. * * <blockquote> * Marsaglia and Tsang, <i>A Simple Method for Generating * Gamma Variables.</i> ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000. * </blockquote> */ private static class MarsagliaTsangGammaSampler extends BaseGammaSampler { /** 1/3. */ private static final double ONE_THIRD = 1d / 3; /** Optimization (see code). */ private final double dOptim; /** Optimization (see code). */ private final double cOptim; /** Gaussian sampling. */ private final NormalizedGaussianSampler gaussian; /** * @param rng Generator of uniformly distributed random numbers. * @param alpha Alpha parameter of the distribution. * @param theta Theta parameter of the distribution. * @throws IllegalArgumentException if {@code alpha <= 0} or {@code theta <= 0} */ MarsagliaTsangGammaSampler(UniformRandomProvider rng, double alpha, double theta) { super(rng, alpha, theta); gaussian = ZigguratSampler.NormalizedGaussian.of(rng); dOptim = alpha - ONE_THIRD; cOptim = ONE_THIRD / Math.sqrt(dOptim); } /** * @param rng Generator of uniformly distributed random numbers. * @param source Source to copy. */ MarsagliaTsangGammaSampler(UniformRandomProvider rng, MarsagliaTsangGammaSampler source) { super(rng, source); gaussian = ZigguratSampler.NormalizedGaussian.of(rng); dOptim = source.dOptim; cOptim = source.cOptim; } @Override public double sample() { while (true) { final double x = gaussian.sample(); final double oPcTx = 1 + cOptim * x; final double v = oPcTx * oPcTx * oPcTx; if (v <= 0) { continue; } final double x2 = x * x; final double u = rng.nextDouble(); // Squeeze. if (u < 1 - 0.0331 * x2 * x2) { return theta * dOptim * v; } if (Math.log(u) < 0.5 * x2 + dOptim * (1 - v + Math.log(v))) { return theta * dOptim * v; } } } @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { return new MarsagliaTsangGammaSampler(rng, this); } } /** * This instance delegates sampling. Use the factory method * {@link #of(UniformRandomProvider, double, double)} to create an optimal sampler. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Alpha parameter of the distribution (this is a shape parameter). * @param theta Theta parameter of the distribution (this is a scale parameter). * @throws IllegalArgumentException if {@code alpha <= 0} or {@code theta <= 0} */ public AhrensDieterMarsagliaTsangGammaSampler(UniformRandomProvider rng, double alpha, double theta) { super(null); delegate = of(rng, alpha, theta); } /** {@inheritDoc} */ @Override public double sample() { return delegate.sample(); } /** {@inheritDoc} */ @Override public String toString() { return delegate.toString(); } /** * {@inheritDoc} * * @since 1.3 */ @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { // Direct return of the optimised sampler return delegate.withUniformRandomProvider(rng); } /** * Creates a new gamma distribution sampler. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Alpha parameter of the distribution (this is a shape parameter). * @param theta Theta parameter of the distribution (this is a scale parameter). * @return the sampler * @throws IllegalArgumentException if {@code alpha <= 0} or {@code theta <= 0} * @since 1.3 */ public static SharedStateContinuousSampler of(UniformRandomProvider rng, double alpha, double theta) { // Each sampler should check the input arguments. return alpha < 1 ? new AhrensDieterGammaSampler(rng, alpha, theta) : new MarsagliaTsangGammaSampler(rng, alpha, theta); } }
3,011
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SharedStateContinuousSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.sampling.SharedStateSampler; /** * Sampler that generates values of type {@code double} and can create new instances to sample * from the same state with a given source of randomness. * * @since 1.3 */ public interface SharedStateContinuousSampler extends ContinuousSampler, SharedStateSampler<SharedStateContinuousSampler> { // Composite interface }
3,012
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from a <a href="https://en.wikipedia.org/wiki/Log-normal_distribution"> * log-normal distribution</a>. * Uses {@link BoxMullerNormalizedGaussianSampler} as the underlying sampler. * * <p>Sampling uses:</p> * * <ul> * <li>{@link UniformRandomProvider#nextDouble()} * <li>{@link UniformRandomProvider#nextLong()} * </ul> * * @since 1.0 * * @deprecated Since version 1.1. Please use {@link LogNormalSampler} instead. */ @Deprecated public class BoxMullerLogNormalSampler extends SamplerBase implements ContinuousSampler { /** Delegate. */ private final ContinuousSampler sampler; /** * @param rng Generator of uniformly distributed random numbers. * @param mu Mean of the natural logarithm of the distribution values. * @param sigma Standard deviation of the natural logarithm of the distribution values. * @throws IllegalArgumentException if {@code sigma <= 0}. */ public BoxMullerLogNormalSampler(UniformRandomProvider rng, double mu, double sigma) { super(null); sampler = LogNormalSampler.of(new BoxMullerNormalizedGaussianSampler(rng), mu, sigma); } /** {@inheritDoc} */ @Override public double sample() { return sampler.sample(); } /** {@inheritDoc} */ @Override public String toString() { return sampler.toString(); } }
3,013
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GeometricSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Sampling from a <a href="https://en.wikipedia.org/wiki/Geometric_distribution">geometric * distribution</a>. * * <p>This distribution samples the number of failures before the first success taking values in the * set {@code [0, 1, 2, ...]}.</p> * * <p>The sample is computed using a related exponential distribution. If \( X \) is an * exponentially distributed random variable with parameter \( \lambda \), then * \( Y = \left \lfloor X \right \rfloor \) is a geometrically distributed random variable with * parameter \( p = 1 − e^\lambda \), with \( p \) the probability of success.</p> * * <p>This sampler outperforms using the {@link InverseTransformDiscreteSampler} with an appropriate * geometric inverse cumulative probability function.</p> * * <p>Usage note: As the probability of success (\( p \)) tends towards zero the mean of the * distribution (\( \frac{1-p}{p} \)) tends towards infinity and due to the use of {@code int} * for the sample this can result in truncation of the distribution.</p> * * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> * * @see <a * href="https://en.wikipedia.org/wiki/Geometric_distribution#Related_distributions">Geometric * distribution - related distributions</a> * @since 1.3 */ public final class GeometricSampler { /** * Sample from the geometric distribution when the probability of success is 1. */ private static class GeometricP1Sampler implements SharedStateDiscreteSampler { /** The single instance. */ static final GeometricP1Sampler INSTANCE = new GeometricP1Sampler(); @Override public int sample() { // When probability of success is 1 the sample is always zero return 0; } @Override public String toString() { return "Geometric(p=1) deviate"; } @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { // No requirement for a new instance return this; } } /** * Sample from the geometric distribution by using a related exponential distribution. */ private static class GeometricExponentialSampler implements SharedStateDiscreteSampler { /** Underlying source of randomness. Used only for the {@link #toString()} method. */ private final UniformRandomProvider rng; /** The related exponential sampler for the geometric distribution. */ private final SharedStateContinuousSampler exponentialSampler; /** * @param rng Generator of uniformly distributed random numbers * @param probabilityOfSuccess The probability of success (must be in the range * {@code [0 < probabilityOfSuccess < 1]}) */ GeometricExponentialSampler(UniformRandomProvider rng, double probabilityOfSuccess) { this.rng = rng; // Use a related exponential distribution: // λ = −ln(1 − probabilityOfSuccess) // exponential mean = 1 / λ // -- // Note on validation: // If probabilityOfSuccess == Math.nextDown(1.0) the exponential mean is >0 (valid). // If probabilityOfSuccess == Double.MIN_VALUE the exponential mean is +Infinity // and the sample will always be Integer.MAX_VALUE (the distribution is truncated). It // is noted in the class Javadoc that the use of a small p leads to truncation so // no checks are made for this case. final double exponentialMean = 1.0 / (-Math.log1p(-probabilityOfSuccess)); exponentialSampler = ZigguratSampler.Exponential.of(rng, exponentialMean); } /** * @param rng Generator of uniformly distributed random numbers * @param source Source to copy. */ GeometricExponentialSampler(UniformRandomProvider rng, GeometricExponentialSampler source) { this.rng = rng; exponentialSampler = source.exponentialSampler.withUniformRandomProvider(rng); } @Override public int sample() { // Return the floor of the exponential sample return (int) Math.floor(exponentialSampler.sample()); } @Override public String toString() { return "Geometric deviate [" + rng.toString() + "]"; } @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new GeometricExponentialSampler(rng, this); } } /** Class contains only static methods. */ private GeometricSampler() {} /** * Creates a new geometric distribution sampler. The samples will be provided in the set * {@code k=[0, 1, 2, ...]} where {@code k} indicates the number of failures before the first * success. * * @param rng Generator of uniformly distributed random numbers. * @param probabilityOfSuccess The probability of success. * @return the sampler * @throws IllegalArgumentException if {@code probabilityOfSuccess} is not in the range * {@code [0 < probabilityOfSuccess <= 1]}) */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double probabilityOfSuccess) { if (probabilityOfSuccess <= 0 || probabilityOfSuccess > 1) { throw new IllegalArgumentException( "Probability of success (p) must be in the range [0 < p <= 1]: " + probabilityOfSuccess); } return probabilityOfSuccess == 1 ? GeometricP1Sampler.INSTANCE : new GeometricExponentialSampler(rng, probabilityOfSuccess); } }
3,014
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteSampler.java
/* * 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.rng.sampling.distribution; import java.util.stream.IntStream; /** * Sampler that generates values of type {@code int}. * * @since 1.0 */ public interface DiscreteSampler { /** * Creates an {@code int} sample. * * @return a sample. */ int sample(); /** * Returns an effectively unlimited stream of {@code int} sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(). * * @return a stream of {@code int} values. * @since 1.5 */ default IntStream samples() { return IntStream.generate(this::sample).sequential(); } /** * Returns a stream producing the given {@code streamSize} number of {@code int} * sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. * * @param streamSize Number of values to generate. * @return a stream of {@code int} values. * @since 1.5 */ default IntStream samples(long streamSize) { return samples().limit(streamSize); } }
3,015
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalGamma.java
/* * 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.rng.sampling.distribution; /** * <h3> * Adapted and stripped down copy of class * {@code "org.apache.commons.math4.special.Gamma"}. * </h3> * * <p> * This is a utility class that provides computation methods related to the * &Gamma; (Gamma) family of functions. * </p> */ final class InternalGamma { // Class is package-private on purpose; do not make it public. /** * Constant \( g = \frac{607}{128} \) in the Lanczos approximation. */ public static final double LANCZOS_G = 607.0 / 128.0; /** Lanczos coefficients. */ private static final double[] LANCZOS_COEFFICIENTS = { 0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, .33994649984811888699e-4, .46523628927048575665e-4, -.98374475304879564677e-4, .15808870322491248884e-3, -.21026444172410488319e-3, .21743961811521264320e-3, -.16431810653676389022e-3, .84418223983852743293e-4, -.26190838401581408670e-4, .36899182659531622704e-5, }; /** Avoid repeated computation of log of 2 PI in logGamma. */ private static final double HALF_LOG_2_PI = 0.5 * Math.log(2.0 * Math.PI); /** * Class contains only static methods. */ private InternalGamma() {} /** * Computes the function \( \ln \Gamma(x) \) for \( x \gt 0 \). * * <p> * For \( x \leq 8 \), the implementation is based on the double precision * implementation in the <em>NSWC Library of Mathematics Subroutines</em>, * {@code DGAMLN}. For \( x \geq 8 \), the implementation is based on * </p> * * <ul> * <li><a href="http://mathworld.wolfram.com/GammaFunction.html">Gamma * Function</a>, equation (28).</li> * <li><a href="http://mathworld.wolfram.com/LanczosApproximation.html"> * Lanczos Approximation</a>, equations (1) through (5).</li> * <li><a href="http://my.fit.edu/~gabdo/gamma.txt">Paul Godfrey, A note on * the computation of the convergent Lanczos complex Gamma * approximation</a></li> * </ul> * * @param x Argument. * @return \( \ln \Gamma(x) \), or {@code NaN} if {@code x <= 0}. */ public static double logGamma(double x) { // Stripped-down version of the same method defined in "Commons Math": // Unused "if" branches (for when x < 8) have been removed here since // this method is only used (by class "InternalUtils") in order to // compute log(n!) for x > 20. final double sum = lanczos(x); final double tmp = x + LANCZOS_G + 0.5; return (x + 0.5) * Math.log(tmp) - tmp + HALF_LOG_2_PI + Math.log(sum / x); } /** * Computes the Lanczos approximation used to compute the gamma function. * * <p> * The Lanczos approximation is related to the Gamma function by the * following equation * \[ * \Gamma(x) = \sqrt{2\pi} \, \frac{(g + x + \frac{1}{2})^{x + \frac{1}{2}} \, e^{-(g + x + \frac{1}{2})} \, \mathrm{lanczos}(x)} * {x} * \] * where \(g\) is the Lanczos constant. * </p> * * @param x Argument. * @return The Lanczos approximation. * * @see <a href="http://mathworld.wolfram.com/LanczosApproximation.html">Lanczos Approximation</a> * equations (1) through (5), and Paul Godfrey's * <a href="http://my.fit.edu/~gabdo/gamma.txt">Note on the computation * of the convergent Lanczos complex Gamma approximation</a> */ private static double lanczos(final double x) { double sum = 0.0; for (int i = LANCZOS_COEFFICIENTS.length - 1; i > 0; --i) { sum += LANCZOS_COEFFICIENTS[i] / (x + i); } return sum + LANCZOS_COEFFICIENTS[0]; } }
3,016
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SharedStateDiscreteSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.sampling.SharedStateSampler; /** * Sampler that generates values of type {@code int} and can create new instances to sample * from the same state with a given source of randomness. * * @since 1.3 */ public interface SharedStateDiscreteSampler extends DiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler> { // Composite interface }
3,017
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LongSampler.java
/* * 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.rng.sampling.distribution; import java.util.stream.LongStream; /** * Sampler that generates values of type {@code long}. * * @since 1.4 */ public interface LongSampler { /** * Creates a {@code long} sample. * * @return a sample. */ long sample(); /** * Returns an effectively unlimited stream of {@code long} sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(). * * @return a stream of {@code long} values. * @since 1.5 */ default LongStream samples() { return LongStream.generate(this::sample).sequential(); } /** * Returns a stream producing the given {@code streamSize} number of {@code long} * sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. * * @param streamSize Number of values to generate. * @return a stream of {@code long} values. * @since 1.5 */ default LongStream samples(long streamSize) { return samples().limit(streamSize); } }
3,018
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/StableSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Samples from a stable distribution. * * <p>Several different parameterizations exist for the stable distribution. * This sampler uses the 0-parameterization distribution described in Nolan (2020) "Univariate Stable * Distributions: Models for Heavy Tailed Data". Springer Series in Operations Research and * Financial Engineering. Springer. Sections 1.7 and 3.3.3. * * <p>The random variable \( X \) has * the stable distribution \( S(\alpha, \beta, \gamma, \delta; 0) \) if its characteristic * function is given by: * * <p>\[ E(e^{iuX}) = \begin{cases} \exp \left (- \gamma^\alpha |u|^\alpha \left [1 - i \beta (\tan \frac{\pi \alpha}{2})(\text{sgn}(u)) \right ] + i \delta u \right ) &amp; \alpha \neq 1 \\ * \exp \left (- \gamma |u| \left [1 + i \beta \frac{2}{\pi} (\text{sgn}(u)) \log |u| \right ] + i \delta u \right ) &amp; \alpha = 1 \end{cases} \] * * <p>The function is continuous with respect to all the parameters; the parameters \( \alpha \) * and \( \beta \) determine the shape and the parameters \( \gamma \) and \( \delta \) determine * the scale and location. The support of the distribution is: * * <p>\[ \text{support} f(x|\alpha,\beta,\gamma,\delta; 0) = \begin{cases} [\delta - \gamma \tan \frac{\pi \alpha}{2}, \infty) &amp; \alpha \lt 1\ and\ \beta = 1 \\ * (-\infty, \delta + \gamma \tan \frac{\pi \alpha}{2}] &amp; \alpha \lt 1\ and\ \beta = -1 \\ * (-\infty, \infty) &amp; otherwise \end{cases} \] * * <p>The implementation uses the Chambers-Mallows-Stuck (CMS) method as described in: * <ul> * <li>Chambers, Mallows &amp; Stuck (1976) "A Method for Simulating Stable Random Variables". * Journal of the American Statistical Association. 71 (354): 340–344. * <li>Weron (1996) "On the Chambers-Mallows-Stuck method for simulating skewed stable * random variables". Statistics &amp; Probability Letters. 28 (2): 165–171. * </ul> * * @see <a href="https://en.wikipedia.org/wiki/Stable_distribution">Stable distribution (Wikipedia)</a> * @see <a href="https://link.springer.com/book/10.1007/978-3-030-52915-4">Nolan (2020) Univariate Stable Distributions</a> * @see <a href="https://doi.org/10.1080%2F01621459.1976.10480344">Chambers et al (1976) JOASA 71: 340-344</a> * @see <a href="https://doi.org/10.1016%2F0167-7152%2895%2900113-1">Weron (1996). * Statistics &amp; Probability Letters. 28 (2): 165–171.</a> * @since 1.4 */ public abstract class StableSampler implements SharedStateContinuousSampler { /** pi / 2. */ private static final double PI_2 = Math.PI / 2; /** The alpha value for the Gaussian case. */ private static final double ALPHA_GAUSSIAN = 2; /** The alpha value for the Cauchy case. */ private static final double ALPHA_CAUCHY = 1; /** The alpha value for the Levy case. */ private static final double ALPHA_LEVY = 0.5; /** The alpha value for the {@code alpha -> 0} to switch to using the Weron formula. * Note that small alpha requires robust correction of infinite samples. */ private static final double ALPHA_SMALL = 0.02; /** The beta value for the Levy case. */ private static final double BETA_LEVY = 1.0; /** The gamma value for the normalized case. */ private static final double GAMMA_1 = 1.0; /** The delta value for the normalized case. */ private static final double DELTA_0 = 0.0; /** The tau value for zero. When tau is zero, this is effectively {@code beta = 0}. */ private static final double TAU_ZERO = 0.0; /** * The lower support for the distribution. * This is the lower bound of {@code (-inf, +inf)} * If the sample is not within this bound ({@code lower < x}) then it is either * infinite or NaN and the result should be checked. */ private static final double LOWER = Double.NEGATIVE_INFINITY; /** * The upper support for the distribution. * This is the upper bound of {@code (-inf, +inf)}. * If the sample is not within this bound ({@code x < upper}) then it is either * infinite or NaN and the result should be checked. */ private static final double UPPER = Double.POSITIVE_INFINITY; /** Underlying source of randomness. */ private final UniformRandomProvider rng; // Implementation notes // // The Chambers-Mallows-Stuck (CMS) method uses a uniform deviate u in (0, 1) and an // exponential deviate w to compute a stable deviate. Chambers et al (1976) published // a formula for alpha = 1 and alpha != 1. The function is discontinuous at alpha = 1 // and to address this a trigonmoic rearrangement was provided using half angles that // is continuous with respect to alpha. The original discontinuous formulas were proven // in Weron (1996). The CMS rearrangement creates a deviate in the 0-parameterization // defined by Nolan (2020); the original discontinuous functions create a deviate in the // 1-parameterization defined by Nolan. A shift can be used to convert one parameterisation // to the other. The shift is the magnitude of the zeta term from the 1-parameterisation. // The following table shows how the zeta term -> inf when alpha -> 1 for // different beta (hence the discontinuity in the function): // // Zeta // Beta // Alpha 1.0 0.5 0.25 0.1 0.0 // 0.001 0.001571 0.0007854 0.0003927 0.0001571 0.0 // 0.01 0.01571 0.007855 0.003927 0.001571 0.0 // 0.05 0.07870 0.03935 0.01968 0.007870 0.0 // 0.01 0.01571 0.007855 0.003927 0.001571 0.0 // 0.1 0.1584 0.07919 0.03960 0.01584 0.0 // 0.5 1.000 0.5000 0.2500 0.1000 0.0 // 0.9 6.314 3.157 1.578 0.6314 0.0 // 0.95 12.71 6.353 3.177 1.271 0.0 // 0.99 63.66 31.83 15.91 6.366 0.0 // 0.995 127.3 63.66 31.83 12.73 0.0 // 0.999 636.6 318.3 159.2 63.66 0.0 // 0.9995 1273 636.6 318.3 127.3 0.0 // 0.9999 6366 3183 1592 636.6 0.0 // 1.0 1.633E+16 8.166E+15 4.083E+15 1.633E+15 0.0 // // For numerical simulation the 0-parameterization is favoured as it is continuous // with respect to all the parameters. When approaching alpha = 1 the large magnitude // of the zeta term used to shift the 1-parameterization results in cancellation and the // number of bits of the output sample is effected. This sampler uses the CMS method with // the continuous function as the base for the implementation. However it is not suitable // for all values of alpha and beta. // // The method computes a value log(z) with z in the interval (0, inf). When z is 0 or infinite // the computation can return invalid results. The open bound for the deviate u avoids // generating an extreme value that results in cancellation, z=0 and an invalid expression. // However due to floating point error this can occur // when u is close to 0 or 1, and beta is -1 or 1. Thus it is not enough to create // u by avoiding 0 or 1 and further checks are required. // The division by the deviate w also results in an invalid expression as the term z becomes // infinite as w -> 0. It should be noted that such events are extremely rare // (frequency in the 1 in 10^15), or will not occur at all depending on the parameters alpha // and beta. // // When alpha -> 0 then the distribution is extremely long tailed and the expression // using log(z) often computes infinity. Certain parameters can create NaN due to // 0 / 0, 0 * inf, or inf - inf. Thus the implementation must check the final result // and perform a correction if required, or generate another sample. // Correcting the original CMS formula has many edge cases depending on parameters. The // alternative formula provided by Weron is easier to correct when infinite values are // created. This correction is made easier by knowing that u is not 0 or 1 as certain // conditions on the intermediate terms can be eliminated. The implementation // thus generates u in the open interval (0,1) but leaves w unchecked and potentially 0. // The sample is generated and the result tested against the expected support. This detects // any NaN and infinite values. Incorrect samples due to the inability to compute log(z) // (extremely rare) and samples where alpha -> 0 has resulted in an infinite expression // for the value d are corrected using the Weron formula and returned within the support. // // The CMS algorithm is continuous for the parameters. However when alpha=1 or beta=0 // many terms cancel and these cases are handled with specialised implementations. // The beta=0 case implements the same CMS algorithm with certain terms eliminated. // Correction uses the alternative Weron formula. When alpha=1 the CMS algorithm can // be corrected from infinite cases due to assumptions on the intermediate terms. // // The following table show the failure frequency (result not finite or, when beta=+/-1, // within the support) for the CMS algorithm computed using 2^30 random deviates. // // CMS failure rate // Beta // Alpha 1.0 0.5 0.25 0.1 0.0 // 1.999 0.0 0.0 0.0 0.0 0.0 // 1.99 0.0 0.0 0.0 0.0 0.0 // 1.9 0.0 0.0 0.0 0.0 0.0 // 1.5 0.0 0.0 0.0 0.0 0.0 // 1.1 0.0 0.0 0.0 0.0 0.0 // 1.0 0.0 0.0 0.0 0.0 0.0 // 0.9 0.0 0.0 0.0 0.0 0.0 // 0.5 0.0 0.0 0.0 0.0 0.0 // 0.25 0.0 0.0 0.0 0.0 0.0 // 0.1 0.0 0.0 0.0 0.0 0.0 // 0.05 0.0003458 0.0 0.0 0.0 0.0 // 0.02 0.009028 6.938E-7 7.180E-7 7.320E-7 6.873E-7 // 0.01 0.004878 0.0008555 0.0008553 0.0008554 0.0008570 // 0.005 0.1519 0.02896 0.02897 0.02897 0.02897 // 0.001 0.6038 0.3903 0.3903 0.3903 0.3903 // // The sampler switches to using the error checked Weron implementation when alpha < 0.02. // Unit tests demonstrate the two samplers (CMS or Weron) product the same result within // a tolerance. The switch point is based on a consistent failure rate above 1 in a million. // At this point zeta is small and cancellation leading to loss of bits in the sample is // minimal. // // In common use the sampler will not have a measurable failure rate. The output will // be continuous as alpha -> 1 and beta -> 0. The evaluated function produces symmetric // samples when u and beta are mirrored around 0.5 and 0 respectively. To achieve this // the computation of certain parameters has been changed from the original implementation // to avoid evaluating Math.tan outside the interval (-pi/2, pi/2). // // Note: Chambers et al (1976) use an approximation to tan(x) / x in the RSTAB routine. // A JMH performance test is available in the RNG examples module comparing Math.tan // with various approximations. The functions are faster than Math.tan(x) / x. // This implementation uses a higher accuracy approximation than the original RSTAB // implementation; it has a mean ULP difference to Math.tan of less than 1 and has // a noticeable performance gain. /** * Base class for implementations of a stable distribution that requires an exponential * random deviate. */ private abstract static class BaseStableSampler extends StableSampler { /** pi/2 scaled by 2^-53. */ private static final double PI_2_SCALED = 0x1.0p-54 * Math.PI; /** pi/4 scaled by 2^-53. */ private static final double PI_4_SCALED = 0x1.0p-55 * Math.PI; /** -pi / 2. */ private static final double NEG_PI_2 = -Math.PI / 2; /** -pi / 4. */ private static final double NEG_PI_4 = -Math.PI / 4; /** The exponential sampler. */ private final ContinuousSampler expSampler; /** * @param rng Underlying source of randomness */ BaseStableSampler(UniformRandomProvider rng) { super(rng); expSampler = ZigguratSampler.Exponential.of(rng); } /** * Gets a random value for the omega parameter ({@code w}). * This is an exponential random variable with mean 1. * * <p>Warning: For simplicity this does not check the variate is not 0. * The calling CMS algorithm should detect and handle incorrect samples as a result * of this unlikely edge case. * * @return omega */ double getOmega() { // Note: Ideally this should not have a value of 0 as the CMS algorithm divides // by w and it creates infinity. This can result in NaN output. // Under certain parameterizations non-zero small w also creates NaN output. // Thus output should be checked regardless. return expSampler.sample(); } /** * Gets a random value for the phi parameter. * This is a uniform random variable in {@code (-pi/2, pi/2)}. * * @return phi */ double getPhi() { // See getPhiBy2 for method details. final double x = (nextLong() >> 10) * PI_2_SCALED; // Deliberate floating-point equality check if (x == NEG_PI_2) { return getPhi(); } return x; } /** * Gets a random value for the phi parameter divided by 2. * This is a uniform random variable in {@code (-pi/4, pi/4)}. * * <p>Note: Ideally this should not have a value of -pi/4 or pi/4 as the CMS algorithm * can generate infinite values when the phi/2 uniform deviate is +/-pi/4. This * can result in NaN output. Under certain parameterizations phi/2 close to the limits * also create NaN output. Thus output should be checked regardless. Avoiding * the extreme values simplifies the number of checks that are required. * * @return phi / 2 */ double getPhiBy2() { // As per o.a.c.rng.core.utils.NumberFactory.makeDouble(long) but using a // signed shift of 10 in place of an unsigned shift of 11. With a factor of 2^-53 // this would produce a double in [-1, 1). // Here the multiplication factor incorporates pi/4 to avoid a separate // multiplication. final double x = (nextLong() >> 10) * PI_4_SCALED; // Deliberate floating-point equality check if (x == NEG_PI_4) { // Sample again using recursion. // A stack overflow due to a broken RNG will eventually occur // rather than the alternative which is an infinite loop // while x == -pi/4. return getPhiBy2(); } return x; } } /** * Class for implementations of a stable distribution transformed by scale and location. */ private static class TransformedStableSampler extends StableSampler { /** Underlying normalized stable sampler. */ private final StableSampler sampler; /** The scale parameter. */ private final double gamma; /** The location parameter. */ private final double delta; /** * @param sampler Normalized stable sampler. * @param gamma Scale parameter. Must be strictly positive. * @param delta Location parameter. */ TransformedStableSampler(StableSampler sampler, double gamma, double delta) { // No RNG required super(null); this.sampler = sampler; this.gamma = gamma; this.delta = delta; } @Override public double sample() { return gamma * sampler.sample() + delta; } @Override public StableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new TransformedStableSampler(sampler.withUniformRandomProvider(rng), gamma, delta); } @Override public String toString() { // Avoid a null pointer from the unset RNG instance in the parent class return sampler.toString(); } } /** * Implement the {@code alpha = 2} stable distribution case (Gaussian distribution). */ private static class GaussianStableSampler extends StableSampler { /** sqrt(2). */ private static final double ROOT_2 = Math.sqrt(2); /** Underlying normalized Gaussian sampler. */ private final NormalizedGaussianSampler sampler; /** The standard deviation. */ private final double stdDev; /** The mean. */ private final double mean; /** * @param rng Underlying source of randomness * @param gamma Scale parameter. Must be strictly positive. * @param delta Location parameter. */ GaussianStableSampler(UniformRandomProvider rng, double gamma, double delta) { super(rng); this.sampler = ZigguratSampler.NormalizedGaussian.of(rng); // A standardized stable sampler with alpha=2 has variance 2. // Set the standard deviation as sqrt(2) * scale. // Avoid this being infinity to avoid inf * 0 in the sample this.stdDev = Math.min(Double.MAX_VALUE, ROOT_2 * gamma); this.mean = delta; } /** * @param rng Underlying source of randomness * @param source Source to copy. */ GaussianStableSampler(UniformRandomProvider rng, GaussianStableSampler source) { super(rng); this.sampler = ZigguratSampler.NormalizedGaussian.of(rng); this.stdDev = source.stdDev; this.mean = source.mean; } @Override public double sample() { return stdDev * sampler.sample() + mean; } @Override public GaussianStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new GaussianStableSampler(rng, this); } } /** * Implement the {@code alpha = 1} and {@code beta = 0} stable distribution case * (Cauchy distribution). */ private static class CauchyStableSampler extends BaseStableSampler { /** The scale parameter. */ private final double gamma; /** The location parameter. */ private final double delta; /** * @param rng Underlying source of randomness * @param gamma Scale parameter. Must be strictly positive. * @param delta Location parameter. */ CauchyStableSampler(UniformRandomProvider rng, double gamma, double delta) { super(rng); this.gamma = gamma; this.delta = delta; } /** * @param rng Underlying source of randomness * @param source Source to copy. */ CauchyStableSampler(UniformRandomProvider rng, CauchyStableSampler source) { super(rng); this.gamma = source.gamma; this.delta = source.delta; } @Override public double sample() { // Note: // The CMS beta=0 with alpha=1 sampler reduces to: // S = 2 * a / a2, with a = tan(x), a2 = 1 - a^2, x = phi/2 // This is a double angle identity for tan: // 2 * tan(x) / (1 - tan^2(x)) = tan(2x) // Here we use the double angle identity for consistency with the other samplers. final double phiby2 = getPhiBy2(); final double a = phiby2 * SpecialMath.tan2(phiby2); final double a2 = 1 - a * a; final double x = 2 * a / a2; return gamma * x + delta; } @Override public CauchyStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new CauchyStableSampler(rng, this); } } /** * Implement the {@code alpha = 0.5} and {@code beta = 1} stable distribution case * (Levy distribution). * * Note: This sampler can be used to output the symmetric case when * {@code beta = -1} by negating {@code gamma}. */ private static class LevyStableSampler extends StableSampler { /** Underlying normalized Gaussian sampler. */ private final NormalizedGaussianSampler sampler; /** The scale parameter. */ private final double gamma; /** The location parameter. */ private final double delta; /** * @param rng Underlying source of randomness * @param gamma Scale parameter. Must be strictly positive. * @param delta Location parameter. */ LevyStableSampler(UniformRandomProvider rng, double gamma, double delta) { super(rng); this.sampler = ZigguratSampler.NormalizedGaussian.of(rng); this.gamma = gamma; this.delta = delta; } /** * @param rng Underlying source of randomness * @param source Source to copy. */ LevyStableSampler(UniformRandomProvider rng, LevyStableSampler source) { super(rng); this.sampler = ZigguratSampler.NormalizedGaussian.of(rng); this.gamma = source.gamma; this.delta = source.delta; } @Override public double sample() { // Levy(Z) = 1 / N(0,1)^2, where N(0,1) is a standard normalized variate final double norm = sampler.sample(); // Here we must transform from the 1-parameterization to the 0-parameterization. // This is a shift of -beta * tan(pi * alpha / 2) = -1 when alpha=0.5, beta=1. final double z = (1.0 / (norm * norm)) - 1.0; // In the 0-parameterization the scale and location are a linear transform. return gamma * z + delta; } @Override public LevyStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new LevyStableSampler(rng, this); } } /** * Implement the generic stable distribution case: {@code alpha < 2} and * {@code beta != 0}. This routine assumes {@code alpha != 1}. * * <p>Implements the Chambers-Mallows-Stuck (CMS) method using the * formula provided in Weron (1996) "On the Chambers-Mallows-Stuck method for * simulating skewed stable random variables" Statistics &amp; Probability * Letters. 28 (2): 165–171. This method is easier to correct from infinite and * NaN results by boxing intermediate infinite values. * * <p>The formula produces a stable deviate from the 1-parameterization that is * discontinuous at {@code alpha=1}. A shift is used to create the 0-parameterization. * This shift is very large as {@code alpha -> 1} and the output loses bits of precision * in the deviate due to cancellation. It is not recommended to use this sampler when * {@code alpha -> 1} except for edge case correction. * * <p>This produces non-NaN output for all parameters alpha, beta, u and w with * the correct orientation for extremes of the distribution support. * The formulas used are symmetric with regard to beta and u. * * @see <a href="https://doi.org/10.1016%2F0167-7152%2895%2900113-1">Weron, R * (1996). Statistics &amp; Probability Letters. 28 (2): 165–171.</a> */ static class WeronStableSampler extends BaseStableSampler { /** Epsilon (1 - alpha). */ protected final double eps; /** Alpha (1 - eps). */ protected final double meps1; /** Cache of expression value used in generation. */ protected final double zeta; /** Cache of expression value used in generation. */ protected final double atanZeta; /** Cache of expression value used in generation. */ protected final double scale; /** 1 / alpha = 1 / (1 - eps). */ protected final double inv1mEps; /** (1 / alpha) - 1 = eps / (1 - eps). */ protected final double epsDiv1mEps; /** The inclusive lower support for the distribution. */ protected final double lower; /** The inclusive upper support for the distribution. */ protected final double upper; /** * @param rng Underlying source of randomness * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. */ WeronStableSampler(UniformRandomProvider rng, double alpha, double beta) { super(rng); eps = 1 - alpha; // When alpha < 0.5, 1 - eps == alpha is not always true as the reverse is not exact. // Here we store 1 - eps in place of alpha. Thus eps + (1 - eps) = 1. meps1 = 1 - eps; // Compute pre-factors for the Weron formula used during error correction. if (meps1 > 1) { // Avoid calling tan outside the domain limit [-pi/2, pi/2]. zeta = beta * Math.tan((2 - meps1) * PI_2); } else { zeta = -beta * Math.tan(meps1 * PI_2); } // Do not store xi = Math.atan(-zeta) / meps1 due to floating-point division errors. // Directly store Math.atan(-zeta). atanZeta = Math.atan(-zeta); scale = Math.pow(1 + zeta * zeta, 0.5 / meps1); // Note: These terms are used interchangeably in formulas // 1 1 // ------- = ----- // (1-eps) alpha inv1mEps = 1.0 / meps1; // 1 eps (1-alpha) 1 // ------- - 1 = ------- = --------- = ----- - 1 // (1-eps) (1-eps) alpha alpha epsDiv1mEps = inv1mEps - 1; // Compute the support. This applies when alpha < 1 and beta = +/-1 if (alpha < 1 && Math.abs(beta) == 1) { if (beta == 1) { // alpha < 0, beta = 1 lower = zeta; upper = UPPER; } else { // alpha < 0, beta = -1 lower = LOWER; upper = zeta; } } else { lower = LOWER; upper = UPPER; } } /** * @param rng Underlying source of randomness * @param source Source to copy. */ WeronStableSampler(UniformRandomProvider rng, WeronStableSampler source) { super(rng); this.eps = source.eps; this.meps1 = source.meps1; this.zeta = source.zeta; this.atanZeta = source.atanZeta; this.scale = source.scale; this.inv1mEps = source.inv1mEps; this.epsDiv1mEps = source.epsDiv1mEps; this.lower = source.lower; this.upper = source.upper; } @Override public double sample() { final double phi = getPhi(); final double w = getOmega(); return createSample(phi, w); } /** * Create the sample. This routine is robust to edge cases and returns a deviate * at the extremes of the support. It correctly handles {@code alpha -> 0} when * the sample is increasingly likely to be +/- infinity. * * @param phi Uniform deviate in {@code (-pi/2, pi/2)} * @param w Exponential deviate * @return x */ protected double createSample(double phi, double w) { // Here we use the formula provided by Weron for the 1-parameterization. // Note: Adding back zeta creates the 0-parameterization defined in Nolan (1998): // X ~ S0_alpha(s,beta,u0) with s=1, u0=0 for a standard random variable. // As alpha -> 1 the translation zeta to create the stable deviate // in the 0-parameterization is increasingly large as tan(pi/2) -> infinity. // The max translation is approximately 1e16. // Without this translation the stable deviate is in the 1-parameterization // and the function is not continuous with respect to alpha. // Due to the large zeta when alpha -> 1 the number of bits of the output variable // are very low due to cancellation. // As alpha -> 0 or 2 then zeta -> 0 and cancellation is not relevant. // The formula can be modified for infinite terms to compute a result for extreme // deviates u and w when the CMS formula fails. // Note the following term is subject to floating point error: // xi = atan(-zeta) / alpha // alphaPhiXi = alpha * (phi + xi) // This is required: cos(phi - alphaPhiXi) > 0 => phi - alphaPhiXi in (-pi/2, pi/2). // Thus we compute atan(-zeta) and use it to compute two terms: // [1] alpha * (phi + xi) = alpha * (phi + atan(-zeta) / alpha) = alpha * phi + atan(-zeta) // [2] phi - alpha * (phi + xi) = phi - alpha * phi - atan(-zeta) = (1-alpha) * phi - atan(-zeta) // Compute terms // Either term can be infinite or 0. Certain parameters compute 0 * inf. // t1=inf occurs alpha -> 0. // t1=0 occurs when beta = tan(-alpha * phi) / tan(alpha * pi / 2). // t2=inf occurs when w -> 0 and alpha -> 0. // t2=0 occurs when alpha -> 0 and phi -> pi/2. // Detect zeros and return as zeta. // Note sin(alpha * phi + atanZeta) is zero when: // alpha * phi = -atan(-zeta) // tan(-alpha * phi) = -zeta // = beta * tan(alpha * pi / 2) // Since |phi| < pi/2 this requires beta to have an opposite sign to phi // and a magnitude < 1. This is possible and in this case avoid a possible // 0 / 0 by setting the result as if term t1=0 and the result is zeta. double t1 = Math.sin(meps1 * phi + atanZeta); if (t1 == 0) { return zeta; } // Since cos(phi) is in (0, 1] this term will not create a // large magnitude to create t1 = 0. t1 /= Math.pow(Math.cos(phi), inv1mEps); // Iff Math.cos(eps * phi - atanZeta) is zero then 0 / 0 can occur if w=0. // Iff Math.cos(eps * phi - atanZeta) is below zero then NaN will occur // in the power function. These cases are avoided by phi=(-pi/2, pi/2) and direct // use of arctan(-zeta). final double t2 = Math.pow(Math.cos(eps * phi - atanZeta) / w, epsDiv1mEps); if (t2 == 0) { return zeta; } final double x = t1 * t2 * scale + zeta; // Check the bounds. Applies when alpha < 1 and beta = +/-1. if (x <= lower) { return lower; } return x < upper ? x : upper; } @Override public WeronStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new WeronStableSampler(rng, this); } } /** * Implement the generic stable distribution case: {@code alpha < 2} and * {@code beta != 0}. This routine assumes {@code alpha != 1}. * * <p>Implements the Chambers-Mallows-Stuck (CMS) method from Chambers, et al * (1976) A Method for Simulating Stable Random Variables. Journal of the * American Statistical Association Vol. 71, No. 354, pp. 340-344. * * <p>The formula produces a stable deviate from the 0-parameterization that is * continuous at {@code alpha=1}. * * <p>This is an implementation of the Fortran routine RSTAB. In the event the * computation fails then an alternative computation is performed using the * formula provided in Weron (1996) "On the Chambers-Mallows-Stuck method for * simulating skewed stable random variables" Statistics &amp; Probability * Letters. 28 (2): 165–171. This method is easier to correct from infinite and * NaN results. The error correction path is extremely unlikely to occur during * use unless {@code alpha -> 0}. In general use it requires the random deviates * w or u are extreme. See the unit tests for conditions that create them. * * <p>This produces non-NaN output for all parameters alpha, beta, u and w with * the correct orientation for extremes of the distribution support. * The formulas used are symmetric with regard to beta and u. */ static class CMSStableSampler extends WeronStableSampler { /** 1/2. */ private static final double HALF = 0.5; /** Cache of expression value used in generation. */ private final double tau; /** * @param rng Underlying source of randomness * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. */ CMSStableSampler(UniformRandomProvider rng, double alpha, double beta) { super(rng, alpha, beta); // Compute the RSTAB pre-factor. tau = getTau(alpha, beta); } /** * @param rng Underlying source of randomness * @param source Source to copy. */ CMSStableSampler(UniformRandomProvider rng, CMSStableSampler source) { super(rng, source); this.tau = source.tau; } /** * Gets tau. This is a factor used in the CMS algorithm. If this is zero then * a special case of {@code beta -> 0} has occurred. * * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @return tau */ static double getTau(double alpha, double beta) { final double eps = 1 - alpha; final double meps1 = 1 - eps; // Compute RSTAB prefactor double tau; // tau is symmetric around alpha=1 // tau -> beta / pi/2 as alpha -> 1 // tau -> 0 as alpha -> 2 or 0 // Avoid calling tan as the value approaches the domain limit [-pi/2, pi/2]. if (Math.abs(eps) < HALF) { // 0.5 < alpha < 1.5. Note: This works when eps=0 as tan(0) / 0 == 1. tau = beta / (SpecialMath.tan2(eps * PI_2) * PI_2); } else { // alpha >= 1.5 or alpha <= 0.5. // Do not call tan with alpha > 1 as it wraps in the domain [-pi/2, pi/2]. // Since pi is approximate the symmetry is lost by wrapping. // Keep within the domain using (2-alpha). if (meps1 > 1) { tau = beta * PI_2 * eps * (2 - meps1) * -SpecialMath.tan2((2 - meps1) * PI_2); } else { tau = beta * PI_2 * eps * meps1 * SpecialMath.tan2(meps1 * PI_2); } } return tau; } @Override public double sample() { final double phiby2 = getPhiBy2(); final double w = getOmega(); // Compute as per the RSTAB routine. // Generic stable distribution that is continuous as alpha -> 1. // This is a trigonomic rearrangement of equation 4.1 from Chambers et al (1976) // as implemented in the Fortran program RSTAB. // Uses the special functions: // tan2 = tan(x) / x // d2 = (exp(x) - 1) / x // The method is implemented as per the RSTAB routine with the exceptions: // 1. The function tan2(x) is implemented with a higher precision approximation. // 2. The sample is tested against the expected distribution support. // Infinite intermediate terms that create infinite or NaN are corrected by // switching the formula and handling infinite terms. // Compute some tangents // a in (-1, 1) // bb in [1, 4/pi) // b in (-1, 1) final double a = phiby2 * SpecialMath.tan2(phiby2); final double bb = SpecialMath.tan2(eps * phiby2); final double b = eps * phiby2 * bb; // Compute some necessary subexpressions final double da = a * a; final double db = b * b; // a2 in (0, 1] final double a2 = 1 - da; // a2p in [1, 2) final double a2p = 1 + da; // b2 in (0, 1] final double b2 = 1 - db; // b2p in [1, 2) final double b2p = 1 + db; // Compute coefficient. // numerator=0 is not possible *in theory* when the uniform deviate generating phi // is in the open interval (0, 1). In practice it is possible to obtain <=0 due // to round-off error, typically when beta -> +/-1 and phiby2 -> -/+pi/4. // This can happen for any alpha. final double z = a2p * (b2 + 2 * phiby2 * bb * tau) / (w * a2 * b2p); // Compute the exponential-type expression // Note: z may be infinite, typically when w->0 and a2->0. // This can produce NaN under certain parameterizations due to multiplication by 0. final double alogz = Math.log(z); final double d = SpecialMath.d2(epsDiv1mEps * alogz) * (alogz * inv1mEps); // Pre-compute the multiplication factor. // The numerator may be zero. The denominator is not zero as a2 is bounded to // above zero when the uniform deviate that generates phiby2 is not 0 or 1. // The min value of a2 is 2^-52. Assume f cannot be infinite as the numerator // is computed with a in (-1, 1); b in (-1, 1); phiby2 in (-pi/4, pi/4); tau in // [-2/pi, 2/pi]; bb in [1, 4/pi); a2 in (0, 1] limiting the numerator magnitude. final double f = (2 * ((a - b) * (1 + a * b) - phiby2 * tau * bb * (b * a2 - 2 * a))) / (a2 * b2p); // Compute the stable deviate: final double x = (1 + eps * d) * f + tau * d; // Test the support if (lower < x && x < upper) { return x; } // Error correction path: // x is at the bounds, infinite or NaN (created by 0 / 0, 0 * inf, or inf - inf). // This is caused by extreme parameterizations of alpha or beta, or extreme values // from the random deviates. // Alternatively alpha < 1 and beta = +/-1 and the sample x is at the edge or // outside the support due to floating point error. // Here we use the formula provided by Weron which is easier to correct // when deviates are extreme or alpha -> 0. The formula is not continuous // as alpha -> 1 without a shift which reduces the precision of the sample; // for rare edge case correction this has minimal effect on sampler output. return createSample(phiby2 * 2, w); } @Override public CMSStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new CMSStableSampler(rng, this); } } /** * Implement the stable distribution case: {@code alpha == 1} and {@code beta != 0}. * * <p>Implements the same algorithm as the {@link CMSStableSampler} with * the {@code alpha} assumed to be 1. * * <p>This sampler specifically requires that {@code beta / (pi/2) != 0}; otherwise * the parameters equal {@code alpha=1, beta=0} as the Cauchy distribution case. */ static class Alpha1CMSStableSampler extends BaseStableSampler { /** Cache of expression value used in generation. */ private final double tau; /** * @param rng Underlying source of randomness * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. */ Alpha1CMSStableSampler(UniformRandomProvider rng, double beta) { super(rng); tau = beta / PI_2; } /** * @param rng Underlying source of randomness * @param source Source to copy. */ Alpha1CMSStableSampler(UniformRandomProvider rng, Alpha1CMSStableSampler source) { super(rng); this.tau = source.tau; } @Override public double sample() { final double phiby2 = getPhiBy2(); final double w = getOmega(); // Compute some tangents final double a = phiby2 * SpecialMath.tan2(phiby2); // Compute some necessary subexpressions final double da = a * a; final double a2 = 1 - da; final double a2p = 1 + da; // Compute coefficient. // numerator=0 is not possible when the uniform deviate generating phi // is in the open interval (0, 1) and alpha=1. final double z = a2p * (1 + 2 * phiby2 * tau) / (w * a2); // Compute the exponential-type expression // Note: z may be infinite, typically when w->0 and a2->0. // This can produce NaN under certain parameterizations due to multiplication by 0. // When alpha=1 the expression // d = d2((eps / (1-eps)) * alogz) * (alogz / (1-eps)) is eliminated to 1 * log(z) final double d = Math.log(z); // Pre-compute the multiplication factor. final double f = (2 * (a - phiby2 * tau * (-2 * a))) / a2; // Compute the stable deviate: // This does not require correction as f is finite (as per the alpha != 1 case), // tau is non-zero and only d can be infinite due to an extreme w -> 0. return f + tau * d; } @Override public Alpha1CMSStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new Alpha1CMSStableSampler(rng, this); } } /** * Implement the generic stable distribution case: {@code alpha < 2} and {@code beta == 0}. * * <p>Implements the same algorithm as the {@link WeronStableSampler} with * the {@code beta} assumed to be 0. * * <p>This routine assumes {@code alpha != 1}; {@code alpha=1, beta=0} is the Cauchy * distribution case. */ static class Beta0WeronStableSampler extends BaseStableSampler { /** Epsilon (1 - alpha). */ protected final double eps; /** Epsilon (1 - alpha). */ protected final double meps1; /** 1 / alpha = 1 / (1 - eps). */ protected final double inv1mEps; /** (1 / alpha) - 1 = eps / (1 - eps). */ protected final double epsDiv1mEps; /** * @param rng Underlying source of randomness * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. */ Beta0WeronStableSampler(UniformRandomProvider rng, double alpha) { super(rng); eps = 1 - alpha; meps1 = 1 - eps; inv1mEps = 1.0 / meps1; epsDiv1mEps = inv1mEps - 1; } /** * @param rng Underlying source of randomness * @param source Source to copy. */ Beta0WeronStableSampler(UniformRandomProvider rng, Beta0WeronStableSampler source) { super(rng); this.eps = source.eps; this.meps1 = source.meps1; this.inv1mEps = source.inv1mEps; this.epsDiv1mEps = source.epsDiv1mEps; } @Override public double sample() { final double phi = getPhi(); final double w = getOmega(); return createSample(phi, w); } /** * Create the sample. This routine is robust to edge cases and returns a deviate * at the extremes of the support. It correctly handles {@code alpha -> 0} when * the sample is increasingly likely to be +/- infinity. * * @param phi Uniform deviate in {@code (-pi/2, pi/2)} * @param w Exponential deviate * @return x */ protected double createSample(double phi, double w) { // As per the Weron sampler with beta=0 and terms eliminated. // Note that if alpha=1 this reduces to sin(phi) / cos(phi) => Cauchy case. // Compute terms. // Either term can be infinite or 0. Certain parameters compute 0 * inf. // Detect zeros and return as 0. // Note sin(alpha * phi) is only ever zero when phi=0. No value of alpha // multiplied by small phi can create zero due to the limited // precision of alpha imposed by alpha = 1 - (1-alpha). At this point cos(phi) = 1. // Thus 0/0 cannot occur. final double t1 = Math.sin(meps1 * phi) / Math.pow(Math.cos(phi), inv1mEps); if (t1 == 0) { return 0; } final double t2 = Math.pow(Math.cos(eps * phi) / w, epsDiv1mEps); if (t2 == 0) { return 0; } return t1 * t2; } @Override public Beta0WeronStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new Beta0WeronStableSampler(rng, this); } } /** * Implement the generic stable distribution case: {@code alpha < 2} and {@code beta == 0}. * * <p>Implements the same algorithm as the {@link CMSStableSampler} with * the {@code beta} assumed to be 0. * * <p>This routine assumes {@code alpha != 1}; {@code alpha=1, beta=0} is the Cauchy * distribution case. */ static class Beta0CMSStableSampler extends Beta0WeronStableSampler { /** * @param rng Underlying source of randomness * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. */ Beta0CMSStableSampler(UniformRandomProvider rng, double alpha) { super(rng, alpha); } /** * @param rng Underlying source of randomness * @param source Source to copy. */ Beta0CMSStableSampler(UniformRandomProvider rng, Beta0CMSStableSampler source) { super(rng, source); } @Override public double sample() { final double phiby2 = getPhiBy2(); final double w = getOmega(); // Compute some tangents final double a = phiby2 * SpecialMath.tan2(phiby2); final double b = eps * phiby2 * SpecialMath.tan2(eps * phiby2); // Compute some necessary subexpressions final double da = a * a; final double db = b * b; final double a2 = 1 - da; final double a2p = 1 + da; final double b2 = 1 - db; final double b2p = 1 + db; // Compute coefficient. final double z = a2p * b2 / (w * a2 * b2p); // Compute the exponential-type expression final double alogz = Math.log(z); final double d = SpecialMath.d2(epsDiv1mEps * alogz) * (alogz * inv1mEps); // Pre-compute the multiplication factor. // The numerator may be zero. The denominator is not zero as a2 is bounded to // above zero when the uniform deviate that generates phiby2 is not 0 or 1. final double f = (2 * ((a - b) * (1 + a * b))) / (a2 * b2p); // Compute the stable deviate: final double x = (1 + eps * d) * f; // Test the support if (LOWER < x && x < UPPER) { return x; } // Error correction path. // Here we use the formula provided by Weron which is easier to correct // when deviates are extreme or alpha -> 0. return createSample(phiby2 * 2, w); } @Override public Beta0CMSStableSampler withUniformRandomProvider(UniformRandomProvider rng) { return new Beta0CMSStableSampler(rng, this); } } /** * Implement special math functions required by the CMS algorithm. */ static final class SpecialMath { /** pi/4. */ private static final double PI_4 = Math.PI / 4; /** 4/pi. */ private static final double FOUR_PI = 4 / Math.PI; /** tan2 product constant. */ private static final double P0 = -0.5712939549476836914932149599e10; /** tan2 product constant. */ private static final double P1 = 0.4946855977542506692946040594e9; /** tan2 product constant. */ private static final double P2 = -0.9429037070546336747758930844e7; /** tan2 product constant. */ private static final double P3 = 0.5282725819868891894772108334e5; /** tan2 product constant. */ private static final double P4 = -0.6983913274721550913090621370e2; /** tan2 quotient constant. */ private static final double Q0 = -0.7273940551075393257142652672e10; /** tan2 quotient constant. */ private static final double Q1 = 0.2125497341858248436051062591e10; /** tan2 quotient constant. */ private static final double Q2 = -0.8000791217568674135274814656e8; /** tan2 quotient constant. */ private static final double Q3 = 0.8232855955751828560307269007e6; /** tan2 quotient constant. */ private static final double Q4 = -0.2396576810261093558391373322e4; /** * The threshold to switch to using {@link Math#expm1(double)}. The following * table shows the mean (max) ULP difference between using expm1 and exp using * random +/-x with different exponents (n=2^30): * * <pre> * x exp positive x negative x * 64.0 6 0.10004021506756544 (2) 0.0 (0) * 32.0 5 0.11177831795066595 (2) 0.0 (0) * 16.0 4 0.0986650362610817 (2) 9.313225746154785E-10 (1) * 8.0 3 0.09863092936575413 (2) 4.9658119678497314E-6 (1) * 4.0 2 0.10015273280441761 (2) 4.547201097011566E-4 (1) * 2.0 1 0.14359260816127062 (2) 0.005623611621558666 (2) * 1.0 0 0.20160607434809208 (2) 0.03312791418284178 (2) * 0.5 -1 0.3993037799373269 (2) 0.28186883218586445 (2) * 0.25 -2 0.6307008266448975 (2) 0.5192863345146179 (2) * 0.125 -3 1.3862918205559254 (4) 1.386285437270999 (4) * 0.0625 -4 2.772640804760158 (8) 2.772612397558987 (8) * </pre> * * <p>The threshold of 0.5 has a mean ULP below 0.5 and max ULP of 2. The * transition is monotonic. Neither is true for the next threshold of 0.25. */ private static final double SWITCH_TO_EXPM1 = 0.5; /** No instances. */ private SpecialMath() {} /** * Evaluate {@code (exp(x) - 1) / x}. For {@code x} in the range {@code [-inf, inf]} returns * a result in {@code [0, inf]}. * * <ul> * <li>For {@code x=-inf} this returns {@code 0}. * <li>For {@code x=0} this returns {@code 1}. * <li>For {@code x=inf} this returns {@code inf}. * <li>For {@code x=nan} this returns {@code nan}. * </ul> * * <p> This corrects {@code 0 / 0} and {@code inf / inf} division from * {@code NaN} to either {@code 1} or the upper bound respectively. * * @param x value to evaluate * @return {@code (exp(x) - 1) / x}. */ static double d2(double x) { // Here expm1 is only used when use of expm1 and exp consistently // compute different results by more than 0.5 ULP. if (Math.abs(x) < SWITCH_TO_EXPM1) { // Deliberate comparison to floating-point zero if (x == 0) { // Avoid 0 / 0 error return 1.0; } return Math.expm1(x) / x; } // No use of expm1. Accuracy as x moves away from 0 is not required as the result // is divided by x and the accuracy of the final result is within a few ULP. if (x < Double.POSITIVE_INFINITY) { return (Math.exp(x) - 1) / x; } // Upper bound (or NaN) return x; } /** * Evaluate {@code tan(x) / x}. * * <p>For {@code x} in the range {@code [0, pi/4]} this returns * a value in the range {@code [1, 4 / pi]}. * * <p>The following properties are desirable for the CMS algorithm: * * <ul> * <li>For {@code x=0} this returns {@code 1}. * <li>For {@code x=pi/4} this returns {@code 4/pi}. * <li>For {@code x=pi/4} this multiplied by {@code x} returns {@code 1}. * </ul> * * <p>This method is called by the CMS algorithm when {@code x < pi/4}. * In this case the method is almost as accurate as {@code Math.tan(x) / x}, does * not require checking for {@code x=0} and is faster. * * @param x the x * @return {@code tan(x) / x}. */ static double tan2(double x) { if (Math.abs(x) > PI_4) { // Reduction is not supported. Delegate to the JDK. return Math.tan(x) / x; } // Testing with approximation 4283 from Hart et al, as used in the RSTAB // routine, showed the method was not accurate enough for use with // double computation. Hart et al state it has max relative error = 1e-10.66. // For tan(x) / x with x in [0, pi/4] values outside [1, 4/pi] were computed. // When testing verses Math.tan(x) the mean ULP difference is 93436.3. // Approximation 4288 from Hart et al (1968, P. 252). // Max relative error = 1e-26.68 (for tan(x)). // When testing verses Math.tan(x) the mean ULP difference is 0.590597. // The approximation is defined as: // tan(x*pi/4) = x * P(x^2) / Q(x^2) // with P and Q polynomials of x squared. // // To create tan(x): // tan(x) = xi * P(xi^2) / Q(xi^2), xi = x * 4/pi // tan(x) / x = xi * P(xi^2) / Q(xi^2) / x // tan(x) / x = 4/pi * (P(xi^2) / Q(xi^2)) // = P(xi^2) / (pi/4 * Q(xi^2)) // The later has a smaller mean ULP difference to Math.tan(x) / x. final double xi = x * FOUR_PI; // Use the power form with a reverse summation order to have smaller // magnitude terms first. Note: x < 1 so greater powers are smaller. // This has essentially the same accuracy as the nested form of the polynomials // for a marginal performance increase. See JMH examples for performance tests. final double x2 = xi * xi; final double x4 = x2 * x2; final double x6 = x4 * x2; final double x8 = x4 * x4; return (x8 * P4 + x6 * P3 + x4 * P2 + x2 * P1 + P0) / (PI_4 * (x8 * x2 + x8 * Q4 + x6 * Q3 + x4 * Q2 + x2 * Q1 + Q0)); } } /** * @param rng Generator of uniformly distributed random numbers. */ StableSampler(UniformRandomProvider rng) { this.rng = rng; } /** * Generate a sample from a stable distribution. * * <p>The distribution uses the 0-parameterization: S(alpha, beta, gamma, delta; 0). */ @Override public abstract double sample(); /** {@inheritDoc} */ // Redeclare the signature to return a StableSampler not a SharedStateContinuousSampler @Override public abstract StableSampler withUniformRandomProvider(UniformRandomProvider rng); /** * Generates a {@code long} value. * Used by algorithm implementations without exposing access to the RNG. * * @return the next random value */ long nextLong() { return rng.nextLong(); } /** {@inheritDoc} */ @Override public String toString() { // All variations use the same string representation, i.e. no changes // for the Gaussian, Levy or Cauchy case. return "Stable deviate [" + rng.toString() + "]"; } /** * Creates a standardized sampler of a stable distribution with zero location and unit scale. * * <p>Special cases: * * <ul> * <li>{@code alpha=2} returns a Gaussian distribution sampler with * {@code mean=0} and {@code variance=2} (Note: {@code beta} has no effect on the distribution). * <li>{@code alpha=1} and {@code beta=0} returns a Cauchy distribution sampler with * {@code location=0} and {@code scale=1}. * <li>{@code alpha=0.5} and {@code beta=1} returns a Levy distribution sampler with * {@code location=-1} and {@code scale=1}. This location shift is due to the * 0-parameterization of the stable distribution. * </ul> * * <p>Note: To allow the computation of the stable distribution the parameter alpha * is validated using {@code 1 - alpha} in the interval {@code [-1, 1)}. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @return the sampler * @throws IllegalArgumentException if {@code 1 - alpha < -1}; or {@code 1 - alpha >= 1}; * or {@code beta < -1}; or {@code beta > 1}. */ public static StableSampler of(UniformRandomProvider rng, double alpha, double beta) { validateParameters(alpha, beta); return create(rng, alpha, beta); } /** * Creates a sampler of a stable distribution. This applies a transformation to the * standardized sampler. * * <p>The random variable \( X \) has * the stable distribution \( S(\alpha, \beta, \gamma, \delta; 0) \) if: * * <p>\[ X = \gamma Z_0 + \delta \] * * <p>where \( Z_0 = S(\alpha, \beta; 0) \) is a standardized stable distribution. * * <p>Note: To allow the computation of the stable distribution the parameter alpha * is validated using {@code 1 - alpha} in the interval {@code [-1, 1)}. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @param gamma Scale parameter. Must be strictly positive and finite. * @param delta Location parameter. Must be finite. * @return the sampler * @throws IllegalArgumentException if {@code 1 - alpha < -1}; or {@code 1 - alpha >= 1}; * or {@code beta < -1}; or {@code beta > 1}; or {@code gamma <= 0}; or * {@code gamma} or {@code delta} are not finite. * @see #of(UniformRandomProvider, double, double) */ public static StableSampler of(UniformRandomProvider rng, double alpha, double beta, double gamma, double delta) { validateParameters(alpha, beta, gamma, delta); // Choose the algorithm. // Reuse the special cases as they have transformation support. if (alpha == ALPHA_GAUSSIAN) { // Note: beta has no effect and is ignored. return new GaussianStableSampler(rng, gamma, delta); } // Note: As beta -> 0 the result cannot be computed differently to beta = 0. if (alpha == ALPHA_CAUCHY && CMSStableSampler.getTau(ALPHA_CAUCHY, beta) == TAU_ZERO) { return new CauchyStableSampler(rng, gamma, delta); } if (alpha == ALPHA_LEVY && Math.abs(beta) == BETA_LEVY) { // Support mirroring for negative beta by inverting the beta=1 Levy sample // using a negative gamma. Note: The delta is not mirrored as it is a shift // applied to the scaled and mirrored distribution. return new LevyStableSampler(rng, beta * gamma, delta); } // Standardized sampler final StableSampler sampler = create(rng, alpha, beta); // Transform return new TransformedStableSampler(sampler, gamma, delta); } /** * Creates a standardized sampler of a stable distribution with zero location and unit scale. * * @param rng Generator of uniformly distributed random numbers. * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @return the sampler */ private static StableSampler create(UniformRandomProvider rng, double alpha, double beta) { // Choose the algorithm. // The special case samplers have transformation support and use gamma=1.0, delta=0.0. // As alpha -> 0 the computation increasingly requires correction // of infinity to the distribution support. if (alpha == ALPHA_GAUSSIAN) { // Note: beta has no effect and is ignored. return new GaussianStableSampler(rng, GAMMA_1, DELTA_0); } // Note: As beta -> 0 the result cannot be computed differently to beta = 0. // This is based on the computation factor tau: final double tau = CMSStableSampler.getTau(alpha, beta); if (tau == TAU_ZERO) { // Symmetric case (beta skew parameter is effectively zero) if (alpha == ALPHA_CAUCHY) { return new CauchyStableSampler(rng, GAMMA_1, DELTA_0); } if (alpha <= ALPHA_SMALL) { // alpha -> 0 requires robust error correction return new Beta0WeronStableSampler(rng, alpha); } return new Beta0CMSStableSampler(rng, alpha); } // Here beta is significant. if (alpha == 1) { return new Alpha1CMSStableSampler(rng, beta); } if (alpha == ALPHA_LEVY && Math.abs(beta) == BETA_LEVY) { // Support mirroring for negative beta by inverting the beta=1 Levy sample // using a negative gamma. Note: The delta is not mirrored as it is a shift // applied to the scaled and mirrored distribution. return new LevyStableSampler(rng, beta, DELTA_0); } if (alpha <= ALPHA_SMALL) { // alpha -> 0 requires robust error correction return new WeronStableSampler(rng, alpha, beta); } return new CMSStableSampler(rng, alpha, beta); } /** * Validate the parameters are in the correct range. * * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @throws IllegalArgumentException if {@code 1 - alpha < -1}; or {@code 1 - alpha >= 1}; * or {@code beta < -1}; or {@code beta > 1}. */ private static void validateParameters(double alpha, double beta) { // The epsilon (1-alpha) value must be in the interval [-1, 1). // Logic inversion will identify NaN final double eps = 1 - alpha; if (!(-1 <= eps && eps < 1)) { throw new IllegalArgumentException("alpha is not in the interval (0, 2]: " + alpha); } if (!(-1 <= beta && beta <= 1)) { throw new IllegalArgumentException("beta is not in the interval [-1, 1]: " + beta); } } /** * Validate the parameters are in the correct range. * * @param alpha Stability parameter. Must be in the interval {@code (0, 2]}. * @param beta Skewness parameter. Must be in the interval {@code [-1, 1]}. * @param gamma Scale parameter. Must be strictly positive and finite. * @param delta Location parameter. Must be finite. * @throws IllegalArgumentException if {@code 1 - alpha < -1}; or {@code 1 - alpha >= 1}; * or {@code beta < -1}; or {@code beta > 1}; or {@code gamma <= 0}; or * {@code gamma} or {@code delta} are not finite. */ private static void validateParameters(double alpha, double beta, double gamma, double delta) { validateParameters(alpha, beta); // Logic inversion will identify NaN if (!(0 < gamma && gamma <= Double.MAX_VALUE)) { throw new IllegalArgumentException("gamma is not strictly positive and finite: " + gamma); } if (!Double.isFinite(delta)) { throw new IllegalArgumentException("delta is not finite: " + delta); } } }
3,019
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ContinuousSampler.java
/* * 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.rng.sampling.distribution; import java.util.stream.DoubleStream; /** * Sampler that generates values of type {@code double}. * * @since 1.0 */ public interface ContinuousSampler { /** * Creates a {@code double} sample. * * @return a sample. */ double sample(); /** * Returns an effectively unlimited stream of {@code double} sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(). * * @return a stream of {@code double} values. * @since 1.5 */ default DoubleStream samples() { return DoubleStream.generate(this::sample).sequential(); } /** * Returns a stream producing the given {@code streamSize} number of {@code double} * sample values. * * <p>The default implementation produces a sequential stream that repeatedly * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. * * @param streamSize Number of values to generate. * @return a stream of {@code double} values. * @since 1.5 */ default DoubleStream samples(long streamSize) { return samples().limit(streamSize); } }
3,020
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/package-info.java
/* * 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. */ /** * This package contains classes for sampling from statistical distributions. * * <p>As of version 1.0, the code for specific distributions was adapted from * the corresponding classes in the development version of "Commons Math" (in * package {@code org.apache.commons.math4.distribution}). * </p> * <p> * When no specific algorithm is provided, one can still sample from any * distribution, using the <em>inverse method</em>, as illustrated in: * </p> * <ul> * <li>{@link org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler InverseTransformDiscreteSampler}</li> * <li>{@link org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler InverseTransformContinuousSampler}</li> * </ul> * * <p> * Algorithms are described in e.g. Luc Devroye (1986), <a href="http://luc.devroye.org/chapter_nine.pdf">chapter 9</a> * and <a href="http://luc.devroye.org/chapter_ten.pdf">chapter 10</a>. * This <a href="http://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf">paper</a> discusses Gaussian generators. * </p> */ package org.apache.commons.rng.sampling.distribution;
3,021
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GuideTableDiscreteSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Compute a sample from {@code n} values each with an associated probability. If all unique items * are assigned the same probability it is more efficient to use the {@link DiscreteUniformSampler}. * * <p>The cumulative probability distribution is searched using a guide table to set an * initial start point. This implementation is based on:</p> * * <blockquote> * Devroye, Luc (1986). Non-Uniform Random Variate Generation. * New York: Springer-Verlag. Chapter 3.2.4 "The method of guide tables" p. 96. * </blockquote> * * <p>The size of the guide table can be controlled using a parameter. A larger guide table * will improve performance at the cost of storage space.</p> * * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> * * @see <a href="http://en.wikipedia.org/wiki/Probability_distribution#Discrete_probability_distribution"> * Discrete probability distribution (Wikipedia)</a> * @since 1.3 */ public final class GuideTableDiscreteSampler implements SharedStateDiscreteSampler { /** The default value for {@code alpha}. */ private static final double DEFAULT_ALPHA = 1.0; /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** * The cumulative probability table ({@code f(x)}). */ private final double[] cumulativeProbabilities; /** * The inverse cumulative probability guide table. This is a guide map between the cumulative * probability (f(x)) and the value x. It is used to set the initial point for search * of the cumulative probability table. * * <p>The index in the map is obtained using {@code p * map.length} where {@code p} is the * known cumulative probability {@code f(x)} or a uniform random deviate {@code u}. The value * stored at the index is value {@code x+1} when {@code p = f(x)} such that it is the * exclusive upper bound on the sample value {@code x} for searching the cumulative probability * table {@code f(x)}. The search of the cumulative probability is towards zero.</p> */ private final int[] guideTable; /** * @param rng Generator of uniformly distributed random numbers. * @param cumulativeProbabilities The cumulative probability table ({@code f(x)}). * @param guideTable The inverse cumulative probability guide table. */ private GuideTableDiscreteSampler(UniformRandomProvider rng, double[] cumulativeProbabilities, int[] guideTable) { this.rng = rng; this.cumulativeProbabilities = cumulativeProbabilities; this.guideTable = guideTable; } /** {@inheritDoc} */ @Override public int sample() { // Compute a probability final double u = rng.nextDouble(); // Initialise the search using the guide table to find an initial guess. // The table provides an upper bound on the sample (x+1) for a known // cumulative probability (f(x)). int x = guideTable[getGuideTableIndex(u, guideTable.length)]; // Search down. // In the edge case where u is 1.0 then 'x' will be 1 outside the range of the // cumulative probability table and this will decrement to a valid range. // In the case where 'u' is mapped to the same guide table index as a lower // cumulative probability f(x) (due to rounding down) then this will not decrement // and return the exclusive upper bound (x+1). while (x != 0 && u <= cumulativeProbabilities[x - 1]) { x--; } return x; } /** {@inheritDoc} */ @Override public String toString() { return "Guide table deviate [" + rng.toString() + "]"; } /** {@inheritDoc} */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new GuideTableDiscreteSampler(rng, cumulativeProbabilities, guideTable); } /** * Create a new sampler for an enumerated distribution using the given {@code probabilities}. * The samples corresponding to each probability are assumed to be a natural sequence * starting at zero. * * <p>The size of the guide table is {@code probabilities.length}.</p> * * @param rng Generator of uniformly distributed random numbers. * @param probabilities The probabilities. * @return the sampler * @throws IllegalArgumentException if {@code probabilities} is null or empty, a * probability is negative, infinite or {@code NaN}, or the sum of all * probabilities is not strictly positive. */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double[] probabilities) { return of(rng, probabilities, DEFAULT_ALPHA); } /** * Create a new sampler for an enumerated distribution using the given {@code probabilities}. * The samples corresponding to each probability are assumed to be a natural sequence * starting at zero. * * <p>The size of the guide table is {@code alpha * probabilities.length}.</p> * * @param rng Generator of uniformly distributed random numbers. * @param probabilities The probabilities. * @param alpha The alpha factor used to set the guide table size. * @return the sampler * @throws IllegalArgumentException if {@code probabilities} is null or empty, a * probability is negative, infinite or {@code NaN}, the sum of all * probabilities is not strictly positive, or {@code alpha} is not strictly positive. */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, double[] probabilities, double alpha) { validateParameters(probabilities, alpha); final int size = probabilities.length; final double[] cumulativeProbabilities = new double[size]; double sumProb = 0; int count = 0; for (final double prob : probabilities) { InternalUtils.validateProbability(prob); // Compute and store cumulative probability. sumProb += prob; cumulativeProbabilities[count++] = sumProb; } if (Double.isInfinite(sumProb) || sumProb <= 0) { throw new IllegalArgumentException("Invalid sum of probabilities: " + sumProb); } // Note: The guide table is at least length 1. Compute the size avoiding overflow // in case (alpha * size) is too large. final int guideTableSize = (int) Math.ceil(alpha * size); final int[] guideTable = new int[Math.max(guideTableSize, guideTableSize + 1)]; // Compute and store cumulative probability. for (int x = 0; x < size; x++) { final double norm = cumulativeProbabilities[x] / sumProb; cumulativeProbabilities[x] = (norm < 1) ? norm : 1.0; // Set the guide table value as an exclusive upper bound (x + 1) final int index = getGuideTableIndex(cumulativeProbabilities[x], guideTable.length); guideTable[index] = x + 1; } // Edge case for round-off cumulativeProbabilities[size - 1] = 1.0; // The final guide table entry is (maximum value of x + 1) guideTable[guideTable.length - 1] = size; // The first non-zero value in the guide table is from f(x=0). // Any probabilities mapped below this must be sample x=0 so the // table may initially be filled with zeros. // Fill missing values in the guide table. for (int i = 1; i < guideTable.length; i++) { guideTable[i] = Math.max(guideTable[i - 1], guideTable[i]); } return new GuideTableDiscreteSampler(rng, cumulativeProbabilities, guideTable); } /** * Validate the parameters. * * @param probabilities The probabilities. * @param alpha The alpha factor used to set the guide table size. * @throws IllegalArgumentException if {@code probabilities} is null or empty, or * {@code alpha} is not strictly positive. */ private static void validateParameters(double[] probabilities, double alpha) { if (probabilities == null || probabilities.length == 0) { throw new IllegalArgumentException("Probabilities must not be empty."); } if (alpha <= 0) { throw new IllegalArgumentException("Alpha must be strictly positive."); } } /** * Gets the guide table index for the probability. This is obtained using * {@code p * (tableLength - 1)} so is inside the length of the table. * * @param p Cumulative probability. * @param tableLength Table length. * @return the guide table index. */ private static int getGuideTableIndex(double p, int tableLength) { // Note: This is only ever called when p is in the range of the cumulative // probability table. So assume 0 <= p <= 1. return (int) (p * (tableLength - 1)); } }
3,022
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformDiscreteSampler.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; /** * Distribution sampler that uses the * <a href="https://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. * * It can be used to sample any distribution that provides access to its * <em>inverse cumulative probability function</em>. * * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> * * <p>Example:</p> * <pre><code> * import org.apache.commons.math3.distribution.IntegerDistribution; * import org.apache.commons.math3.distribution.BinomialDistribution; * * import org.apache.commons.rng.simple.RandomSource; * import org.apache.commons.rng.sampling.distribution.DiscreteSampler; * import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler; * import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction; * * // Distribution to sample. * final IntegerDistribution dist = new BinomialDistribution(11, 0.56); * // Create the sampler. * final DiscreteSampler binomialSampler = * InverseTransformDiscreteSampler.of(RandomSource.XO_RO_SHI_RO_128_PP.create(), * new DiscreteInverseCumulativeProbabilityFunction() { * public int inverseCumulativeProbability(double p) { * return dist.inverseCumulativeProbability(p); * } * }); * * // Generate random deviate. * int random = binomialSampler.sample(); * </code></pre> * * @since 1.0 */ public class InverseTransformDiscreteSampler extends SamplerBase implements SharedStateDiscreteSampler { /** Inverse cumulative probability function. */ private final DiscreteInverseCumulativeProbabilityFunction function; /** Underlying source of randomness. */ private final UniformRandomProvider rng; /** * @param rng Generator of uniformly distributed random numbers. * @param function Inverse cumulative probability function. */ public InverseTransformDiscreteSampler(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function) { super(null); this.rng = rng; this.function = function; } /** {@inheritDoc} */ @Override public int sample() { return function.inverseCumulativeProbability(rng.nextDouble()); } /** {@inheritDoc} */ @Override public String toString() { return function.toString() + " (inverse method) [" + rng.toString() + "]"; } /** * {@inheritDoc} * * <p>Note: The new sampler will share the inverse cumulative probability function. This * must be suitable for concurrent use to ensure thread safety.</p> * * @since 1.3 */ @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng) { return new InverseTransformDiscreteSampler(rng, function); } /** * Create a new inverse-transform discrete sampler. * * <p>To use the sampler to * {@link org.apache.commons.rng.sampling.SharedStateSampler share state} the function must be * suitable for concurrent use.</p> * * @param rng Generator of uniformly distributed random numbers. * @param function Inverse cumulative probability function. * @return the sampler * @see #withUniformRandomProvider(UniformRandomProvider) * @since 1.3 */ public static SharedStateDiscreteSampler of(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function) { return new InverseTransformDiscreteSampler(rng, function); } }
3,023
0
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling
Create_ds/commons-rng/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
/* * 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.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.SharedStateSampler; /** * Functions used by some of the samplers. * This class is not part of the public API, as it would be * better to group these utilities in a dedicated component. */ final class InternalUtils { // Class is package-private on purpose; do not make it public. /** All long-representable factorials. */ private static final long[] FACTORIALS = { 1L, 1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L, 6227020800L, 87178291200L, 1307674368000L, 20922789888000L, 355687428096000L, 6402373705728000L, 121645100408832000L, 2432902008176640000L }; /** The first array index with a non-zero log factorial. */ private static final int BEGIN_LOG_FACTORIALS = 2; /** * The multiplier to convert the least significant 53-bits of a {@code long} to a {@code double}. * Taken from org.apache.commons.rng.core.util.NumberFactory. */ private static final double DOUBLE_MULTIPLIER = 0x1.0p-53d; /** Utility class. */ private InternalUtils() {} /** * @param n Argument. * @return {@code n!} * @throws IndexOutOfBoundsException if the result is too large to be represented * by a {@code long} (i.e. if {@code n > 20}), or {@code n} is negative. */ static long factorial(int n) { return FACTORIALS[n]; } /** * Validate the probabilities sum to a finite positive number. * * @param probabilities the probabilities * @return the sum * @throws IllegalArgumentException if {@code probabilities} is null or empty, a * probability is negative, infinite or {@code NaN}, or the sum of all * probabilities is not strictly positive. */ static double validateProbabilities(double[] probabilities) { if (probabilities == null || probabilities.length == 0) { throw new IllegalArgumentException("Probabilities must not be empty."); } double sumProb = 0; for (final double prob : probabilities) { validateProbability(prob); sumProb += prob; } if (Double.isInfinite(sumProb) || sumProb <= 0) { throw new IllegalArgumentException("Invalid sum of probabilities: " + sumProb); } return sumProb; } /** * Validate the probability is a finite positive number. * * @param probability Probability. * @throws IllegalArgumentException if {@code probability} is negative, infinite or {@code NaN}. */ static void validateProbability(double probability) { if (probability < 0 || Double.isInfinite(probability) || Double.isNaN(probability)) { throw new IllegalArgumentException("Invalid probability: " + probability); } } /** * Create a new instance of the given sampler using * {@link SharedStateSampler#withUniformRandomProvider(UniformRandomProvider)}. * * @param sampler Source sampler. * @param rng Generator of uniformly distributed random numbers. * @return the new sampler * @throws UnsupportedOperationException if the underlying sampler is not a * {@link SharedStateSampler} or does not return a {@link NormalizedGaussianSampler} when * sharing state. */ static NormalizedGaussianSampler newNormalizedGaussianSampler( NormalizedGaussianSampler sampler, UniformRandomProvider rng) { if (!(sampler instanceof SharedStateSampler<?>)) { throw new UnsupportedOperationException("The underlying sampler cannot share state"); } final Object newSampler = ((SharedStateSampler<?>) sampler).withUniformRandomProvider(rng); if (!(newSampler instanceof NormalizedGaussianSampler)) { throw new UnsupportedOperationException( "The underlying sampler did not create a normalized Gaussian sampler"); } return (NormalizedGaussianSampler) newSampler; } /** * Creates a {@code double} in the interval {@code [0, 1)} from a {@code long} value. * * @param v Number. * @return a {@code double} value in the interval {@code [0, 1)}. */ static double makeDouble(long v) { // This matches the method in o.a.c.rng.core.util.NumberFactory.makeDouble(long) // without adding an explicit dependency on that module. return (v >>> 11) * DOUBLE_MULTIPLIER; } /** * Creates a {@code double} in the interval {@code (0, 1]} from a {@code long} value. * * @param v Number. * @return a {@code double} value in the interval {@code (0, 1]}. */ static double makeNonZeroDouble(long v) { // This matches the method in o.a.c.rng.core.util.NumberFactory.makeDouble(long) // but shifts the range from [0, 1) to (0, 1]. return ((v >>> 11) + 1L) * DOUBLE_MULTIPLIER; } /** * Class for computing the natural logarithm of the factorial of {@code n}. * It allows to allocate a cache of precomputed values. * In case of cache miss, computation is performed by a call to * {@link InternalGamma#logGamma(double)}. */ public static final class FactorialLog { /** * Precomputed values of the function: * {@code LOG_FACTORIALS[i] = log(i!)}. */ private final double[] logFactorials; /** * Creates an instance, reusing the already computed values if available. * * @param numValues Number of values of the function to compute. * @param cache Existing cache. * @throws NegativeArraySizeException if {@code numValues < 0}. */ private FactorialLog(int numValues, double[] cache) { logFactorials = new double[numValues]; int endCopy; if (cache != null && cache.length > BEGIN_LOG_FACTORIALS) { // Copy available values. endCopy = Math.min(cache.length, numValues); System.arraycopy(cache, BEGIN_LOG_FACTORIALS, logFactorials, BEGIN_LOG_FACTORIALS, endCopy - BEGIN_LOG_FACTORIALS); } else { // All values to be computed endCopy = BEGIN_LOG_FACTORIALS; } // Compute remaining values. for (int i = endCopy; i < numValues; i++) { if (i < FACTORIALS.length) { logFactorials[i] = Math.log(FACTORIALS[i]); } else { logFactorials[i] = logFactorials[i - 1] + Math.log(i); } } } /** * Creates an instance with no precomputed values. * * @return an instance with no precomputed values. */ public static FactorialLog create() { return new FactorialLog(0, null); } /** * Creates an instance with the specified cache size. * * @param cacheSize Number of precomputed values of the function. * @return a new instance where {@code cacheSize} values have been * precomputed. * @throws IllegalArgumentException if {@code n < 0}. */ public FactorialLog withCache(final int cacheSize) { return new FactorialLog(cacheSize, logFactorials); } /** * Computes {@code log(n!)}. * * @param n Argument. * @return {@code log(n!)}. * @throws IndexOutOfBoundsException if {@code numValues < 0}. */ public double value(final int n) { // Use cache of precomputed values. if (n < logFactorials.length) { return logFactorials[n]; } // Use cache of precomputed factorial values. if (n < FACTORIALS.length) { return Math.log(FACTORIALS[n]); } // Delegate. return InternalGamma.logGamma(n + 1.0); } } }
3,024
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersCommonParametricTest.java
/* * 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.rng.core; import java.util.Arrays; import java.util.List; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.function.LongSupplier; import java.util.stream.Collectors; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.RandomProviderState; /** * Tests which all generators must pass. */ class ProvidersCommonParametricTest { private static Iterable<RestorableUniformRandomProvider> getList() { return ProvidersList.list(); } // Precondition tests @ParameterizedTest @MethodSource("getList") void testPreconditionNextInt(UniformRandomProvider generator) { Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(-1)); Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextInt(0)); } @ParameterizedTest @MethodSource("getList") void testPreconditionNextLong(UniformRandomProvider generator) { Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(-1)); Assertions.assertThrows(IllegalArgumentException.class, () -> generator.nextLong(0)); } @ParameterizedTest @MethodSource("getList") void testPreconditionNextBytes(UniformRandomProvider generator) { Assertions.assertThrows(NullPointerException.class, () -> generator.nextBytes(null, 0, 0)); final int size = 10; final int num = 1; final byte[] buf = new byte[size]; Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, -1, num)); // Edge-case allowed by JDK range checks (see RNG-170) generator.nextBytes(buf, size, 0); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, size, 1)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, size, -1)); final int offset = 2; Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, size - offset + 1)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, -1)); // offset + length overflows Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(buf, offset, Integer.MAX_VALUE)); // Should be OK generator.nextBytes(buf, 0, size); generator.nextBytes(buf, 0, size - offset); generator.nextBytes(buf, offset, size - offset); // Should be consistent with no length final byte[] empty = {}; generator.nextBytes(empty); generator.nextBytes(empty, 0, 0); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(empty, 0, 1)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(empty, 0, -1)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> generator.nextBytes(empty, -1, 0)); } // Uniformity tests @ParameterizedTest @MethodSource("getList") void testUniformNextBytesFullBuffer(UniformRandomProvider generator) { // Value chosen to exercise all the code lines in the // "nextBytes" methods. final int size = 23; final byte[] buffer = new byte[size]; final Runnable nextMethod = new Runnable() { @Override public void run() { generator.nextBytes(buffer); } }; Assertions.assertTrue(isUniformNextBytes(buffer, 0, size, nextMethod), generator::toString); } @ParameterizedTest @MethodSource("getList") void testUniformNextBytesPartialBuffer(UniformRandomProvider generator) { final int totalSize = 1234; final int offset = 567; final int size = 89; final byte[] buffer = new byte[totalSize]; final Runnable nextMethod = new Runnable() { @Override public void run() { generator.nextBytes(buffer, offset, size); } }; // Test should pass for the part of the buffer where values are put. Assertions.assertTrue(isUniformNextBytes(buffer, offset, offset + size, nextMethod), generator::toString); // The parts of the buffer where no values are put should be zero. for (int i = 0; i < offset; i++) { Assertions.assertEquals(0, buffer[i], generator::toString); } for (int i = offset + size; i < totalSize; i++) { Assertions.assertEquals(0, buffer[i], generator::toString); } } @ParameterizedTest @MethodSource("getList") void testUniformNextIntegerInRange(UniformRandomProvider generator) { // Statistical test uses 10 bins so tests are invalid below this level checkNextIntegerInRange(generator, 10, 1000); checkNextIntegerInRange(generator, 12, 1000); checkNextIntegerInRange(generator, 31, 1000); checkNextIntegerInRange(generator, 32, 1000); checkNextIntegerInRange(generator, 2016128993, 1000); checkNextIntegerInRange(generator, 1834691456, 1000); checkNextIntegerInRange(generator, 869657561, 1000); checkNextIntegerInRange(generator, 1570504788, 1000); } @ParameterizedTest @MethodSource("getList") void testUniformNextLongInRange(UniformRandomProvider generator) { // Statistical test uses 10 bins so tests are invalid below this level checkNextLongInRange(generator, 11, 1000); checkNextLongInRange(generator, 19, 1000); checkNextLongInRange(generator, 31, 1000); checkNextLongInRange(generator, 32, 1000); final long q = Long.MAX_VALUE / 4; checkNextLongInRange(generator, q, 1000); checkNextLongInRange(generator, 2 * q, 1000); checkNextLongInRange(generator, 3 * q, 1000); } @ParameterizedTest @MethodSource("getList") void testUniformNextFloat(UniformRandomProvider generator) { checkNextFloat(generator, 1000); } @ParameterizedTest @MethodSource("getList") void testUniformNextDouble(UniformRandomProvider generator) { checkNextDouble(generator, 1000); } @ParameterizedTest @MethodSource("getList") void testUniformNextIntRandomWalk(UniformRandomProvider generator) { final Callable<Boolean> nextMethod = new Callable<Boolean>() { @Override public Boolean call() throws Exception { return generator.nextInt() >= 0; } }; checkRandomWalk(generator, 1000, nextMethod); } @ParameterizedTest @MethodSource("getList") void testUniformNextLongRandomWalk(UniformRandomProvider generator) { final Callable<Boolean> nextMethod = new Callable<Boolean>() { @Override public Boolean call() throws Exception { return generator.nextLong() >= 0; } }; checkRandomWalk(generator, 1000, nextMethod); } @ParameterizedTest @MethodSource("getList") void testUniformNextBooleanRandomWalk(UniformRandomProvider generator) { final Callable<Boolean> nextMethod = new Callable<Boolean>() { @Override public Boolean call() throws Exception { return generator.nextBoolean(); } }; checkRandomWalk(generator, 1000, nextMethod); } // State save and restore tests. @ParameterizedTest @MethodSource("getList") void testStateSettable(RestorableUniformRandomProvider generator) { // Should be fairly large in order to ensure that all the internal // state is away from its initial settings. final int n = 10000; // Save. final RandomProviderState state = generator.saveState(); // Store some values. final List<Number> listOrig = makeList(n, generator); // Discard a few more. final List<Number> listDiscard = makeList(n, generator); Assertions.assertNotEquals(0, listDiscard.size()); Assertions.assertNotEquals(listOrig, listDiscard); // Reset. generator.restoreState(state); // Replay. final List<Number> listReplay = makeList(n, generator); Assertions.assertNotSame(listOrig, listReplay); // Check that the restored state is the same as the original. Assertions.assertEquals(listOrig, listReplay); } @ParameterizedTest @MethodSource("getList") void testStateWrongSize(RestorableUniformRandomProvider generator) { final RandomProviderState state = new DummyGenerator().saveState(); // Try to restore with an invalid state (wrong size). Assertions.assertThrows(IllegalStateException.class, () -> generator.restoreState(state)); } @ParameterizedTest @MethodSource("getList") void testRestoreForeignState(RestorableUniformRandomProvider generator) { final RandomProviderState state = new RandomProviderState() {}; Assertions.assertThrows(IllegalArgumentException.class, () -> generator.restoreState(state)); } ///// Support methods below. /** * Populates a list with random numbers. * * @param n Loop counter. * @param generator RNG under test. * @return a list containing {@code 11 * n} random numbers. */ private static List<Number> makeList(int n, UniformRandomProvider generator) { final List<Number> list = new ArrayList<>(); for (int i = 0; i < n; i++) { // Append 11 values. list.add(generator.nextInt()); list.add(generator.nextInt(21)); list.add(generator.nextInt(436)); list.add(generator.nextLong()); list.add(generator.nextLong(157894)); list.add(generator.nextLong(5745833)); list.add(generator.nextFloat()); list.add(generator.nextFloat()); list.add(generator.nextDouble()); list.add(generator.nextDouble()); list.add(generator.nextBoolean() ? 1 : 0); } return list; } /** * Checks that the generator values can be placed into 256 bins with * approximately equal number of counts. * Test allows to select only part of the buffer for performing the * statistics. * * @param buffer Buffer to be filled. * @param first First element (included) of {@code buffer} range for * which statistics must be taken into account. * @param last Last element (excluded) of {@code buffer} range for * which statistics must be taken into account. * @param nextMethod Method that fills the given {@code buffer}. * @return {@code true} if the distribution is uniform. */ private static boolean isUniformNextBytes(byte[] buffer, int first, int last, Runnable nextMethod) { final int sampleSize = 10000; // Number of possible values (do not change). final int byteRange = 256; // Chi-square critical value with 255 degrees of freedom // and 1% significance level. final double chi2CriticalValue = 310.45738821990585; // Bins. final long[] observed = new long[byteRange]; final double expected = (double) sampleSize * (last - first) / byteRange; try { for (int k = 0; k < sampleSize; k++) { nextMethod.run(); for (int i = first; i < last; i++) { // Convert byte to an index in [0, 255] ++observed[buffer[i] & 0xff]; } } } catch (final Exception e) { // Should never happen. throw new RuntimeException("Unexpected"); } // Compute chi-square. double chi2 = 0; for (int k = 0; k < byteRange; k++) { final double diff = observed[k] - expected; chi2 += diff * diff / expected; } // Statistics check. return chi2 <= chi2CriticalValue; } /** * Checks that the generator values can be placed into 2 bins with * approximately equal number of counts. * The test uses the expectation from a fixed-step "random walk". * * @param generator Generator. * @param sampleSize Number of random values generated. * @param nextMethod Method that returns {@code true} if the generated * values are to be placed in the first bin, {@code false} if it must * go to the second bin. */ private static void checkRandomWalk(UniformRandomProvider generator, int sampleSize, Callable<Boolean> nextMethod) { int walk = 0; try { for (int k = 0; k < sampleSize; ++k) { if (nextMethod.call()) { ++walk; } else { --walk; } } } catch (final Exception e) { // Should never happen. throw new RuntimeException("Unexpected"); } final double actual = Math.abs(walk); final double max = Math.sqrt(sampleSize) * 2.576; Assertions.assertTrue(actual < max, () -> generator + ": Walked too far astray: " + actual + " > " + max + " (test will fail randomly about 1 in 100 times)"); } /** * Tests uniformity of the distribution produced by {@code nextInt(int)}. * * @param generator Generator. * @param max Upper bound. * @param sampleSize Number of random values generated. */ private static void checkNextIntegerInRange(final UniformRandomProvider generator, final int max, int sampleSize) { final LongSupplier nextMethod = () -> generator.nextInt(max); checkNextInRange(generator, max, sampleSize, nextMethod); } /** * Tests uniformity of the distribution produced by {@code nextLong(long)}. * * @param generator Generator. * @param max Upper bound. * @param sampleSize Number of random values generated. */ private static void checkNextLongInRange(final UniformRandomProvider generator, long max, int sampleSize) { final LongSupplier nextMethod = () -> generator.nextLong(max); checkNextInRange(generator, max, sampleSize, nextMethod); } /** * Tests uniformity of the distribution produced by {@code nextFloat()}. * * @param generator Generator. * @param sampleSize Number of random values generated. */ private static void checkNextFloat(final UniformRandomProvider generator, int sampleSize) { final int max = 1234; final LongSupplier nextMethod = () -> (int) (max * generator.nextFloat()); checkNextInRange(generator, max, sampleSize, nextMethod); } /** * Tests uniformity of the distribution produced by {@code nextDouble()}. * * @param generator Generator. * @param sampleSize Number of random values generated. */ private static void checkNextDouble(final UniformRandomProvider generator, int sampleSize) { final int max = 578; final LongSupplier nextMethod = () -> (int) (max * generator.nextDouble()); checkNextInRange(generator, max, sampleSize, nextMethod); } /** * Tests uniformity of the distribution produced by the given * {@code nextMethod}. * It performs a chi-square test of homogeneity of the observed * distribution with the expected uniform distribution. * Repeat tests are performed at the 1% level and the total number of failed * tests is tested at the 0.5% significance level. * * @param generator Generator. * @param n Upper bound. * @param nextMethod method to call. * @param sampleSize Number of random values generated. */ private static void checkNextInRange(final UniformRandomProvider generator, long n, int sampleSize, LongSupplier nextMethod) { final int numTests = 500; // Do not change (statistical test assumes that dof = 9). final int numBins = 10; // dof = numBins - 1 // Set up bins. final long[] binUpperBounds = new long[numBins]; final double step = n / (double) numBins; for (int k = 0; k < numBins; k++) { binUpperBounds[k] = (long) ((k + 1) * step); } // Rounding error occurs on the long value of 2305843009213693951L binUpperBounds[numBins - 1] = n; // Run the tests. int numFailures = 0; final double[] expected = new double[numBins]; long previousUpperBound = 0; for (int k = 0; k < numBins; k++) { final long range = binUpperBounds[k] - previousUpperBound; expected[k] = sampleSize * (range / (double) n); previousUpperBound = binUpperBounds[k]; } final int[] observed = new int[numBins]; // Chi-square critical value with 9 degrees of freedom // and 1% significance level. final double chi2CriticalValue = 21.665994333461924; // For storing chi2 larger than the critical value. final List<Double> failedStat = new ArrayList<>(); try { final int lastDecileIndex = numBins - 1; for (int i = 0; i < numTests; i++) { Arrays.fill(observed, 0); SAMPLE: for (int j = 0; j < sampleSize; j++) { final long value = nextMethod.getAsLong(); Assertions.assertTrue(value >= 0 && value < n, "Range"); for (int k = 0; k < lastDecileIndex; k++) { if (value < binUpperBounds[k]) { ++observed[k]; continue SAMPLE; } } ++observed[lastDecileIndex]; } // Compute chi-square. double chi2 = 0; for (int k = 0; k < numBins; k++) { final double diff = observed[k] - expected[k]; chi2 += diff * diff / expected[k]; } // Statistics check. if (chi2 > chi2CriticalValue) { failedStat.add(chi2); ++numFailures; } } } catch (final Exception e) { // Should never happen. throw new RuntimeException("Unexpected", e); } // The expected number of failed tests can be modelled as a Binomial distribution // B(n, p) with n=500, p=0.01 (500 tests with a 1% significance level). // The cumulative probability of the number of failed tests (X) is: // x P(X>x) // 10 0.0132 // 11 0.00521 // 12 0.00190 if (numFailures > 11) { // Test will fail with 0.5% probability Assertions.fail(String.format( "%s: Too many failures for n = %d, sample size = %d " + "(%d out of %d tests failed, chi2 > %.3f=%s)", generator, n, sampleSize, numFailures, numTests, chi2CriticalValue, failedStat.stream().map(d -> String.format("%.3f", d)) .collect(Collectors.joining(", ", "[", "]")))); } } /** * @param generator Generator. * @param chunkSize Size of the small buffer. * @param numChunks Number of chunks that make the large buffer. */ static void checkNextBytesChunks(RestorableUniformRandomProvider generator, int chunkSize, int numChunks) { final byte[] b1 = new byte[chunkSize * numChunks]; final byte[] b2 = new byte[chunkSize]; final RandomProviderState state = generator.saveState(); // Generate the chunks in a single call. generator.nextBytes(b1); // Reset to previous state. generator.restoreState(state); // Generate the chunks in consecutive calls. for (int i = 0; i < numChunks; i++) { generator.nextBytes(b2); } // Store last "chunkSize" bytes of b1 into b3. final byte[] b3 = new byte[chunkSize]; System.arraycopy(b1, b1.length - b3.length, b3, 0, b3.length); // Sequence of calls must be the same. Assertions.assertArrayEquals(b2, b3, () -> "chunkSize=" + chunkSize + " numChunks=" + numChunks); } /** * Dummy class for checking that restoring fails when an invalid state is used. */ class DummyGenerator extends org.apache.commons.rng.core.source32.IntProvider { /** {@inheritDoc} */ @Override public int next() { return 4; // https://www.xkcd.com/221/ } /** {@inheritDoc} */ @Override protected byte[] getStateInternal() { return new byte[0]; } /** {@inheritDoc} */ @Override protected void setStateInternal(byte[] s) { // No state. } } }
3,025
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/SplittableProvidersParametricTest.java
/* * 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.rng.core; import java.util.HashSet; import java.util.Set; import java.util.Spliterator; import java.util.SplittableRandom; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Consumer; import java.util.function.UnaryOperator; import java.util.stream.Stream; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.util.RandomStreamsTestHelper; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; /** * Tests which all {@link SplittableUniformRandomProvider} generators must pass. */ class SplittableProvidersParametricTest { /** The expected characteristics for the spliterator from the splittable stream. */ private static final int SPLITERATOR_CHARACTERISTICS = Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE; /** * Dummy class for checking the behavior of the SplittableUniformRandomProvider. * All generation and split methods throw an exception. This can be used to test * exception conditions for arguments to default stream functions. */ private static class DummyGenerator implements SplittableUniformRandomProvider { /** An instance. */ static final DummyGenerator INSTANCE = new DummyGenerator(); @Override public long nextLong() { throw new UnsupportedOperationException("The nextLong method should not be invoked"); } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { throw new UnsupportedOperationException("The split(source) method should not be invoked"); } } /** * Thread-safe class for checking the behavior of the SplittableUniformRandomProvider. * Generation methods default to ThreadLocalRandom. Split methods return the same instance. * This is a functioning generator that can be used as a source to seed splitting. */ private static class ThreadLocalGenerator implements SplittableUniformRandomProvider { /** An instance. */ static final ThreadLocalGenerator INSTANCE = new ThreadLocalGenerator(); @Override public long nextLong() { return ThreadLocalRandom.current().nextLong(); } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { return this; } } /** * Gets the list of splittable generators. * * @return the list */ private static Iterable<SplittableUniformRandomProvider> getSplittableProviders() { return ProvidersList.listSplittable(); } /** * Test that the split methods throw when the source of randomness is null. */ @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitThrowsWithNullSource(SplittableUniformRandomProvider generator) { Assertions.assertThrows(NullPointerException.class, () -> generator.split(null)); } /** * Test that the random generator returned from the split is a new instance of the same class. */ @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitReturnsANewInstance(SplittableUniformRandomProvider generator) { assertSplitReturnsANewInstance(SplittableUniformRandomProvider::split, generator); } /** * Test that the random generator returned from the split(source) is a new instance of the same class. */ @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitWithSourceReturnsANewInstance(SplittableUniformRandomProvider generator) { assertSplitReturnsANewInstance(s -> s.split(ThreadLocalGenerator.INSTANCE), generator); } /** * Assert that the random generator returned from the split function is a new instance of the same class. * * @param splitFunction Split function to test. * @param generator RNG under test. */ private static void assertSplitReturnsANewInstance(UnaryOperator<SplittableUniformRandomProvider> splitFunction, SplittableUniformRandomProvider generator) { final UniformRandomProvider child = splitFunction.apply(generator); Assertions.assertNotSame(generator, child, "The child instance should be a different object"); Assertions.assertEquals(generator.getClass(), child.getClass(), "The child instance should be the same class"); RandomAssert.assertNextLongNotEquals(10, generator, child); } /** * Test that the split method is reproducible when used with the same generator source in the * same state. */ @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitWithSourceIsReproducible(SplittableUniformRandomProvider generator) { final long seed = ThreadLocalRandom.current().nextLong(); final UniformRandomProvider rng1 = generator.split(new SplittableRandom(seed)::nextLong); final UniformRandomProvider rng2 = generator.split(new SplittableRandom(seed)::nextLong); RandomAssert.assertNextLongEquals(10, rng1, rng2); } /** * Test that the other stream splits methods all call the * {@link SplittableUniformRandomProvider#splits(long, SplittableUniformRandomProvider)} method. * This is tested by checking the spliterator is the same. * * <p>This test serves to ensure the default implementations in SplittableUniformRandomProvider * eventually call the same method. The RNG implementation thus only has to override one method. */ @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsMethodsUseSameSpliterator(SplittableUniformRandomProvider generator) { final long size = 10; final Spliterator<SplittableUniformRandomProvider> s = generator.splits(size, generator).spliterator(); Assertions.assertEquals(s.getClass(), generator.splits().spliterator().getClass()); Assertions.assertEquals(s.getClass(), generator.splits(size).spliterator().getClass()); Assertions.assertEquals(s.getClass(), generator.splits(ThreadLocalGenerator.INSTANCE).spliterator().getClass()); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsSize(SplittableUniformRandomProvider generator) { for (final long size : new long[] {0, 1, 7, 13}) { Assertions.assertEquals(size, generator.splits(size).count(), "splits"); Assertions.assertEquals(size, generator.splits(size, ThreadLocalGenerator.INSTANCE).count(), "splits with source"); } } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplits(SplittableUniformRandomProvider generator) { assertSplits(generator, false); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsParallel(SplittableUniformRandomProvider generator) { assertSplits(generator, true); } /** * Test the splits method returns a stream of unique instances. The test uses a * fixed source of randomness such that the only randomness is from the stream * position. * * @param generator Generator * @param parallel true to use a parallel stream */ private static void assertSplits(SplittableUniformRandomProvider generator, boolean parallel) { final long size = 13; for (final long seed : new long[] {0, RandomStreamsTestHelper.createSeed(ThreadLocalGenerator.INSTANCE)}) { final SplittableUniformRandomProvider source = new SplittableUniformRandomProvider() { @Override public long nextLong() { return seed; } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { return this; } }; // Test the assumption that the seed will be passed through (lowest bit is set) Assertions.assertEquals(seed | 1, RandomStreamsTestHelper.createSeed(source)); Stream<SplittableUniformRandomProvider> stream = generator.splits(size, source); Assertions.assertFalse(stream.isParallel(), "Initial stream should be sequential"); if (parallel) { stream = stream.parallel(); Assertions.assertTrue(stream.isParallel(), "Stream should be parallel"); } // Check the instance is a new object of the same type. // These will be hashed using the system identity hash code. final Set<SplittableUniformRandomProvider> observed = ConcurrentHashMap.newKeySet(); observed.add(generator); stream.forEach(r -> { Assertions.assertTrue(observed.add(r), "Instance should be unique"); Assertions.assertEquals(generator.getClass(), r.getClass()); }); // Note: observed contains the original generator so subtract 1 Assertions.assertEquals(size, observed.size() - 1); // Test instances generate different values. // The only randomness is from the stream position. final long[] values = observed.stream().mapToLong(r -> { // Warm up generator with some cycles. // E.g. LXM generators return the first value from the initial state. for (int i = 0; i < 10; i++) { r.nextLong(); } return r.nextLong(); }).distinct().toArray(); // This test is looking for different values. // To avoid the rare case of not all distinct we relax the threshold to // half the generators. This will spot errors where all generators are // the same. Assertions.assertTrue(values.length > size / 2, () -> "splits did not seed randomness from the stream position. Initial seed = " + seed); } } // Test adapted from stream tests in commons-rng-client-api module /** * Helper method to raise an assertion error inside an action passed to a Spliterator * when the action should not be invoked. * * @see Spliterator#tryAdvance(Consumer) * @see Spliterator#forEachRemaining(Consumer) */ private static void failSpliteratorShouldBeEmpty() { Assertions.fail("Spliterator should not have any remaining elements"); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsInvalidStreamSizeThrows(SplittableUniformRandomProvider rng) { Assertions.assertThrows(IllegalArgumentException.class, () -> rng.splits(-1), "splits(size)"); final SplittableUniformRandomProvider source = DummyGenerator.INSTANCE; Assertions.assertThrows(IllegalArgumentException.class, () -> rng.splits(-1, source), "splits(size, source)"); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsUnlimitedStreamSize(SplittableUniformRandomProvider rng) { assertUnlimitedSpliterator(rng.splits().spliterator(), "splits()"); final SplittableUniformRandomProvider source = ThreadLocalGenerator.INSTANCE; assertUnlimitedSpliterator(rng.splits(source).spliterator(), "splits(source)"); } /** * Assert the spliterator has an unlimited expected size and the characteristics for a sized * immutable stream. * * @param spliterator Spliterator. * @param msg Error message. */ private static void assertUnlimitedSpliterator(Spliterator<?> spliterator, String msg) { Assertions.assertEquals(Long.MAX_VALUE, spliterator.estimateSize(), msg); Assertions.assertTrue(spliterator.hasCharacteristics(SPLITERATOR_CHARACTERISTICS), () -> String.format("%s: characteristics = %s, expected %s", msg, Integer.toBinaryString(spliterator.characteristics()), Integer.toBinaryString(SPLITERATOR_CHARACTERISTICS) )); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsNullSourceThrows(SplittableUniformRandomProvider rng) { final SplittableUniformRandomProvider source = null; Assertions.assertThrows(NullPointerException.class, () -> rng.splits(source)); Assertions.assertThrows(NullPointerException.class, () -> rng.splits(1, source)); } @ParameterizedTest @MethodSource("getSplittableProviders") void testSplitsSpliterator(SplittableUniformRandomProvider rng) { // Split a large spliterator into four smaller ones; // each is used to test different functionality final long size = 41; Spliterator<SplittableUniformRandomProvider> s1 = rng.splits(size).spliterator(); Assertions.assertEquals(size, s1.estimateSize()); final Spliterator<SplittableUniformRandomProvider> s2 = s1.trySplit(); final Spliterator<SplittableUniformRandomProvider> s3 = s1.trySplit(); final Spliterator<SplittableUniformRandomProvider> s4 = s2.trySplit(); Assertions.assertEquals(size, s1.estimateSize() + s2.estimateSize() + s3.estimateSize() + s4.estimateSize()); // s1. Test cannot split indefinitely while (s1.estimateSize() > 1) { final long currentSize = s1.estimateSize(); final Spliterator<SplittableUniformRandomProvider> other = s1.trySplit(); Assertions.assertEquals(currentSize, s1.estimateSize() + other.estimateSize()); s1 = other; } Assertions.assertNull(s1.trySplit(), "Cannot split when size <= 1"); // Check the instance is a new object of the same type. // These will be hashed using the system identity hash code. final HashSet<SplittableUniformRandomProvider> observed = new HashSet<>(); observed.add(rng); final Consumer<SplittableUniformRandomProvider> action = r -> { Assertions.assertTrue(observed.add(r), "Instance should be unique"); Assertions.assertEquals(rng.getClass(), r.getClass()); }; // s2. Test advance for (long newSize = s2.estimateSize(); newSize-- > 0;) { Assertions.assertTrue(s2.tryAdvance(action)); Assertions.assertEquals(newSize, s2.estimateSize(), "s2 size estimate"); } Assertions.assertFalse(s2.tryAdvance(r -> failSpliteratorShouldBeEmpty())); s2.forEachRemaining(r -> failSpliteratorShouldBeEmpty()); // s3. Test forEachRemaining s3.forEachRemaining(action); Assertions.assertEquals(0, s3.estimateSize()); s3.forEachRemaining(r -> failSpliteratorShouldBeEmpty()); // s4. Test tryAdvance and forEachRemaining when the action throws an exception final IllegalStateException ex = new IllegalStateException(); final Consumer<SplittableUniformRandomProvider> badAction = r -> { throw ex; }; final long currentSize = s4.estimateSize(); Assertions.assertTrue(currentSize > 1, "Spliterator requires more elements to test advance"); Assertions.assertSame(ex, Assertions.assertThrows(IllegalStateException.class, () -> s4.tryAdvance(badAction))); Assertions.assertEquals(currentSize - 1, s4.estimateSize(), "Spliterator should be advanced even when action throws"); Assertions.assertSame(ex, Assertions.assertThrows(IllegalStateException.class, () -> s4.forEachRemaining(badAction))); Assertions.assertEquals(0, s4.estimateSize(), "Spliterator should be finished even when action throws"); s4.forEachRemaining(r -> failSpliteratorShouldBeEmpty()); } }
3,026
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers64ParametricTest.java
/* * 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.rng.core; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.apache.commons.rng.RestorableUniformRandomProvider; /** * Tests which all 64-bits based generators must pass. */ class Providers64ParametricTest { private static Iterable<RestorableUniformRandomProvider> getList() { return ProvidersList.list64(); } @ParameterizedTest @MethodSource("getList") void testNextBytesChunks(RestorableUniformRandomProvider generator) { final int[] chunkSizes = {8, 16, 24}; final int[] chunks = {1, 2, 3, 4, 5}; for (final int chunkSize : chunkSizes) { for (final int numChunks : chunks) { ProvidersCommonParametricTest.checkNextBytesChunks(generator, chunkSize, numChunks); } } } }
3,027
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomAssert.java
/* * 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.rng.core; import org.junit.jupiter.api.Assertions; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.apache.commons.rng.JumpableUniformRandomProvider; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; /** * Utility class for testing random generators. */ public final class RandomAssert { /** * Class contains only static methods. */ private RandomAssert() {} /** * Assert that the random generator produces the expected output using * {@link UniformRandomProvider#nextInt()}. * * @param expected Expected output. * @param rng Random generator. */ public static void assertEquals(int[] expected, UniformRandomProvider rng) { assertEquals(expected, rng, "Value at position "); } /** * Assert that the random generator produces the expected output using * {@link UniformRandomProvider#nextLong()}. * * @param expected Expected output. * @param rng Random generator. */ public static void assertEquals(long[] expected, UniformRandomProvider rng) { assertEquals(expected, rng, "Value at position "); } /** * Assert that the random generator produces the expected output using * {@link UniformRandomProvider#nextInt()}. * The message prefix is prepended to the array index for the assertion message. * * @param expected Expected output. * @param rng Random generator. * @param messagePrefix Message prefix. */ private static void assertEquals(int[] expected, UniformRandomProvider rng, String messagePrefix) { for (int i = 0; i < expected.length; i++) { final int index = i; Assertions.assertEquals(expected[i], rng.nextInt(), () -> messagePrefix + index); } } /** * Assert that the random generator produces the expected output using * {@link UniformRandomProvider#nextLong()}. * The message prefix is prepended to the array index for the assertion message. * * @param expected Expected output. * @param rng Random generator. * @param messagePrefix Message prefix. */ private static void assertEquals(long[] expected, UniformRandomProvider rng, String messagePrefix) { for (int i = 0; i < expected.length; i++) { final int index = i; Assertions.assertEquals(expected[i], rng.nextLong(), () -> messagePrefix + index); } } /** * Assert that the random generator produces a <strong>different</strong> output using * {@link UniformRandomProvider#nextLong()} to the expected output. * * @param expected Expected output. * @param rng Random generator. */ public static void assertNotEquals(long[] expected, UniformRandomProvider rng) { for (int i = 0; i < expected.length; i++) { if (expected[i] != rng.nextLong()) { return; } } Assertions.fail("Expected a different nextLong output"); } /** * Assert that the random generator satisfies the contract of the * {@link JumpableUniformRandomProvider#jump()} function. * * <ul> * <li>The jump returns a copy instance. This is asserted to be a different object * of the same class type as the input. * <li>The copy instance outputs the expected sequence for the current state of the input generator. * This is asserted using the {@code expectedBefore} sequence. * <li>The input instance outputs the expected sequence for an advanced state. * This is asserted using the {@code expectedAfter} sequence. * <ul> * * @param expectedBefore Expected output before the jump. * @param expectedAfter Expected output after the jump. * @param rng Random generator. */ public static void assertJumpEquals(int[] expectedBefore, int[] expectedAfter, JumpableUniformRandomProvider rng) { final UniformRandomProvider copy = rng.jump(); Assertions.assertNotSame(rng, copy, "The copy instance should be a different object"); Assertions.assertEquals(rng.getClass(), copy.getClass(), "The copy instance should be the same class"); assertEquals(expectedBefore, copy, "Pre-jump value at position "); assertEquals(expectedAfter, rng, "Post-jump value at position "); } /** * Assert that the random generator satisfies the contract of the * {@link JumpableUniformRandomProvider#jump()} function. * * <ul> * <li>The jump returns a copy instance. This is asserted to be a different object * of the same class type as the input. * <li>The copy instance outputs the expected sequence for the current state of the input generator. * This is asserted using the {@code expectedBefore} sequence. * <li>The input instance outputs the expected sequence for an advanced state. * This is asserted using the {@code expectedAfter} sequence. * <ul> * * @param expectedBefore Expected output before the jump. * @param expectedAfter Expected output after the jump. * @param rng Random generator. */ public static void assertJumpEquals(long[] expectedBefore, long[] expectedAfter, JumpableUniformRandomProvider rng) { final UniformRandomProvider copy = rng.jump(); Assertions.assertNotSame(rng, copy, "The copy instance should be a different object"); Assertions.assertEquals(rng.getClass(), copy.getClass(), "The copy instance should be the same class"); assertEquals(expectedBefore, copy, "Pre-jump value at position "); assertEquals(expectedAfter, rng, "Post-jump value at position "); } /** * Assert that the random generator satisfies the contract of the * {@link LongJumpableUniformRandomProvider#longJump()} function. * * <ul> * <li>The long jump returns a copy instance. This is asserted to be a different object * of the same class type as the input. * <li>The copy instance outputs the expected sequence for the current state of the input generator. * This is asserted using the {@code expectedBefore} sequence. * <li>The input instance outputs the expected sequence for an advanced state. * This is asserted using the {@code expectedAfter} sequence. * <ul> * * @param expectedBefore Expected output before the long jump. * @param expectedAfter Expected output after the long jump. * @param rng Random generator. */ public static void assertLongJumpEquals(int[] expectedBefore, int[] expectedAfter, LongJumpableUniformRandomProvider rng) { final UniformRandomProvider copy = rng.longJump(); Assertions.assertNotSame(rng, copy, "The copy instance should be a different object"); Assertions.assertEquals(rng.getClass(), copy.getClass(), "The copy instance should be the same class"); assertEquals(expectedBefore, copy, "Pre-jump value at position "); assertEquals(expectedAfter, rng, "Post-jump value at position "); } /** * Assert that the random generator satisfies the contract of the * {@link LongJumpableUniformRandomProvider#longJump()} function. * * <ul> * <li>The long jump returns a copy instance. This is asserted to be a different object * of the same class type as the input. * <li>The copy instance outputs the expected sequence for the current state of the input generator. * This is asserted using the {@code expectedBefore} sequence. * <li>The input instance outputs the expected sequence for an advanced state. * This is asserted using the {@code expectedAfter} sequence. * <ul> * * @param expectedBefore Expected output before the long jump. * @param expectedAfter Expected output after the long jump. * @param rng Random generator. */ public static void assertLongJumpEquals(long[] expectedBefore, long[] expectedAfter, LongJumpableUniformRandomProvider rng) { final UniformRandomProvider copy = rng.longJump(); Assertions.assertNotSame(rng, copy, "The copy instance should be a different object"); Assertions.assertEquals(rng.getClass(), copy.getClass(), "The copy instance should be the same class"); assertEquals(expectedBefore, copy, "Pre-jump value at position "); assertEquals(expectedAfter, rng, "Post-jump value at position "); } /** * Assert that the two random generators produce the same output for * {@link UniformRandomProvider#nextInt()} over the given number of cycles. * * @param cycles Number of cycles. * @param rng1 Random generator 1. * @param rng2 Random generator 2. */ public static void assertNextIntEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { for (int i = 0; i < cycles; i++) { final int index = i; Assertions.assertEquals(rng1.nextInt(), rng2.nextInt(), () -> "Value at position " + index); } } /** * Assert that the two random generators produce the same output for * {@link UniformRandomProvider#nextLong()} over the given number of cycles. * * @param cycles Number of cycles. * @param rng1 Random generator 1. * @param rng2 Random generator 2. */ public static void assertNextLongEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { for (int i = 0; i < cycles; i++) { final int index = i; Assertions.assertEquals(rng1.nextLong(), rng2.nextLong(), () -> "Value at position " + index); } } /** * Assert that the two random generators produce a different output for * {@link UniformRandomProvider#nextInt()} over the given number of cycles. * * @param cycles Number of cycles. * @param rng1 Random generator 1. * @param rng2 Random generator 2. */ public static void assertNextIntNotEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { for (int i = 0; i < cycles; i++) { if (rng1.nextInt() != rng2.nextInt()) { return; } } Assertions.fail(() -> cycles + " cycles of nextb has same output"); } /** * Assert that the two random generators produce a different output for * {@link UniformRandomProvider#nextLong()} over the given number of cycles. * * @param cycles Number of cycles. * @param rng1 Random generator 1. * @param rng2 Random generator 2. */ public static void assertNextLongNotEquals(int cycles, UniformRandomProvider rng1, UniformRandomProvider rng2) { for (int i = 0; i < cycles; i++) { if (rng1.nextLong() != rng2.nextLong()) { return; } } Assertions.fail(() -> cycles + " cycles of nextLong has same output"); } /** * Assert that the random generator produces zero output for * {@link UniformRandomProvider#nextInt()} over the given number of cycles. * This is used to test a poorly seeded generator cannot generate random output. * * @param rng Random generator. * @param cycles Number of cycles. */ public static void assertNextIntZeroOutput(UniformRandomProvider rng, int cycles) { for (int i = 0; i < cycles; i++) { Assertions.assertEquals(0, rng.nextInt(), "Expected the generator to output zeros"); } } /** * Assert that the random generator produces zero output for * {@link UniformRandomProvider#nextLong()} over the given number of cycles. * This is used to test a poorly seeded generator cannot generate random output. * * @param rng Random generator. * @param cycles Number of cycles. */ public static void assertNextLongZeroOutput(UniformRandomProvider rng, int cycles) { for (int i = 0; i < cycles; i++) { Assertions.assertEquals(0, rng.nextLong(), "Expected the generator to output zeros"); } } /** * Assert that following a set number of warm-up cycles the random generator produces * at least one non-zero output for {@link UniformRandomProvider#nextInt()} over the * given number of test cycles. This is used to test a poorly seeded generator can recover * internal state to generate "random" output. * * @param rng Random generator. * @param warmupCycles Number of warm-up cycles. * @param testCycles Number of test cycles. */ public static void assertNextIntNonZeroOutput(UniformRandomProvider rng, int warmupCycles, int testCycles) { for (int i = 0; i < warmupCycles; i++) { rng.nextInt(); } for (int i = 0; i < testCycles; i++) { if (rng.nextInt() != 0) { return; } } Assertions.fail("No non-zero output after " + (warmupCycles + testCycles) + " cycles"); } /** * Assert that following a set number of warm-up cycles the random generator produces * at least one non-zero output for {@link UniformRandomProvider#nextLong()} over the * given number of test cycles. This is used to test a poorly seeded generator can recover * internal state to generate "random" output. * * @param rng Random generator. * @param warmupCycles Number of warm-up cycles. * @param testCycles Number of test cycles. */ public static void assertNextLongNonZeroOutput(UniformRandomProvider rng, int warmupCycles, int testCycles) { for (int i = 0; i < warmupCycles; i++) { rng.nextLong(); } for (int i = 0; i < testCycles; i++) { if (rng.nextLong() != 0L) { return; } } Assertions.fail("No non-zero output after " + (warmupCycles + testCycles) + " cycles"); } /** * Assert that following a set number of warm-up cycles the random generator produces * at least one non-zero output for {@link UniformRandomProvider#nextLong()} over the * given number of test cycles. * * <p>Helper function to add the seed element and bit that was non zero to the fail message. * * @param rng Random generator. * @param warmupCycles Number of warm-up cycles. * @param testCycles Number of test cycles. * @param seedElement Seed element for the message. * @param bit Seed bit for the message. */ private static void assertNextLongNonZeroOutputFromSingleBitSeed( UniformRandomProvider rng, int warmupCycles, int testCycles, int seedElement, int bit) { try { assertNextLongNonZeroOutput(rng, warmupCycles, testCycles); } catch (final AssertionError ex) { Assertions.fail("No non-zero output after " + (warmupCycles + testCycles) + " cycles. " + "Seed element [" + seedElement + "], bit=" + bit); } } /** * Assert that the random generator created using an {@code int[]} seed with a * single bit set is functional. This is tested using the * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using * two times the seed size as the warm-up and test cycles. * * @param type Class of the generator. * @param size Seed size. */ public static <T extends UniformRandomProvider> void assertIntArrayConstructorWithSingleBitSeedIsFunctional(Class<T> type, int size) { assertIntArrayConstructorWithSingleBitInPoolIsFunctional(type, 32 * size); } /** * Assert that the random generator created using an {@code int[]} seed with a * single bit set is functional. This is tested using the * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using * two times the seed size as the warm-up and test cycles. * * <p>The seed size is determined from the size of the bit pool. Bits are set for every position * in the pool from most significant first. If the pool size is not a multiple of 32 then the * remaining lower significant bits of the last seed position are not tested.</p> * * @param type Class of the generator. * @param k Number of bits in the pool (not necessarily a multiple of 32). */ public static <T extends UniformRandomProvider> void assertIntArrayConstructorWithSingleBitInPoolIsFunctional(Class<T> type, int k) { try { // Find the int[] constructor final Constructor<T> constructor = type.getConstructor(int[].class); final int size = (k + 31) / 32; final int[] seed = new int[size]; int remaining = k; for (int i = 0; i < seed.length; i++) { seed[i] = Integer.MIN_VALUE; for (int j = 0; j < 32; j++) { final UniformRandomProvider rng = constructor.newInstance(seed); RandomAssert.assertNextLongNonZeroOutputFromSingleBitSeed(rng, 2 * size, 2 * size, i, 31 - j); // Eventually reset to zero seed[i] >>>= 1; // Terminate when the entire bit pool has been tested if (--remaining == 0) { return; } } Assertions.assertEquals(0, seed[i], "Seed element was not reset"); } } catch (IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException ex) { Assertions.fail(ex.getMessage()); } } /** * Assert that the random generator created using a {@code long[]} seed with a * single bit set is functional. This is tested using the * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using two times the seed * size as the warm-up and test cycles. * * @param type Class of the generator. * @param size Seed size. */ public static <T extends UniformRandomProvider> void assertLongArrayConstructorWithSingleBitSeedIsFunctional(Class<T> type, int size) { assertLongArrayConstructorWithSingleBitInPoolIsFunctional(type, 64 * size); } /** * Assert that the random generator created using an {@code long[]} seed with a * single bit set is functional. This is tested using the * {@link #assertNextLongNonZeroOutput(UniformRandomProvider, int, int)} using * two times the seed size as the warm-up and test cycles. * * <p>The seed size is determined from the size of the bit pool. Bits are set for every position * in the pool from most significant first. If the pool size is not a multiple of 64 then the * remaining lower significant bits of the last seed position are not tested.</p> * * @param type Class of the generator. * @param k Number of bits in the pool (not necessarily a multiple of 64). */ public static <T extends UniformRandomProvider> void assertLongArrayConstructorWithSingleBitInPoolIsFunctional(Class<T> type, int k) { try { // Find the long[] constructor final Constructor<T> constructor = type.getConstructor(long[].class); final int size = (k + 63) / 64; final long[] seed = new long[size]; int remaining = k; for (int i = 0; i < seed.length; i++) { seed[i] = Long.MIN_VALUE; for (int j = 0; j < 64; j++) { final UniformRandomProvider rng = constructor.newInstance(seed); RandomAssert.assertNextLongNonZeroOutputFromSingleBitSeed(rng, 2 * size, 2 * size, i, 63 - j); // Eventually reset to zero seed[i] >>>= 1; // Terminate when the entire bit pool has been tested if (--remaining == 0) { return; } } Assertions.assertEquals(0L, seed[i], "Seed element was not reset"); } } catch (IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException ex) { Assertions.fail(ex.getMessage()); } } }
3,028
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/BaseProviderTest.java
/* * 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.rng.core; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import java.util.Arrays; import java.util.SplittableRandom; import org.apache.commons.rng.core.source64.SplitMix64; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assumptions; /** * Tests for {@link BaseProvider}. * * This class should only contain unit tests that cover code paths not * exercised elsewhere. Those code paths are most probably only taken * in case of a wrong implementation (and would therefore fail other * tests too). */ class BaseProviderTest { @Test void testStateSizeTooSmall() { final DummyGenerator dummy = new DummyGenerator(); final int size = dummy.getStateSize(); Assumptions.assumeTrue(size > 0); final RandomProviderDefaultState state = new RandomProviderDefaultState(new byte[size - 1]); Assertions.assertThrows(IllegalStateException.class, () -> dummy.restoreState(state)); } @Test void testStateSizeTooLarge() { final DummyGenerator dummy = new DummyGenerator(); final int size = dummy.getStateSize(); final RandomProviderDefaultState state = new RandomProviderDefaultState(new byte[size + 1]); Assertions.assertThrows(IllegalStateException.class, () -> dummy.restoreState(state)); } @Test void testFillStateInt() { final int[] state = new int[10]; final int[] seed = {1, 2, 3}; for (int i = 0; i < state.length; i++) { Assertions.assertEquals(0, state[i]); } new DummyGenerator().fillState(state, seed); for (int i = 0; i < seed.length; i++) { Assertions.assertEquals(seed[i], state[i]); } for (int i = seed.length; i < state.length; i++) { Assertions.assertNotEquals(0, state[i]); } } @Test void testFillStateLong() { final long[] state = new long[10]; final long[] seed = {1, 2, 3}; for (int i = 0; i < state.length; i++) { Assertions.assertEquals(0, state[i]); } new DummyGenerator().fillState(state, seed); for (int i = 0; i < seed.length; i++) { Assertions.assertEquals(seed[i], state[i]); } for (int i = seed.length; i < state.length; i++) { Assertions.assertNotEquals(0, state[i]); } } /** * Test the checkIndex method. This is not used by any RNG implementations * as it has been superseded by the equivalent of JDK 9 Objects.checkFromIndexSize. */ @Test void testCheckIndex() { final BaseProvider rng = new BaseProvider() { @Override public void nextBytes(byte[] bytes) { /* noop */ } @Override public void nextBytes(byte[] bytes, int start, int len) { /* noop */ } @Override public int nextInt() { return 0; } @Override public long nextLong() { return 0; } @Override public boolean nextBoolean() { return false; } @Override public float nextFloat() { return 0; } @Override public double nextDouble() { return 0; } }; // Arguments are (min, max, index) // index must be in [min, max] rng.checkIndex(-10, 5, 0); rng.checkIndex(-10, 5, -10); rng.checkIndex(-10, 5, 5); rng.checkIndex(Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE); rng.checkIndex(Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.checkIndex(-10, 5, -11)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.checkIndex(-10, 5, 6)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.checkIndex(-10, 5, Integer.MIN_VALUE)); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.checkIndex(-10, 5, Integer.MAX_VALUE)); } /** * Test a seed can be extended to a required size by filling with a SplitMix64 generator. */ @ParameterizedTest @ValueSource(ints = {0, 1, 2, 4, 5, 6, 7, 8, 9}) void testExpandSeedLong(int length) { // The seed does not matter. // Create random seeds that are smaller or larger than length. final SplittableRandom rng = new SplittableRandom(); for (final long[] seed : new long[][] { {}, rng.longs(1).toArray(), rng.longs(2).toArray(), rng.longs(3).toArray(), rng.longs(4).toArray(), rng.longs(5).toArray(), rng.longs(6).toArray(), rng.longs(7).toArray(), rng.longs(8).toArray(), rng.longs(9).toArray(), }) { Assertions.assertArrayEquals(expandSeed(length, seed), BaseProvider.extendSeed(seed, length)); } } /** * Expand the seed to the minimum specified length using a {@link SplitMix64} generator * seeded with {@code seed[0]}, or zero if the seed length is zero. * * @param length the length * @param seed the seed * @return the seed */ private static long[] expandSeed(int length, long... seed) { if (seed.length < length) { final long[] s = Arrays.copyOf(seed, length); final SplitMix64 rng = new SplitMix64(s[0]); for (int i = seed.length; i < length; i++) { s[i] = rng.nextLong(); } return s; } return seed; } /** * Test a seed can be extended to a required size by filling with a SplitMix64-style * generator using MurmurHash3's 32-bit mix function. * * <p>There is no reference RNG for this output. The test uses fixed output computed * from the reference c++ function in smhasher. */ @ParameterizedTest @ValueSource(ints = {0, 1, 2, 4, 5, 6, 7, 8, 9}) void testExpandSeedInt(int length) { // Reference output from the c++ function fmix32(uint32_t) in smhasher. // https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp final int seedA = 0x012de1ba; final int[] valuesA = { 0x2f66c8b6, 0x256c0269, 0x054ef409, 0x402425ba, 0x78ebf590, 0x76bea1db, 0x8bf5dcbe, 0x104ecdd4, 0x43cfc87e, 0xa33c7643, 0x4d210f56, 0xfa12093d, }; // Values from a seed of zero final int[] values0 = { 0x92ca2f0e, 0x3cd6e3f3, 0x1b147dcc, 0x4c081dbf, 0x487981ab, 0xdb408c9d, 0x78bc1b8f, 0xd83072e5, 0x65cbdd54, 0x1f4b8cef, 0x91783bb0, 0x0231739b, }; // Create a random seed that is larger than the maximum length; // start with the initial value final int[] data = new SplittableRandom().ints(10).toArray(); data[0] = seedA; for (int i = 0; i <= 9; i++) { final int seedLength = i; // Truncate the random seed final int[] seed = Arrays.copyOf(data, seedLength); // Create the expected output length. // If too short it should be extended with values from the reference output final int[] expected = Arrays.copyOf(seed, Math.max(seedLength, length)); if (expected.length == 0) { // Edge case for zero length Assertions.assertArrayEquals(new int[0], BaseProvider.extendSeed(seed, length)); continue; } // Extend the truncated seed using the reference output. // This may be seeded with zero or the non-zero initial value. final int[] source = expected[0] == 0 ? values0 : valuesA; System.arraycopy(source, 0, expected, seedLength, expected.length - seedLength); Assertions.assertArrayEquals(expected, BaseProvider.extendSeed(seed, length), () -> String.format("%d -> %d", seedLength, length)); } } /** * Dummy class for checking the behaviorof the IntProvider. Tests: * <ul> * <li>an incomplete implementation</li> * <li>{@code fillState} methods with "protected" access</li> * </ul> */ static class DummyGenerator extends org.apache.commons.rng.core.source32.IntProvider { /** {@inheritDoc} */ @Override public int next() { return 4; // https://www.xkcd.com/221/ } /** * Gets the state size. This captures the state size of the IntProvider. * * @return the state size */ int getStateSize() { return getStateInternal().length; } // Missing overrides of "setStateInternal" and "getStateInternal". /** {@inheritDoc} */ @Override public void fillState(int[] state, int[] seed) { super.fillState(state, seed); } /** {@inheritDoc} */ @Override public void fillState(long[] state, long[] seed) { super.fillState(state, seed); } } }
3,029
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/ProvidersList.java
/* * 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.rng.core; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.security.SecureRandom; import org.apache.commons.rng.core.source32.JDKRandom; import org.apache.commons.rng.core.source32.JenkinsSmallFast32; import org.apache.commons.rng.core.source32.Well512a; import org.apache.commons.rng.core.source32.XoRoShiRo64Star; import org.apache.commons.rng.core.source32.XoRoShiRo64StarStar; import org.apache.commons.rng.core.source32.XoShiRo128Plus; import org.apache.commons.rng.core.source32.XoShiRo128PlusPlus; import org.apache.commons.rng.core.source32.XoShiRo128StarStar; import org.apache.commons.rng.core.source32.Well1024a; import org.apache.commons.rng.core.source32.Well19937a; import org.apache.commons.rng.core.source32.Well19937c; import org.apache.commons.rng.core.source32.Well44497a; import org.apache.commons.rng.core.source32.Well44497b; import org.apache.commons.rng.core.source32.ISAACRandom; import org.apache.commons.rng.core.source32.MersenneTwister; import org.apache.commons.rng.core.source32.MiddleSquareWeylSequence; import org.apache.commons.rng.core.source32.MultiplyWithCarry256; import org.apache.commons.rng.core.source32.KISSRandom; import org.apache.commons.rng.core.source32.L32X64Mix; import org.apache.commons.rng.core.source32.PcgXshRr32; import org.apache.commons.rng.core.source32.PcgXshRs32; import org.apache.commons.rng.core.source32.DotyHumphreySmallFastCounting32; import org.apache.commons.rng.core.source32.PcgMcgXshRr32; import org.apache.commons.rng.core.source32.PcgMcgXshRs32; import org.apache.commons.rng.core.source64.SplitMix64; import org.apache.commons.rng.core.source64.XorShift1024Star; import org.apache.commons.rng.core.source64.XorShift1024StarPhi; import org.apache.commons.rng.core.source64.TwoCmres; import org.apache.commons.rng.core.source64.XoRoShiRo1024PlusPlus; import org.apache.commons.rng.core.source64.XoRoShiRo1024Star; import org.apache.commons.rng.core.source64.XoRoShiRo1024StarStar; import org.apache.commons.rng.core.source64.XoRoShiRo128Plus; import org.apache.commons.rng.core.source64.XoRoShiRo128PlusPlus; import org.apache.commons.rng.core.source64.XoRoShiRo128StarStar; import org.apache.commons.rng.core.source64.XoShiRo256Plus; import org.apache.commons.rng.core.source64.XoShiRo256PlusPlus; import org.apache.commons.rng.core.source64.XoShiRo256StarStar; import org.apache.commons.rng.core.source64.XoShiRo512Plus; import org.apache.commons.rng.core.source64.XoShiRo512PlusPlus; import org.apache.commons.rng.core.source64.XoShiRo512StarStar; import org.apache.commons.rng.core.source64.JenkinsSmallFast64; import org.apache.commons.rng.core.source64.L128X1024Mix; import org.apache.commons.rng.core.source64.L128X128Mix; import org.apache.commons.rng.core.source64.L128X256Mix; import org.apache.commons.rng.core.source64.L64X1024Mix; import org.apache.commons.rng.core.source64.L64X128Mix; import org.apache.commons.rng.core.source64.L64X128StarStar; import org.apache.commons.rng.core.source64.L64X256Mix; import org.apache.commons.rng.core.source64.MersenneTwister64; import org.apache.commons.rng.core.source64.PcgRxsMXs64; import org.apache.commons.rng.core.source64.DotyHumphreySmallFastCounting64; import org.apache.commons.rng.JumpableUniformRandomProvider; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; /** * The purpose of this class is to provide the list of all generators * implemented in the library. * The list must be updated with each new RNG implementation. * * @see #list() * @see #list32() * @see #list64() */ public final class ProvidersList { /** List of all RNGs implemented in the library. */ private static final List<RestorableUniformRandomProvider> LIST = new ArrayList<>(); /** List of 32-bits based RNGs. */ private static final List<RestorableUniformRandomProvider> LIST32 = new ArrayList<>(); /** List of 64-bits based RNGs. */ private static final List<RestorableUniformRandomProvider> LIST64 = new ArrayList<>(); /** List of {@link JumpableUniformRandomProvider} RNGs. */ private static final List<JumpableUniformRandomProvider> LIST_JUMP = new ArrayList<>(); /** List of {@link SplittableUniformRandomProvider} RNGs. */ private static final List<SplittableUniformRandomProvider> LIST_SPLIT = new ArrayList<>(); static { // External generator for creating a random seed. final SecureRandom g = new SecureRandom(); try { // "int"-based RNGs. LIST32.add(new JDKRandom(g.nextLong())); LIST32.add(new MersenneTwister(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well512a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well1024a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well19937a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well19937c(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well44497a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new Well44497b(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new ISAACRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new MultiplyWithCarry256(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new KISSRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new XoRoShiRo64Star(new int[] {g.nextInt(), g.nextInt()})); LIST32.add(new XoRoShiRo64StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new XoShiRo128Plus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new XoShiRo128StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new PcgXshRr32(new long[] {g.nextLong()})); LIST32.add(new PcgXshRr32(g.nextLong())); LIST32.add(new PcgXshRs32(new long[] {g.nextLong()})); LIST32.add(new PcgXshRs32(g.nextLong())); LIST32.add(new PcgMcgXshRr32(g.nextLong())); LIST32.add(new PcgMcgXshRs32(g.nextLong())); // Ensure a high complexity increment is used for the Weyl sequence LIST32.add(new MiddleSquareWeylSequence(new long[] {g.nextLong(), g.nextLong(), 0xb5ad4eceda1ce2a9L})); LIST32.add(new DotyHumphreySmallFastCounting32(new int[] {g.nextInt(), g.nextInt()})); LIST32.add(new JenkinsSmallFast32(g.nextInt())); LIST32.add(new XoShiRo128PlusPlus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()})); LIST32.add(new L32X64Mix(new int[] {g.nextInt(), g.nextInt()})); // ... add more here. // "long"-based RNGs. LIST64.add(new SplitMix64(g.nextLong())); LIST64.add(new XorShift1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XorShift1024StarPhi(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new TwoCmres(g.nextInt())); LIST64.add(new TwoCmres(g.nextInt(), 5, 8)); LIST64.add(new MersenneTwister64(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoRoShiRo128Plus(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new XoRoShiRo128StarStar(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo256Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo256StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo512Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo512StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new PcgRxsMXs64(new long[] {g.nextLong()})); LIST64.add(new PcgRxsMXs64(g.nextLong())); LIST64.add(new DotyHumphreySmallFastCounting64(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new JenkinsSmallFast64(g.nextLong())); LIST64.add(new XoRoShiRo128PlusPlus(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo256PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoShiRo512PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoRoShiRo1024PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoRoShiRo1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new XoRoShiRo1024StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new L64X128StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); // LXM family must have non-zero XBG state; use a shortened array to default fill LIST64.add(new L64X128Mix(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new L64X256Mix(new long[] {g.nextLong(), g.nextLong()})); LIST64.add(new L64X1024Mix(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new L128X128Mix(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new L128X256Mix(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); LIST64.add(new L128X1024Mix(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()})); // ... add more here. // Do not modify the remaining statements. // Complete list. LIST.addAll(LIST32); LIST.addAll(LIST64); // Dynamically identify the sub-type RNGs LIST.stream() .filter(rng -> rng instanceof JumpableUniformRandomProvider) .forEach(rng -> LIST_JUMP.add((JumpableUniformRandomProvider) rng)); LIST.stream() .filter(rng -> rng instanceof SplittableUniformRandomProvider) .forEach(rng -> LIST_SPLIT.add((SplittableUniformRandomProvider) rng)); } catch (final Exception e) { // CHECKSTYLE: stop Regexp System.err.println("Unexpected exception while creating the list of generators: " + e); e.printStackTrace(System.err); // CHECKSTYLE: resume Regexp throw new RuntimeException(e); } } /** * Class contains only static methods. */ private ProvidersList() {} /** * Subclasses that are "parametric" tests can forward the call to * the "@Parameters"-annotated method to this method. * * @return the list of all generators. */ public static Iterable<RestorableUniformRandomProvider> list() { return Collections.unmodifiableList(LIST); } /** * Subclasses that are "parametric" tests can forward the call to * the "@Parameters"-annotated method to this method. * * @return the list of 32-bits based generators. */ public static Iterable<RestorableUniformRandomProvider> list32() { return Collections.unmodifiableList(LIST32); } /** * Subclasses that are "parametric" tests can forward the call to * the "@Parameters"-annotated method to this method. * * @return the list of 64-bits based generators. */ public static Iterable<RestorableUniformRandomProvider> list64() { return Collections.unmodifiableList(LIST64); } /** * Subclasses that are "parametric" tests can forward the call to * the "@Parameters"-annotated method to this method. * * @return the list of {@link JumpableUniformRandomProvider} generators. */ public static Iterable<JumpableUniformRandomProvider> listJumpable() { return Collections.unmodifiableList(LIST_JUMP); } /** * Subclasses that are "parametric" tests can forward the call to * the "@Parameters"-annotated method to this method. * * @return the list of {@link SplittableUniformRandomProvider} generators. */ public static Iterable<SplittableUniformRandomProvider> listSplittable() { return Collections.unmodifiableList(LIST_SPLIT); } }
3,030
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/JumpableProvidersParametricTest.java
/* * 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.rng.core; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import java.util.Arrays; import org.apache.commons.rng.JumpableUniformRandomProvider; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.RandomProviderState; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.source32.IntProvider; import org.apache.commons.rng.core.source64.LongProvider; /** * Tests which all {@link JumpableUniformRandomProvider} generators must pass. */ class JumpableProvidersParametricTest { /** The default state for the IntProvider. */ private static final byte[] INT_PROVIDER_STATE; /** The default state for the LongProvider. */ private static final byte[] LONG_PROVIDER_STATE; static { INT_PROVIDER_STATE = new State32Generator().getState(); LONG_PROVIDER_STATE = new State64Generator().getState(); } /** * Gets the list of Jumpable generators. * * @return the list */ private static Iterable<JumpableUniformRandomProvider> getJumpableProviders() { return ProvidersList.listJumpable(); } /** * Gets the function using the {@link LongJumpableUniformRandomProvider#longJump()} method. * If the RNG is not long jumpable then this will raise an exception to skip the test. * * @param generator RNG under test. * @return the jump function */ private static TestJumpFunction getLongJumpFunction(JumpableUniformRandomProvider generator) { Assumptions.assumeTrue(generator instanceof LongJumpableUniformRandomProvider, "No long jump function"); final LongJumpableUniformRandomProvider rng2 = (LongJumpableUniformRandomProvider) generator; return rng2::jump; } /** * Test that the random generator returned from the jump is a new instance of the same class. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testJumpReturnsACopy(JumpableUniformRandomProvider generator) { assertJumpReturnsACopy(generator::jump, generator); } /** * Test that the random generator returned from the long jump is a new instance of the same class. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testLongJumpReturnsACopy(JumpableUniformRandomProvider generator) { assertJumpReturnsACopy(getLongJumpFunction(generator), generator); } /** * Assert that the random generator returned from the jump function is a new instance of the same class. * * @param jumpFunction Jump function to test. * @param generator RNG under test. */ private static void assertJumpReturnsACopy(TestJumpFunction jumpFunction, JumpableUniformRandomProvider generator) { final UniformRandomProvider copy = jumpFunction.jump(); Assertions.assertNotSame(generator, copy, "The copy instance should be a different object"); Assertions.assertEquals(generator.getClass(), copy.getClass(), "The copy instance should be the same class"); } /** * Test that the random generator state of the copy instance returned from the jump * matches the input state. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testJumpCopyMatchesPreJumpState(JumpableUniformRandomProvider generator) { assertCopyMatchesPreJumpState(generator::jump, generator); } /** * Test that the random generator state of the copy instance returned from the long jump * matches the input state. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testLongJumpCopyMatchesPreJumpState(JumpableUniformRandomProvider generator) { assertCopyMatchesPreJumpState(getLongJumpFunction(generator), generator); } /** * Assert that the random generator state of the copy instance returned from the jump * function matches the input state. * * <p>The generator must be a {@link RestorableUniformRandomProvider} and return an * instance of {@link RandomProviderDefaultState}.</p> * * <p>The input generator is sampled using methods in the * {@link UniformRandomProvider} interface, the state is saved and a jump is * performed. The states from the pre-jump generator and the returned copy instance * must match.</p> * * <p>This test targets any cached state of the default implementation of a generator * in {@link IntProvider} and {@link LongProvider} such as the state cached for the * nextBoolean() and nextInt() functions.</p> * * @param jumpFunction Jump function to test. * @param generator RNG under test. */ private static void assertCopyMatchesPreJumpState(TestJumpFunction jumpFunction, JumpableUniformRandomProvider generator) { Assumptions.assumeTrue(generator instanceof RestorableUniformRandomProvider, "Not a restorable RNG"); for (int repeats = 0; repeats < 2; repeats++) { // Exercise the generator. // This calls nextInt() once so the default implementation of LongProvider // should have cached a state for nextInt() in one of the two repeats. // Calls nextBoolean() to ensure a cached state in one of the two repeats. generator.nextInt(); generator.nextBoolean(); final RandomProviderState preJumpState = ((RestorableUniformRandomProvider) generator).saveState(); Assumptions.assumeTrue(preJumpState instanceof RandomProviderDefaultState, "Not a recognized state"); final UniformRandomProvider copy = jumpFunction.jump(); final RandomProviderState copyState = ((RestorableUniformRandomProvider) copy).saveState(); final RandomProviderDefaultState expected = (RandomProviderDefaultState) preJumpState; final RandomProviderDefaultState actual = (RandomProviderDefaultState) copyState; Assertions.assertArrayEquals(expected.getState(), actual.getState(), "The copy instance state should match the state of the original"); } } /** * Test that a jump resets the state of the default implementation of a generator in * {@link IntProvider} and {@link LongProvider}. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testJumpResetsDefaultState(JumpableUniformRandomProvider generator) { assertJumpResetsDefaultState(generator::jump, generator); } /** * Test that a long jump resets the state of the default implementation of a generator in * {@link IntProvider} and {@link LongProvider}. */ @ParameterizedTest @MethodSource("getJumpableProviders") void testLongJumpResetsDefaultState(JumpableUniformRandomProvider generator) { assertJumpResetsDefaultState(getLongJumpFunction(generator), generator); } /** * Assert the jump resets the specified number of bytes of the state. The bytes are * checked from the end of the saved state. * * <p>This is intended to check the default state of the base implementation of * {@link IntProvider} and {@link LongProvider} is reset.</p> * * @param jumpFunction Jump function to test. * @param generator RNG under test. */ private static void assertJumpResetsDefaultState(TestJumpFunction jumpFunction, JumpableUniformRandomProvider generator) { byte[] expected; if (generator instanceof IntProvider) { expected = INT_PROVIDER_STATE; } else if (generator instanceof LongProvider) { expected = LONG_PROVIDER_STATE; } else { throw new AssertionError("Unsupported RNG"); } final int stateSize = expected.length; for (int repeats = 0; repeats < 2; repeats++) { // Exercise the generator. // This calls nextInt() once so the default implementation of LongProvider // should have cached a state for nextInt() in one of the two repeats. // Calls nextBoolean() to ensure a cached state in one of the two repeats. generator.nextInt(); generator.nextBoolean(); jumpFunction.jump(); // An Int/LongProvider so must be a RestorableUniformRandomProvider final RandomProviderState postJumpState = ((RestorableUniformRandomProvider) generator).saveState(); final byte[] actual = ((RandomProviderDefaultState) postJumpState).getState(); Assumptions.assumeTrue(actual.length >= stateSize, "Implementation has removed default state"); // The implementation requires that any sub-class state is prepended to the // state thus the default state is at the end. final byte[] defaultState = Arrays.copyOfRange(actual, actual.length - stateSize, actual.length); Assertions.assertArrayEquals(expected, defaultState, "The jump should reset the default state to zero"); } } /** * Dummy class for checking the state size of the IntProvider. */ static class State32Generator extends IntProvider { /** {@inheritDoc} */ @Override public int next() { return 0; } /** * Gets the default state. This captures the initial state of the IntProvider * including the values of the cache variables. * * @return the state */ byte[] getState() { return getStateInternal(); } } /** * Dummy class for checking the state size of the LongProvider. */ static class State64Generator extends LongProvider { /** {@inheritDoc} */ @Override public long next() { return 0; } /** * Gets the default state. This captures the initial state of the LongProvider * including the values of the cache variables. * * @return the state size */ byte[] getState() { return getStateInternal(); } } /** * Specify the jump operation to test. * * <p>This allows testing {@link JumpableUniformRandomProvider} or * {@link LongJumpableUniformRandomProvider}.</p> */ interface TestJumpFunction { /** * Perform the jump and return a pre-jump copy. * * @return the pre-jump copy. */ UniformRandomProvider jump(); } }
3,031
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/Providers32ParametricTest.java
/* * 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.rng.core; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.apache.commons.rng.RestorableUniformRandomProvider; /** * Tests which all 32-bits based generators must pass. */ class Providers32ParametricTest { private static Iterable<RestorableUniformRandomProvider> getList() { return ProvidersList.list32(); } @ParameterizedTest @MethodSource("getList") void testNextBytesChunks(RestorableUniformRandomProvider generator) { final int[] chunkSizes = {4, 8, 12, 16}; final int[] chunks = {1, 2, 3, 4, 5}; for (final int chunkSize : chunkSizes) { for (final int numChunks : chunks) { ProvidersCommonParametricTest.checkNextBytesChunks(generator, chunkSize, numChunks); } } } }
3,032
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/RandomProviderDefaultStateTest.java
/* * 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.rng.core; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; /** * Tests for {@link RandomProviderDefaultState}. */ class RandomProviderDefaultStateTest { @Test void testConsistency() { final byte[] internalState = {1, 0, -23, 67, -128, 54, 100, 127}; final RandomProviderDefaultState state = new RandomProviderDefaultState(internalState); Assertions.assertArrayEquals(internalState, state.getState()); } }
3,033
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
/* * 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.rng.core.util; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; import java.util.stream.LongStream; import org.apache.commons.math3.util.Precision; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; /** * Tests for the {@link NumberFactory}. */ class NumberFactoryTest { /** sizeof(int) in bytes. */ private static final int INT_SIZE = Integer.BYTES; /** sizeof(long) in bytes. */ private static final int LONG_SIZE = Long.BYTES; /** Test values. */ private static final long[] LONG_TEST_VALUES = new long[] {0L, 1L, -1L, 19337L, 1234567891011213L, -11109876543211L, Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE, 0x9e3779b97f4a7c13L}; /** Test values. */ private static final int[] INT_TEST_VALUES = new int[] {0, 1, -1, 19337, 1234567891, -1110987656, Integer.MAX_VALUE, Integer.MIN_VALUE, 0x9e3779b9}; /** * Provide a stream of the test values for long conversion. * * @return the stream */ static LongStream longTestValues() { return Arrays.stream(LONG_TEST_VALUES); } /** * Provide a stream of the test values for long conversion. * * @return the stream */ static IntStream intTestValues() { return Arrays.stream(INT_TEST_VALUES); } @ParameterizedTest @MethodSource(value = {"intTestValues"}) void testMakeBooleanFromInt(int v) { // Test if the bit is set differently then the booleans are opposite final boolean b1 = NumberFactory.makeBoolean(v); final boolean b2 = NumberFactory.makeBoolean(~v); Assertions.assertNotEquals(b1, b2); } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testMakeBooleanFromLong(long v) { // Test if the bit is set differently then the booleans are opposite final boolean b1 = NumberFactory.makeBoolean(v); final boolean b2 = NumberFactory.makeBoolean(~v); Assertions.assertNotEquals(b1, b2); } @Test void testMakeIntFromLong() { // Test the high order bits and low order bits are xor'd together Assertions.assertEquals(0xffffffff, NumberFactory.makeInt(0xffffffff00000000L)); Assertions.assertEquals(0x00000000, NumberFactory.makeInt(0xffffffffffffffffL)); Assertions.assertEquals(0xffffffff, NumberFactory.makeInt(0x00000000ffffffffL)); Assertions.assertEquals(0x00000000, NumberFactory.makeInt(0x0000000000000000L)); Assertions.assertEquals(0x0f0f0f0f, NumberFactory.makeInt(0x0f0f0f0f00000000L)); Assertions.assertEquals(0xf0f0f0f0, NumberFactory.makeInt(0x00000000f0f0f0f0L)); Assertions.assertEquals(0x00000000, NumberFactory.makeInt(0x0f0f0f0f0f0f0f0fL)); Assertions.assertEquals(0xffffffff, NumberFactory.makeInt(0x0f0f0f0ff0f0f0f0L)); } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testExtractLoExtractHi(long v) { final int vL = NumberFactory.extractLo(v); final int vH = NumberFactory.extractHi(v); final long actual = (((long) vH) << 32) | (vL & 0xffffffffL); Assertions.assertEquals(v, actual); } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testLong2Long(long v) { final int vL = NumberFactory.extractLo(v); final int vH = NumberFactory.extractHi(v); Assertions.assertEquals(v, NumberFactory.makeLong(vH, vL)); } @Test void testLongToByteArraySignificanceOrder() { // Start at the least significant bit long value = 1; for (int i = 0; i < LONG_SIZE; i++) { final byte[] b = NumberFactory.makeByteArray(value); for (int j = 0; j < LONG_SIZE; j++) { // Only one byte should be non zero Assertions.assertEquals(b[j] != 0, j == i); } // Shift to the next byte value <<= 8; } } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testLongToBytesIsLittleEndian(long v) { final ByteBuffer bb = ByteBuffer.allocate(LONG_SIZE).order(ByteOrder.LITTLE_ENDIAN); bb.putLong(v); Assertions.assertArrayEquals(bb.array(), NumberFactory.makeByteArray(v)); } @RepeatedTest(value = 5) void testByteArrayToLongArrayIsLittleEndian() { final int n = 5; byte[] bytes = new byte[n * LONG_SIZE]; ThreadLocalRandom.current().nextBytes(bytes); final ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); final long[] data = NumberFactory.makeLongArray(bytes); for (int i = 0; i < n; i++) { Assertions.assertEquals(bb.getLong(), data[i]); } Assertions.assertArrayEquals(bytes, NumberFactory.makeByteArray(data)); } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testLongFromByteArray2Long(long expected) { final byte[] b = NumberFactory.makeByteArray(expected); Assertions.assertEquals(expected, NumberFactory.makeLong(b)); } @Test void testLongArrayFromByteArray2LongArray() { final byte[] b = NumberFactory.makeByteArray(LONG_TEST_VALUES); Assertions.assertArrayEquals(LONG_TEST_VALUES, NumberFactory.makeLongArray(b)); } @ParameterizedTest @MethodSource(value = {"longTestValues"}) void testLongArrayToByteArrayMatchesLongToByteArray(long v) { // Test individually the bytes are the same as the array conversion final byte[] b1 = NumberFactory.makeByteArray(v); final byte[] b2 = NumberFactory.makeByteArray(new long[] {v}); Assertions.assertArrayEquals(b1, b2); } @Test void testIntToByteArraySignificanceOrder() { // Start at the least significant bit int value = 1; for (int i = 0; i < INT_SIZE; i++) { final byte[] b = NumberFactory.makeByteArray(value); for (int j = 0; j < INT_SIZE; j++) { // Only one byte should be non zero Assertions.assertEquals(b[j] != 0, j == i); } // Shift to the next byte value <<= 8; } } @ParameterizedTest @MethodSource(value = {"intTestValues"}) void testIntToBytesIsLittleEndian(int v) { final ByteBuffer bb = ByteBuffer.allocate(INT_SIZE).order(ByteOrder.LITTLE_ENDIAN); bb.putInt(v); Assertions.assertArrayEquals(bb.array(), NumberFactory.makeByteArray(v)); } @RepeatedTest(value = 5) void testByteArrayToIntArrayIsLittleEndian() { final int n = 5; byte[] bytes = new byte[n * INT_SIZE]; ThreadLocalRandom.current().nextBytes(bytes); final ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); final int[] data = NumberFactory.makeIntArray(bytes); for (int i = 0; i < n; i++) { Assertions.assertEquals(bb.getInt(), data[i]); } Assertions.assertArrayEquals(bytes, NumberFactory.makeByteArray(data)); } @ParameterizedTest @MethodSource(value = {"intTestValues"}) void testIntFromByteArray2Int(int expected) { final byte[] b = NumberFactory.makeByteArray(expected); Assertions.assertEquals(expected, NumberFactory.makeInt(b)); } @Test void testIntArrayFromByteArray2IntArray() { final byte[] b = NumberFactory.makeByteArray(INT_TEST_VALUES); Assertions.assertArrayEquals(INT_TEST_VALUES, NumberFactory.makeIntArray(b)); } @ParameterizedTest @MethodSource(value = {"intTestValues"}) void testIntArrayToByteArrayMatchesIntToByteArray(int v) { // Test individually the bytes are the same as the array conversion final byte[] b1 = NumberFactory.makeByteArray(v); final byte[] b2 = NumberFactory.makeByteArray(new int[] {v}); Assertions.assertArrayEquals(b1, b2); } @Test void testMakeIntPrecondition1() { for (int i = 0; i <= 10; i++) { final byte[] bytes = new byte[i]; if (i != INT_SIZE) { Assertions.assertThrows(IllegalArgumentException.class, () -> NumberFactory.makeInt(bytes)); } else { Assertions.assertEquals(0, NumberFactory.makeInt(bytes)); } } } @Test void testMakeIntArrayPrecondition1() { for (int i = 0; i <= 20; i++) { final byte[] bytes = new byte[i]; if (i != 0 && i % INT_SIZE != 0) { Assertions.assertThrows(IllegalArgumentException.class, () -> NumberFactory.makeIntArray(bytes)); } else { Assertions.assertArrayEquals(new int[i / INT_SIZE], NumberFactory.makeIntArray(bytes)); } } } @Test void testMakeLongPrecondition1() { for (int i = 0; i <= 10; i++) { final byte[] bytes = new byte[i]; if (i != LONG_SIZE) { Assertions.assertThrows(IllegalArgumentException.class, () -> NumberFactory.makeLong(bytes)); } else { Assertions.assertEquals(0L, NumberFactory.makeLong(bytes)); } } } @Test void testMakeLongArrayPrecondition1() { for (int i = 0; i <= 20; i++) { final byte[] bytes = new byte[i]; if (i != 0 && i % LONG_SIZE != 0) { Assertions.assertThrows(IllegalArgumentException.class, () -> NumberFactory.makeLongArray(bytes)); } else { Assertions.assertArrayEquals(new long[i / LONG_SIZE], NumberFactory.makeLongArray(bytes)); } } } /** * Test different methods for generation of a {@code float} from a {@code int}. The output * value should be in the range between 0 and 1. */ @Test void testFloatGenerationMethods() { final int allBits = 0xffffffff; // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1. assertCloseToNotAbove1((allBits >>> 9) * 0x1.0p-23f, 2); assertCloseToNotAbove1((allBits >>> 8) * 0x1.0p-24f, 1); assertCloseToNotAbove1(Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, 2); final int noBits = 0; Assertions.assertEquals(0.0f, (noBits >>> 9) * 0x1.0p-23f); Assertions.assertEquals(0.0f, (noBits >>> 8) * 0x1.0p-24f); Assertions.assertEquals(0.0f, Float.intBitsToFloat(0x7f << 23 | noBits >>> 9) - 1.0f); } /** * Test different methods for generation of a {@code double} from a {@code long}. The output * value should be in the range between 0 and 1. */ @Test void testDoubleGenerationMethods() { final long allBits = 0xffffffffffffffffL; // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1. assertCloseToNotAbove1((allBits >>> 12) * 0x1.0p-52d, 2); assertCloseToNotAbove1((allBits >>> 11) * 0x1.0p-53d, 1); assertCloseToNotAbove1(Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, 2); final long noBits = 0; Assertions.assertEquals(0.0, (noBits >>> 12) * 0x1.0p-52d); Assertions.assertEquals(0.0, (noBits >>> 11) * 0x1.0p-53d); Assertions.assertEquals(0.0, Double.longBitsToDouble(0x3ffL << 52 | noBits >>> 12) - 1.0); } @Test void testMakeDoubleFromLong() { final long allBits = 0xffffffffffffffffL; final long noBits = 0; // Within 1 ULP of 1.0 assertCloseToNotAbove1(NumberFactory.makeDouble(allBits), 1); Assertions.assertEquals(0.0, NumberFactory.makeDouble(noBits)); } @Test void testMakeDoubleFromIntInt() { final int allBits = 0xffffffff; final int noBits = 0; // Within 1 ULP of 1.0 assertCloseToNotAbove1(NumberFactory.makeDouble(allBits, allBits), 1); Assertions.assertEquals(0.0, NumberFactory.makeDouble(noBits, noBits)); } @Test void testMakeFloatFromInt() { final int allBits = 0xffffffff; final int noBits = 0; // Within 1 ULP of 1.0f assertCloseToNotAbove1(NumberFactory.makeFloat(allBits), 1); Assertions.assertEquals(0.0f, NumberFactory.makeFloat(noBits)); } /** * Assert that the value is close to but <strong>not above</strong> 1. This is used to test * the output from methods that produce a {@code float} value that must be in the range * between 0 and 1. * * @param value the value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point values between x and y. * @see Precision#equals(float, float, int) */ private static void assertCloseToNotAbove1(float value, int maxUlps) { Assertions.assertTrue(value <= 1.0f, "Not <= 1.0f"); Assertions.assertTrue(Precision.equals(1.0f, value, maxUlps), () -> "Not equal to 1.0f within units of least precision: " + maxUlps); } /** * Assert that the value is close to but <strong>not above</strong> 1. This is used to test * the output from methods that produce a {@code double} value that must be in the range * between 0 and 1. * * @param value the value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point values between x and y. * @see Precision#equals(double, double, int) */ private static void assertCloseToNotAbove1(double value, int maxUlps) { Assertions.assertTrue(value <= 1.0, "Not <= 1.0"); Assertions.assertTrue(Precision.equals(1.0, value, maxUlps), () -> "Not equal to 1.0 within units of least precision: " + maxUlps); } }
3,034
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/RandomStreamsTestHelper.java
/* * 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.rng.core.util; import org.apache.commons.rng.UniformRandomProvider; /** * Test helper class to expose package-private functionality for tests in other packages. */ public final class RandomStreamsTestHelper { /** No instances. */ private RandomStreamsTestHelper() {} /** * Creates a seed to prepend to a counter. This method makes public the package-private * seed generation method used in {@link RandomStreams} for test classes in other packages. * * @param rng Source of randomness. * @return the seed */ public static long createSeed(UniformRandomProvider rng) { return RandomStreams.createSeed(rng); } }
3,035
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/RandomStreamsTest.java
/* * 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.rng.core.util; import java.util.Arrays; import java.util.Spliterator; import java.util.SplittableRandom; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; import java.util.function.IntConsumer; import java.util.function.LongConsumer; import java.util.function.Supplier; import java.util.stream.LongStream; import org.apache.commons.math3.stat.inference.ChiSquareTest; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.util.RandomStreams.SeededObjectFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; /** * Tests for {@link RandomStreams}. */ class RandomStreamsTest { /** The size in bits of the seed characters. */ private static final int CHAR_BITS = 4; /** * Class for outputting a unique sequence from the nextLong() method even under * recursive splitting. Splitting creates a new instance. */ private static class SequenceGenerator implements SplittableUniformRandomProvider { /** The value for nextLong. */ private final AtomicLong value; /** * @param seed Sequence seed value. */ SequenceGenerator(long seed) { value = new AtomicLong(seed); } /** * @param value The value for nextLong. */ SequenceGenerator(AtomicLong value) { this.value = value; } @Override public long nextLong() { return value.getAndIncrement(); } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { // Ignore the source (use of the source is optional) return new SequenceGenerator(value); } } /** * Class for decoding the combined seed ((seed << shift) | position). * Requires the unshifted seed. The shift is assumed to be a multiple of 4. * The first call to the consumer will extract the current position. * Further calls will compare the value with the predicted value using * the last known position. */ private static class SeedDecoder implements Consumer<Long>, LongConsumer { /** The initial (unshifted) seed. */ private final long initial; /** The current shifted seed. */ private long seed; /** The last known position. */ private long position = -1; /** * @param initial Unshifted seed value. */ SeedDecoder(long initial) { this.initial = initial; } @Override public void accept(long value) { if (position < 0) { // Search for the initial seed value seed = initial; long mask = -1; while (seed != 0 && (value & mask) != seed) { seed <<= CHAR_BITS; mask <<= CHAR_BITS; } if (seed == 0) { Assertions.fail(() -> String.format("Failed to decode position from %s using seed %s", Long.toBinaryString(value), Long.toBinaryString(initial))); } // Remove the seed contribution leaving the position position = value & ~seed; } else { // Predict final long expected = position + 1; //seed = initial; while (seed != 0 && Long.compareUnsigned(Long.lowestOneBit(seed), expected) <= 0) { seed <<= CHAR_BITS; } Assertions.assertEquals(expected | seed, value); position = expected; } } @Override public void accept(Long t) { accept(t.longValue()); } /** * Reset the decoder. */ void reset() { position = -1; } } /** * Test the seed has the required properties: * <ul> * <li>Test the seed has an odd character in the least significant position * <li>Test the remaining characters in the seed do not match this character * <li>Test the distribution of characters is uniform * <ul> * * <p>The test assumes the character size is 4-bits. * * @param seed the seed */ @ParameterizedTest @ValueSource(longs = {1628346812812L}) void testCreateSeed(long seed) { final UniformRandomProvider rng = new SplittableRandom(seed)::nextLong; // Histogram the distribution for each unique 4-bit character final int m = (1 << CHAR_BITS) - 1; // Number of remaining characters final int n = (int) Math.ceil((Long.SIZE - CHAR_BITS) / CHAR_BITS); final int[][] h = new int[m + 1][m + 1]; final int samples = 1 << 16; for (int i = 0; i < samples; i++) { long s = RandomStreams.createSeed(rng); final int unique = (int) (s & m); for (int j = 0; j < n; j++) { s >>>= CHAR_BITS; h[unique][(int) (s & m)]++; } } // Test unique characters are always odd. final int[] empty = new int[m + 1]; for (int i = 0; i <= m; i += 2) { Assertions.assertArrayEquals(empty, h[i], "Even histograms should be empty"); } // Test unique characters are not repeated for (int i = 1; i <= m; i += 2) { Assertions.assertEquals(0, h[i][i]); } // Chi-square test the distribution of unique characters final long[] sum = new long[(m + 1) / 2]; for (int i = 1; i <= m; i += 2) { final long total = Arrays.stream(h[i]).sum(); Assertions.assertEquals(0, total % n, "Samples should be a multiple of the number of characters"); sum[i / 2] = total / n; } assertChiSquare(sum, () -> "Unique character distribution"); // Chi-square test the distribution for each unique character. // Note: This will fail if the characters do not evenly divide into 64. // In that case the expected values are not uniform as the final // character will be truncated and skew the expected values to lower characters. // For simplicity this has not been accounted for as 4-bits evenly divides 64. Assertions.assertEquals(0, Long.SIZE % CHAR_BITS, "Character distribution cannot be tested as uniform"); for (int i = 1; i <= m; i += 2) { final long[] obs = Arrays.stream(h[i]).filter(c -> c != 0).asLongStream().toArray(); final int c = i; assertChiSquare(obs, () -> "Other character distribution for unique character " + c); } } /** * Assert the observations are uniform using a chi-square test. * * @param obs Observations. * @param msg Failure message prefix. */ private static void assertChiSquare(long[] obs, Supplier<String> msg) { final ChiSquareTest t = new ChiSquareTest(); final double alpha = 0.001; final double[] expected = new double[obs.length]; Arrays.fill(expected, 1.0 / obs.length); final double p = t.chiSquareTest(expected, obs); Assertions.assertFalse(p < alpha, () -> String.format("%s: chi2 p-value: %s < %s", msg.get(), p, alpha)); } @ParameterizedTest @ValueSource(longs = {-1, -2, Long.MIN_VALUE}) void testGenerateWithSeedInvalidStreamSizeThrows(long size) { final SplittableUniformRandomProvider source = new SequenceGenerator(0); final SeededObjectFactory<Long> factory = (s, r) -> Long.valueOf(s); final IllegalArgumentException ex1 = Assertions.assertThrows(IllegalArgumentException.class, () -> RandomStreams.generateWithSeed(size, source, factory)); // Check the exception method is consistent with UniformRandomProvider stream methods final IllegalArgumentException ex2 = Assertions.assertThrows(IllegalArgumentException.class, () -> source.ints(size)); Assertions.assertEquals(ex2.getMessage(), ex1.getMessage(), "Inconsistent exception message"); } @Test void testGenerateWithSeedNullArgumentThrows() { final long size = 10; final SplittableUniformRandomProvider source = new SequenceGenerator(0); final SeededObjectFactory<Long> factory = (s, r) -> Long.valueOf(s); Assertions.assertThrows(NullPointerException.class, () -> RandomStreams.generateWithSeed(size, null, factory)); Assertions.assertThrows(NullPointerException.class, () -> RandomStreams.generateWithSeed(size, source, null)); } /** * Test that the seed passed to the factory is ((seed << shift) | position). * This is done by creating an initial seed value of 1. When removed the * remaining values should be a sequence. * * @param threads Number of threads. * @param streamSize Stream size. */ @ParameterizedTest @CsvSource({ "1, 23", "4, 31", "4, 3", "8, 127", }) void testGenerateWithSeed(int threads, long streamSize) throws InterruptedException, ExecutionException { // Provide a generator that results in the seed being set as 1. final SplittableUniformRandomProvider rng = new SplittableUniformRandomProvider() { @Override public long nextLong() { return 1; } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { return this; } }; Assertions.assertEquals(1, RandomStreams.createSeed(rng), "Unexpected seed value"); // Create a factory that will return the seed passed to the factory final SeededObjectFactory<Long> factory = (s, r) -> { Assertions.assertSame(rng, r, "The source RNG is not used"); return Long.valueOf(s); }; // Stream in a custom pool final ForkJoinPool threadPool = new ForkJoinPool(threads); Long[] values; try { values = threadPool.submit(() -> RandomStreams.generateWithSeed(streamSize, rng, factory).parallel().toArray(Long[]::new)).get(); } finally { threadPool.shutdown(); } // Remove the highest 1 bit from each long. The rest should be a sequence. final long[] actual = Arrays.stream(values).mapToLong(Long::longValue) .map(l -> l - Long.highestOneBit(l)).sorted().toArray(); final long[] expected = LongStream.range(0, streamSize).toArray(); Assertions.assertArrayEquals(expected, actual); } @Test void testGenerateWithSeedSpliteratorThrows() { final long size = 10; final SplittableUniformRandomProvider source = new SequenceGenerator(0); final SeededObjectFactory<Long> factory = (s, r) -> Long.valueOf(s); final Spliterator<Long> s1 = RandomStreams.generateWithSeed(size, source, factory).spliterator(); final Consumer<Long> badAction = null; final NullPointerException ex1 = Assertions.assertThrows(NullPointerException.class, () -> s1.tryAdvance(badAction), "tryAdvance"); final NullPointerException ex2 = Assertions.assertThrows(NullPointerException.class, () -> s1.forEachRemaining(badAction), "forEachRemaining"); // Check the exception method is consistent with UniformRandomProvider stream methods final Spliterator.OfInt s2 = source.ints().spliterator(); final NullPointerException ex3 = Assertions.assertThrows(NullPointerException.class, () -> s2.tryAdvance((IntConsumer) null), "tryAdvance"); Assertions.assertEquals(ex3.getMessage(), ex1.getMessage(), "Inconsistent tryAdvance exception message"); Assertions.assertEquals(ex3.getMessage(), ex2.getMessage(), "Inconsistent forEachRemaining exception message"); } @Test void testGenerateWithSeedSpliterator() { // Create an initial seed value. This should not be modified by the algorithm // when generating a 'new' seed from the RNG. final long initial = RandomStreams.createSeed(new SplittableRandom()::nextLong); final SplittableUniformRandomProvider rng = new SplittableUniformRandomProvider() { @Override public long nextLong() { return initial; } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { return this; } }; Assertions.assertEquals(initial, RandomStreams.createSeed(rng), "Unexpected seed value"); // Create a factory that will return the seed passed to the factory final SeededObjectFactory<Long> factory = (s, r) -> { Assertions.assertSame(rng, r, "The source RNG is not used"); return Long.valueOf(s); }; // Split a large spliterator into four smaller ones; // each is used to test different functionality final long size = 41; Spliterator<Long> s1 = RandomStreams.generateWithSeed(size, rng, factory).spliterator(); Assertions.assertEquals(size, s1.estimateSize()); Assertions.assertTrue(s1.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE), "Invalid characteristics"); final Spliterator<Long> s2 = s1.trySplit(); final Spliterator<Long> s3 = s1.trySplit(); final Spliterator<Long> s4 = s2.trySplit(); Assertions.assertEquals(size, s1.estimateSize() + s2.estimateSize() + s3.estimateSize() + s4.estimateSize()); // s1. Test cannot split indefinitely while (s1.estimateSize() > 1) { final long currentSize = s1.estimateSize(); final Spliterator<Long> other = s1.trySplit(); Assertions.assertEquals(currentSize, s1.estimateSize() + other.estimateSize()); s1 = other; } Assertions.assertNull(s1.trySplit(), "Cannot split when size <= 1"); // Create an action that will decode the shift and position using the // known initial seed. This can be used to predict and assert the next value. final SeedDecoder action = new SeedDecoder(initial); // s2. Test advance for (long newSize = s2.estimateSize(); newSize-- > 0;) { Assertions.assertTrue(s2.tryAdvance(action)); Assertions.assertEquals(newSize, s2.estimateSize(), "s2 size estimate"); } final Consumer<Long> throwIfCalled = r -> Assertions.fail("spliterator should be empty"); Assertions.assertFalse(s2.tryAdvance(throwIfCalled)); s2.forEachRemaining(throwIfCalled); // s3. Test forEachRemaining action.reset(); s3.forEachRemaining(action); Assertions.assertEquals(0, s3.estimateSize()); s3.forEachRemaining(throwIfCalled); // s4. Test tryAdvance and forEachRemaining when the action throws an exception final IllegalStateException ex = new IllegalStateException(); final Consumer<Long> badAction = r -> { throw ex; }; final long currentSize = s4.estimateSize(); Assertions.assertTrue(currentSize > 1, "Spliterator requires more elements to test advance"); Assertions.assertSame(ex, Assertions.assertThrows(IllegalStateException.class, () -> s4.tryAdvance(badAction))); Assertions.assertEquals(currentSize - 1, s4.estimateSize(), "Spliterator should be advanced even when action throws"); Assertions.assertSame(ex, Assertions.assertThrows(IllegalStateException.class, () -> s4.forEachRemaining(badAction))); Assertions.assertEquals(0, s4.estimateSize(), "Spliterator should be finished even when action throws"); s4.forEachRemaining(throwIfCalled); } /** * Test a very large stream size above 2<sup>60</sup>. * In this case it is not possible to prepend a 4-bit character * to the stream position. The seed passed to the factory will be the stream position. */ @Test void testLargeStreamSize() { // Create an initial seed value. This should not be modified by the algorithm // when generating a 'new' seed from the RNG. final long initial = RandomStreams.createSeed(new SplittableRandom()::nextLong); final SplittableUniformRandomProvider rng = new SplittableUniformRandomProvider() { @Override public long nextLong() { return initial; } @Override public SplittableUniformRandomProvider split(UniformRandomProvider source) { return this; } }; Assertions.assertEquals(initial, RandomStreams.createSeed(rng), "Unexpected seed value"); // Create a factory that will return the seed passed to the factory final SeededObjectFactory<Long> factory = (s, r) -> { Assertions.assertSame(rng, r, "The source RNG is not used"); return Long.valueOf(s); }; final Spliterator<Long> s = RandomStreams.generateWithSeed(1L << 62, rng, factory).spliterator(); // Split uses a divide-by-two approach. The child uses the smaller half. final Spliterator<Long> s1 = s.trySplit(); // Lower half. The next position can be predicted using the decoder. final SeedDecoder action = new SeedDecoder(initial); long size = s1.estimateSize(); for (int i = 1; i <= 5; i++) { Assertions.assertTrue(s1.tryAdvance(action)); Assertions.assertEquals(size - i, s1.estimateSize(), "s1 size estimate"); } // Upper half. This should be just the stream position which we can // collect with a call to advance. final long[] expected = {0}; s.tryAdvance(seed -> expected[0] = seed); size = s.estimateSize(); for (int i = 1; i <= 5; i++) { Assertions.assertTrue(s.tryAdvance(seed -> Assertions.assertEquals(++expected[0], seed))); Assertions.assertEquals(size - i, s.estimateSize(), "s size estimate"); } } }
3,036
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/MersenneTwisterTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class MersenneTwisterTest { @Test void testMakotoNishimura() { final MersenneTwister rng = new MersenneTwister(new int[] {0x123, 0x234, 0x345, 0x456}); /* * Data from * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.out * converted to hexadecimal. */ final int[] expectedSequence = { 0x3fa23623, 0x38fa935f, 0x1c72dc38, 0xf4cf2f5f, 0xfc110f5c, 0xc75677aa, 0xc802152f, 0x0d9155da, 0x304aacd1, 0x9a73f337, 0x989a7a43, 0xc1483a50, 0x268c922d, 0x582fa6ba, 0xfd0cc411, 0x44267b5e, 0xe64aeede, 0xbffce512, 0x69b7263d, 0x43df2416, 0x54c06fe4, 0x4bb1636f, 0xaa772159, 0x692b9302, 0xe6f6290f, 0xec59faf1, 0x0453050b, 0x8e18c8c2, 0x9afc3045, 0xc0f8369c, 0xa6784b64, 0x2b3baca5, 0x241c69b2, 0x102b153e, 0x2aa0204a, 0xc4937ab0, 0x4edada28, 0xfc1a4165, 0x5327669e, 0xdeaa3938, 0x7d45d251, 0xde57ac80, 0xafdc0e1a, 0x630eea2f, 0xac9709be, 0x8d4b2277, 0x3bd93aca, 0x85eff969, 0x7dd7008b, 0xc0f5f7ef, 0xf354dbb7, 0x0d19a749, 0x9245ad51, 0xdd1bbe7b, 0x31ce56ca, 0x8fc36f3b, 0xa307eaa6, 0x2d16123c, 0x81a710e8, 0x6fb0ece0, 0xa4a8ed5d, 0xf81025ee, 0xc7bdb7cc, 0x0acbd297, 0xc2e69738, 0x2e414667, 0x8436773b, 0xb2eb0184, 0x6f458090, 0x7f2c9350, 0x0213f68e, 0x470b7a6d, 0xb8e983ba, 0xadd68783, 0x3c580a4a, 0x8239e155, 0xfdc2c299, 0xacd2d0b2, 0xe39381d6, 0xb4a5ad7e, 0x4c6c3bdd, 0x1908bf3a, 0x8aaa5fe5, 0xa3674b65, 0x4c2d0c3f, 0xdf2ba5a5, 0x1032fcf8, 0x9c8a9da2, 0x989f1b17, 0xb62684e4, 0xfc56810e, 0x4937dc5d, 0xd6502fba, 0x403fad7e, 0x8ecf04fa, 0x6e622af6, 0xb3a12932, 0x7735265b, 0xb3397c02, 0x3e92651e, 0x58bca8af, 0xd02046e6, 0x06394b11, 0x91ed9821, 0xb75225a3, 0xe6cf1b38, 0x35297ffe, 0xeaa2f3af, 0x8740f423, 0x9cf755ec, 0x3e71ab47, 0x9b3f3b19, 0xa17cb981, 0x745c768b, 0x0c5fa06c, 0xa9ddfe32, 0x27fb2a2d, 0x83c11cc4, 0x1be0b4bd, 0x0fadc6d9, 0xd4c4cf4b, 0x3e2019a7, 0xe6489797, 0x5746fcb5, 0xa468a4c8, 0xe1f303c8, 0x892aba04, 0xb92617d6, 0x0079af91, 0xa6719544, 0x2123c124, 0x250f6867, 0x4ed30865, 0x32e1592c, 0x28f2364b, 0x56bb6094, 0x39162749, 0x6b68d894, 0x3ce35fee, 0xcfc8dc4f, 0x71602f12, 0xdc9ae288, 0xf8ef2ed7, 0x6b06d07a, 0x216e06f0, 0xdec994b2, 0xbb3a7736, 0x5c9957e9, 0xc8bca92a, 0x2a6955f6, 0x93876aff, 0x0fac9a03, 0x0efc4f05, 0xb1a05dc2, 0x6bae0207, 0x39c9d223, 0xd25d4245, 0xa3194800, 0xb20d013e, 0x6249fe2f, 0x837f9243, 0xb5af74d1, 0xda4d5e81, 0xbb17131c, 0x9e8e92bc, 0x631fa28c, 0x6c4862df, 0x188d56e3, 0xe7b5f3c2, 0x8be50cef, 0x3c846d8b, 0x47bd5cf0, 0x816608b6, 0x99263fac, 0x3082b3dd, 0x41e2e09e, 0x554d804b, 0x0d25a3b7, 0xfcf0b24b, 0xf91f82af, 0x9b6ad24b, 0xb83703f9, 0x10d431ab, 0x675e6dc2, 0x52f58561, 0x3badef9a, 0x31592d6b, 0x70748fc3, 0xea8cd203, 0x9cdde8aa, 0xe8c41002, 0x0696ec53, 0x91e8d6be, 0xdd90041f, 0xc4fb23f3, 0xdee5fd0f, 0xb65694ac, 0x38dba6c3, 0x9a6226c2, 0x4380bf2d, 0xe23c29cf, 0xa62df7dd, 0x3a8a7f47, 0xcef7d974, 0x3341ae98, 0x9fcced1f, 0x89117a75, 0x2697345b, 0x0dec75a6, 0x243febfb, 0xdbe6ab5d, 0x6e41602c, 0x5fded1ce, 0xec854a95, 0xa0839470, 0xa9fc1f3c, 0x0eb51cb9, 0xd58d7944, 0xc6767655, 0xf096123e, 0x3c60b5b2, 0x38abc388, 0xec0546a8, 0x974f331d, 0x0b68e0fe, 0x9e7008a7, 0xc3496c73, 0xae81ba8c, 0x499065db, 0x727d51f3, 0xd2c437c1, 0x5b879655, 0x822f5cf7, 0xc3525dae, 0x198a3598, 0x8dc3afd0, 0xf1fb2ace, 0xe9470165, 0xa2d922ee, 0x03d634c3, 0xdfdafa3a, 0x323cb60d, 0xa8142cc2, 0x8fedaffd, 0x3c344d7e, 0x6da7d412, 0x8faa6de0, 0x091b26b9, 0xcfb90648, 0xf4811a08, 0xaa091e50, 0x3472e158, 0x6a650e2e, 0xa3cf7e2f, 0x50c01ec0, 0xc2c67020, 0x87402425, 0xb2e34cb9, 0xbd87d91b, 0x3563a6d3, 0x339cf74e, 0xffcc2cf9, 0x05537400, 0x57ae171b, 0xf5396a89, 0xbed9b927, 0xaaea3a07, 0x92add60c, 0xd6cec30b, 0x85729ab0, 0xc5326ca8, 0x7b83f953, 0xa43ff0cf, 0xe0eea920, 0x65de4954, 0xff454bcb, 0xa3af3b3a, 0xa8d5900a, 0x19d5c121, 0xbd4a89ac, 0x019084ae, 0x98130311, 0x0aeba10a, 0xe80fa57c, 0x72ed83fd, 0x4fb367f3, 0xcc63dc6a, 0xc3372454, 0x6a37e254, 0xfe2da32c, 0x6fda8ee3, 0xffaf02b4, 0xc027d10e, 0x6709f3e9, 0xf27c7cfe, 0xaaefc51f, 0x446a04a8, 0x120d944c, 0x6723ed43, 0x72ab3b17, 0x465e7e94, 0x5d910b0f, 0x0bd96427, 0x216d0655, 0x7d9b8ad4, 0xa14a22ac, 0xcd90160a, 0xdb3f890b, 0x2d37dcc3, 0x34b8c67f, 0x1cfd3624, 0x69ca4cc3, 0xe756cff2, 0x9d122b27, 0x46667c33, 0x59ee9c5c, 0xbd7b82d1, 0x5f0576e1, 0x499ef4c9, 0x1370277c, 0x8954bac1, 0x37da5236, 0xa02eb689, 0xbe47fedc, 0x776ea717, 0x6cb476ac, 0xa47b4a6a, 0x00999efe, 0x5b639436, 0xf650de88, 0x04e8fd98, 0x191216d2, 0xceaed09b, 0x204794eb, 0xd2c25028, 0x87bd221d, 0xc68852c5, 0xbfaafd1e, 0xf534182b, 0xfaa3297f, 0x80e14a8d, 0xc204c912, 0x84449c0d, 0xb184ee7d, 0x0f3f7b53, 0xaa6e3294, 0xb89cbf37, 0x992ad523, 0x6efb1e90, 0xad46858f, 0xa217c69e, 0x3b8123f6, 0x9651ad17, 0x8f4c0a9a, 0x0f76fc20, 0x7c62f8c3, 0x6da9dc30, 0x70dd3d5d, 0x96ae2e55, 0x58c655e4, 0xaa853035, 0x048e81ea, 0x87002693, 0x2c667e97, 0x9a2f4d5b, 0x52933a95, 0x03f72a82, 0x6c6d501a, 0x713505ec, 0x7d0f9784, 0x6aebb0b6, 0x107a7e37, 0x15773151, 0xf90a74da, 0x2377363c, 0x0a8de6de, 0xa6e5d5a2, 0x5dca95f2, 0x5c53d917, 0x168ec0cf, 0x570dfc18, 0x4288fe6b, 0xb9833a27, 0xdd1e7904, 0x315a4474, 0xd5631e5f, 0x7213cfd6, 0x249938a9, 0xff052817, 0x718eb33c, 0x02b4dec3, 0xc4964842, 0x469f5baa, 0xd941297a, 0xe9dded03, 0x60fac145, 0xdb37f217, 0x39cd8ee4, 0x452c33d1, 0x5abe9be8, 0x1af69e0d, 0x5b0bd6d7, 0x319ecd5f, 0x3bcd235c, 0x3fdbfa77, 0xac550eb4, 0x3a0a346c, 0xf39919d4, 0x6e1f71ee, 0xe832ed0e, 0x84c9d733, 0x60a2537d, 0xabbdd4b3, 0xd598dffd, 0xd13c8592, 0x32a03cc7, 0x336e5faf, 0x2f8e590a, 0xaaeec5d4, 0xa8295b34, 0xc30ce8ca, 0xee4c5501, 0x0c77d054, 0x6b982f24, 0x00678195, 0xa849138f, 0x2f3dd18e, 0xfe23a88a, 0x2e47871d, 0xe263d69f, 0xaa1419fe, 0xa2b0fa92, 0x3fe37d3d, 0x4f37161e, 0x4cd9b682, 0xc65860f6, 0x77e4edc3, 0x04c71a18, 0x36fb25b8, 0x451411f4, 0x635fecf1, 0x92b03a64, 0x9b7fd167, 0x171906d3, 0xc76604e6, 0x59257d37, 0xf444dead, 0xbc26a68d, 0xd225e427, 0xf675b085, 0x1aa04db0, 0x7f683b77, 0xd79d3278, 0x308425a0, 0x4504d28a, 0xde9ae465, 0x64275e5a, 0xbd910789, 0xd13421f7, 0x84ce54b4, 0x3c166b93, 0x1d040e83, 0x337c6ae7, 0xbe1630aa, 0x3e9a6e14, 0xe125a856, 0xffce8ca5, 0x324f5b19, 0x5050a29f, 0xa878afad, 0x2c6ee4b4, 0x7a891b36, 0xfdeda343, 0x8e420be9, 0xb1a90f55, 0x1aa82dfc, 0x7bd87288, 0x497d36dd, 0xefca266c, 0x536338b5, 0xbb314af9, 0x99c64a66, 0xe230edff, 0x35b07a32, 0xac172bc3, 0x66890dcd, 0xc8b7e513, 0x9f14818d, 0x38f45e55, 0xe39d2420, 0x41e7b802, 0xe7d097d7, 0x87bde5db, 0xa3b40719, 0x6903a4f1, 0x8fe17270, 0xa00bc9ad, 0xfcbd3397, 0x458ad6f3, 0xfb3f1663, 0xb7b4fe23, 0xec0fda7a, 0x6324016b, 0x7c6c5059, 0xf81c1522, 0x957286ba, 0x5e27c862, 0x2dbb10a2, 0x39db5731, 0x1d567daa, 0x55ee48f2, 0x4e5e0742, 0xc27142ca, 0xcbacae9e, 0x5d1a105a, 0xb37e6bbc, 0x4457de32, 0xc2731190, 0x51f2e26b, 0x616f5ec8, 0x4c524088, 0x84eb772e, 0x18fe5f9c, 0xc27be658, 0x025f0b8e, 0x61d91e60, 0x65599112, 0x839a9784, 0x9942f04b, 0x907c8596, 0x2e824b62, 0xa1d696d8, 0xca1de87f, 0x09b97e72, 0x89a8b34e, 0x6edda0f8, 0x21202673, 0x10b55868, 0x5fa7c76b, 0xa1b56faf, 0x670e4131, 0xd8f5502e, 0x25233991, 0xe43445a3, 0xfed6a20a, 0xd19733f7, 0x8bb5db5b, 0x90e132fc, 0x25e17e90, 0xe697ac65, 0x302fda43, 0xb7064f65, 0xff3caaf3, 0x7cc369c7, 0x789d0230, 0x5d7fe246, 0xcbfd430c, 0xf66fcdee, 0xb3d37457, 0xc24547aa, 0xac23da09, 0xbddb1df4, 0xfdd7d1fb, 0x4b8987b3, 0x3cf260e1, 0x30a24d85, 0x375fb86c, 0xb287e74a, 0xc8f1b360, 0xd70e2779, 0x784fa37b, 0x07477485, 0xa787685e, 0x541fbaf2, 0x49d05a21, 0x46bcecac, 0xbc1c4443, 0x85b0917e, 0x693c3ac2, 0x10a30d08, 0xff192733, 0x61d88012, 0xe2474eaf, 0x00cbb899, 0x062b8b3a, 0x5ff1fcdb, 0x20dde01f, 0x94a3ef59, 0x4e75b597, 0xea677d68, 0xf9b9c06b, 0x1b9ee46c, 0xaf1a479a, 0x6e9a7611, 0x9c9284f6, 0x0348a296, 0xe3e3b5ab, 0x2ce8b80a, 0xf11a7efc, 0x4bf1a59c, 0x4301a8f0, 0x057c6a80, 0x7ebd7550, 0x1963d609, 0x17064918, 0x9a5e486f, 0x767ada6e, 0xf379835d, 0x817d0eed, 0xc8a9fc9f, 0xd6c0e87d, 0x8dc9a94f, 0xacf56951, 0xdd8bd5ee, 0xdd248898, 0x9e286bc3, 0xced01226, 0xb88ffe3c, 0xea662cad, 0x9ab07c59, 0x13032ac1, 0xbb873d74, 0x32e0776e, 0x089b5f90, 0xe09d5b09, 0x2ea60da1, 0x80cfe80b, 0x21fa1ce2, 0x706dc2f1, 0x00d96e10, 0x84f75f04, 0x5fa97783, 0xea2b6877, 0xe9c417ed, 0x807f00ac, 0x0802442e, 0x19a90570, 0x72ae16ca, 0x107e4fa1, 0x0bb6135e, 0xd775a370, 0x5ff6f941, 0x6a707d8a, 0x32be82f8, 0xe02e92e3, 0xadf9e5ba, 0xc88902f1, 0xb38bd032, 0xdeae3543, 0x9d8b53e4, 0xe138fc67, 0x4565a728, 0x0c45115d, 0xab6dfd67, 0xd0d19d8a, 0xb77c9c49, 0xcef31821, 0x683e0485, 0xa35e3a23, 0x12d6a276, 0x62f81a9c, 0x0a10fe92, 0x38fb7c97, 0xed7c6de5, 0x21c46edb, 0x3babb813, 0x488587af, 0xaff84a55, 0xe48cce39, 0xe5098cad, 0x9310cf58, 0xece52930, 0xc21bcc91, 0x540108c4, 0x4f44bcdb, 0x898a9365, 0xba470a57, 0xd7f15ff8, 0xe473ab14, 0xe76833ff, 0x89997b1b, 0x9e7f7c54, 0x5673fcd9, 0xd289f943, 0xbdeb72e2, 0x490d3961, 0x4302a415, 0x0a7aa0c1, 0xb35bde2f, 0xa6b2690f, 0xd9acf25b, 0x09950b7e, 0x71621b83, 0x653f519c, 0x43a66e3d, 0x1da1d26a, 0xc4db0a37, 0xf1d8513e, 0xd6c9840b, 0xf39e866b, 0x197fe72c, 0x11196229, 0x311007b0, 0x61028494, 0xdf40c6a7, 0x9a54746c, 0xfa75ce2e, 0x03bf7309, 0xc33d7966, 0xeda6af60, 0xa6672387, 0x72411b87, 0x9a211f11, 0x51f56dc7, 0x53a684e0, 0x0397541a, 0xe33486f2, 0xb4186699, 0x9bbf1cb2, 0x1f86e0f1, 0xa459fea9, 0xc729c354, 0x9c466f44, 0x10479afe, 0xef53e2d2, 0xb8769c4a, 0x60a7fa28, 0x8b551da9, 0x6b17bf70, 0x4b0a4a73, 0xfcd67534, 0x7f77d788, 0xb422f68f, 0xf8ccf23c, 0x1ad3e613, 0xc33054e4, 0x24c8f64a, 0xa135d8b9, 0x5799da51, 0x4ff771cb, 0x15430a3f, 0x46db7ec8, 0x87d4c88f, 0x694bb4c8, 0xa6a5ff3b, 0x037255c0, 0xac77403c, 0x49a7a6d5, 0x61326bcb, 0xea1febca, 0x19905ce4, 0x6c208616, 0x88601b2b, 0xf5ecddc0, 0xec8475d1, 0xd3853b52, 0x3d21610b, 0x53b3a29c, 0x77575565, 0x05825b57, 0x89ebfb88, 0x8f3c2f00, 0xb32ead1a, 0xd44f8744, 0x63a692b2, 0x69697bae, 0x6b7fe1c9, 0x325b5bf1, 0xb32e0e11, 0x54187523, 0x4501dc49, 0x7eb3aed1, 0x6ab0eb88, 0x6b3e94bf, 0x2c2d293b, 0x8f668ba5, 0x2db81bb2, 0x9e43c4ae, 0xda5b5b13, 0x81763a88, 0x95b90733, 0x642b0bca, 0x0dd1dd3a, 0x44d79c88, 0x095e327f, 0x56abfd80, 0xd0f75ce3, 0x730607c6, 0x89a6fd0e, 0x7fc09cd8, 0xb078f466, 0x184ba518, 0xaf1ff8e1, 0x99e25f5f, 0x201e00a3, 0x8ba1ae03, 0x19a9942b, 0x2c1e6198, 0x737f2878, 0xc59a2b1c, 0x7c4f8850, 0xf0409a77, 0x2da9101d, 0xd1aaf46f, 0x291c51af, 0x4d234c8a, 0x68810c4a, 0xfa7e9b84, 0x75b5dcb8, 0x01fe8b9f, 0xd4acb1ef, 0xbb6ff83b, 0x64805089, 0xf6763925, 0xb327edc3, 0xd688dbca, 0x196cf19e, 0x2cf2b856, 0xda34c994, 0x7518948f, 0x3170a8d1, 0x30f86e08, 0x681c6bac, 0x3bfddb0c, 0x5f168b17, 0x4ae631b1, 0x3d105d00, 0x7116b018, 0xad06c9a1, 0x293be0e9, 0x3dd73c5b, 0xbd5a98e8, 0x75536a9f, 0x818d1508, 0x40e9aa2e, 0x68367be3, 0xaf51e5f2, 0x39b8ccc2, 0x6c67fb45, 0xb13771e2, 0xe706549e, 0x42b7fc98, 0xedbd44eb, 0x622d9985, 0x85107f0a, 0xcfc9061b, 0xd9e6d66b, 0xe011bb5e, 0x327eb11e, 0x407ecdd9, 0x3afad29e, 0xab283ffb, 0xbe83d61e, 0xea3e7a46, 0x273b2541, 0xda729e01, 0x81a62472, 0x8359eedb, 0xa4457261, 0x926ad13f, 0xe7e0c4fe, 0x4d1da2d8, 0xa8b3aed0, 0xb658ec23, 0xf7e24a58, 0x5d154e58, 0x9a2bf0ba, 0x2eb455b0, 0x50cb61f3, 0x01e095a2, 0xe1de399f, 0xc71cbd35, 0x7cb90a7e, 0x356e4628, 0x280991f0, 0xd26e64a1, 0xd6c9df5a, 0x3ef127a4, 0xcd2d747b, 0x595f8ee8, 0xba946959, 0xd8ee7838, 0x010f24d0, 0x86b0eaec, 0x1d078587, 0xfbf516ea, 0xdc2a79f8, 0x7015e404, 0xe9efdc2c, 0x16bca751, 0xef7b9df7, 0x9227157f, 0xa31e6f81, 0x83b7e291, 0x7ee5c10b, 0x1a196d7c, 0x23829142, 0x263fe2c2, 0x618461ac, 0xe8681ba5, 0x258b3130, 0xd60cf96a, 0x14fadac6, 0x9d48a438, 0x8bfa6a79, 0x8b920d34, 0x7e98a7fc, 0x088cc57d, 0x5af407f0, 0x15bc4713, 0x23b64b00, 0x37f96d03, 0x5ac72f49, 0x065a9a89, 0x16724cac, 0x503ea2fc, 0x4d548e23, 0x2d92d724, 0xdefe753f, 0x37a36c6a, 0x70708d68, 0xbfe7aba8, 0x1f510c48, 0xb3a995e3, 0x1342df6d, 0x7986ddb9, 0xf4630113, 0xa9f9b81d, 0xb1dca7ae, 0x70915ce7, 0x780e2c35, 0xd1859a18, 0xf92e7d5a, 0xce573f6b, 0x9d79a6e9, 0xbd7f3425, 0xacfd81c7, 0x8429a008, 0xf0da82bb, 0x6a5fb409, 0xc567b073, 0x05371eda, 0xa417042a, 0x9d162579, 0xa8c0eb06, 0xdb7a6342, 0xae0c5575, 0xe0e256a8, 0xa7de78a0, 0x12797538, 0x8e154606, 0x093b23bf, 0x16f4562d, 0x47067058, 0x6db6dc93, 0x33fc94d2, 0xae60549d, 0x4e1b4fcb, 0x7d4aff37, 0x59b050d1, 0xd50b787e, 0x4700ba54, 0xc6848ba2, 0xbd8f6801, 0xd2f62909, 0x289cde1e, 0xc5003778, 0xd973a07d, 0x38a099d8, 0xf8bedbdd, 0x68e9601d, 0x1e86cf6d, 0x2899e89c, 0x229ab04a, 0x45b7a393, 0xef508cd2, 0xa03448ed, 0x29b47253, 0x9f5a48ac, 0x43c89eec, 0x075dfc10, 0x1c2c3184, 0x3ecca1e3, 0x3c5cc91e, 0xcf7cd58b, 0xe014d295, 0xed503a24, 0xe452210e, 0x6fe3ac6f, 0xd99dca00, 0x3ae4ac67, 0xd189afe0, 0xf3ba5df0, 0x84a10e38, 0x4a7b85e4, 0x9d8b4bf7, 0xe83b5b67, 0x90106e3d, 0x534fbf99, 0xce3bcd2e, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithEmptySeed() { // An empty seed is allowed final MersenneTwister rng = new MersenneTwister(new int[0]); // It should be functional so check it returns different values. Assertions.assertNotEquals(rng.nextInt(), rng.nextInt(), "Empty seed creates sequence with same values"); } }
3,037
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/JDKRandomTest.java
/* * 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.rng.core.source32; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Random; import org.apache.commons.rng.RandomProviderState; import org.apache.commons.rng.core.RandomAssert; import org.apache.commons.rng.core.RandomProviderDefaultState; import org.apache.commons.rng.core.util.NumberFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class JDKRandomTest { /** * A class that is Serializable. * It contains member fields so there is something to serialize and malicious * deserialization code. */ static class SerializableTestObject implements Serializable { private static final long serialVersionUID = 1L; private int state0; private double state1; private long state2; private boolean state3; /** * This simulates doing something malicious when deserializing. * * @param input Input stream. * @throws IOException if an error occurs. * @throws ClassNotFoundException if an error occurs. */ private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { Assertions.fail("*** Malicious code ***. This should not be run during the test"); } } @Test void testReferenceCode() { final long refSeed = -1357111213L; final JDKRandom rng = new JDKRandom(refSeed); final Random jdk = new Random(refSeed); // This is a trivial test since "JDKRandom" delegates to "Random". final int numRepeats = 1000; for (int[] r = {0}; r[0] < numRepeats; r[0]++) { Assertions.assertEquals(jdk.nextInt(), rng.nextInt(), () -> r[0] + " nextInt"); } } /** * Test the state can be used to restore a new instance that has not previously had a call * to save the state. */ @Test void testRestoreToNewInstance() { final long seed = 8796746234L; final JDKRandom rng1 = new JDKRandom(seed); final JDKRandom rng2 = new JDKRandom(seed + 1); // Ensure different final int numRepeats = 10; for (int[] r = {0}; r[0] < numRepeats; r[0]++) { Assertions.assertNotEquals(rng1.nextInt(), rng2.nextInt(), () -> r[0] + " nextInt"); } final RandomProviderState state = rng1.saveState(); // This second instance will not know the state size required to write // java.util.Random to serialized form. This is only known when the saveState // method is called. rng2.restoreState(state); // Ensure the same RandomAssert.assertNextIntEquals(numRepeats, rng1, rng2); } /** * Test the deserialization code identifies bad states that do not contain a Random instance. * This test exercises the code that uses a custom deserialization ObjectInputStream. * * @throws IOException Signals that an I/O exception has occurred. */ @Test void testRestoreWithInvalidClass() throws IOException { // Serialize something final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { oos.writeObject(new SerializableTestObject()); } // Compose the size with the state. // This is what is expected by the JDKRandom class. final byte[] state = bos.toByteArray(); final int stateSize = state.length; final byte[] sizeAndState = new byte[4 + stateSize]; System.arraycopy(NumberFactory.makeByteArray(stateSize), 0, sizeAndState, 0, 4); System.arraycopy(state, 0, sizeAndState, 4, stateSize); final RandomProviderDefaultState dummyState = new RandomProviderDefaultState(sizeAndState); final JDKRandom rng = new JDKRandom(13L); Assertions.assertThrows(IllegalStateException.class, () -> rng.restoreState(dummyState)); } }
3,038
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well19937cTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well19937cTest { /** The size of the array seed. */ private static final int SEED_SIZE = 624; @Test void testReferenceCode() { final int[] base = { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0, 0xdc8174cf, 0x3e73a4bc, 0x6fce0775, 0x9e6141fc, 0x5232218a, 0x0fa9e601, 0x0b6fdb4a, 0xf10a0a8c, 0x97829dba, 0xc60b0778, 0x0566db41, 0x620807aa, 0x599b89c9, 0x1a34942b, 0x6baae3da, 0x4ba0b73d }; final int[] seed = new int[624]; for (int i = 0; i < seed.length; ++i) { seed[i] = base[i % base.length] + i; } final Well19937c rng = new Well19937c(seed); final int[] expectedSequence = { 0x7edec319, 0x137f7bdc, 0x37c1c37b, 0xfb0d1dff, 0xc1f16ad5, 0x7c4068a8, 0xc0032722, 0x9bee9252, 0xe0c9f2cb, 0xa5b568ea, 0xd4158686, 0x5b0f0588, 0xc15788dd, 0x3a55d414, 0x187504f3, 0x66fc6017, 0xd959cc8e, 0x25e97379, 0x747e1644, 0x010eeaba, 0xc6dc7c9f, 0xbef3f7ae, 0x6ae035d2, 0x9bd81a26, 0x6ad25d08, 0x38256124, 0x84d90476, 0xb59176cc, 0xc0bf323e, 0x0f9e1c5d, 0x4c1cddd9, 0xf20f2bc0, 0x695a0d04, 0x94000049, 0xe7cb28ff, 0xd3bf53b0, 0xaf8bb19b, 0x0e532354, 0xec339ebf, 0x58bf7827, 0xec325ac8, 0x7cc5aa3d, 0x2837eb14, 0x8cf477fc, 0x0f5335ab, 0x03cd69f2, 0x8aea9f89, 0x4e6e43ad, 0xd0796ae6, 0x51c765ea, 0xa975e2af, 0xb103fbf3, 0xcc1ebb29, 0xb2da48c7, 0x05740a3f, 0x17050574, 0xd608ac32, 0xb6a553e5, 0x5a38d698, 0xf052fef0, 0x47499f92, 0x536193f4, 0x2fe7ca52, 0x97a2fa11, 0xb45f6e9c, 0x4985f38c, 0x594e74ce, 0xc625f3e7, 0x246cdac7, 0xf067ce26, 0x6d521aad, 0x65e3fdec, 0xf709204a, 0x7e65316b, 0xfc441a64, 0x1c8e2f2a, 0x5b80263a, 0x6cae8d07, 0xbcb0698f, 0xbeb89941, 0x7cba00fd, 0x04b2f254, 0xd6c0aecf, 0x622d78dd, 0x91daf1d5, 0x6d91ea98, 0xcd6f73ef, 0xae08d95e, 0x36d9bb0d, 0x5a28e042, 0xd3547654, 0x113787ce, 0xe689290d, 0x28827f84, 0x195fadc8, 0x8429f741, 0x8cff2aa9, 0xd96cdf1d, 0x26c4e246, 0x8d282435, 0xa2bbe3e7, 0x2bc77ef6, 0x0672e7cf, 0x0ead6d17, 0x30484504, 0x745e4f6b, 0x40ecb162, 0x9048d3fd, 0x5523f340, 0xe29de2f0, 0x73f279f2, 0xf400ee3e, 0xd2f82235, 0xdac8031a, 0xd64cdd2d, 0xf93cf1b2, 0x3039c0e3, 0xa54f4589, 0x43bdb22d, 0xe0c9915c, 0x07ec53b1, 0x72d4f7e1, 0xa777e6eb, 0xe1b39d36, 0x02c299ac, 0x9efea144, 0xdcdead5a, 0x916676cc, 0x22d3b419, 0x4bde036a, 0xdc66ccde, 0x37246ed1, 0x41b7d435, 0xd3190503, 0xff6eafee, 0x95fc3949, 0x21e9b23e, 0x2d8ee04e, 0x4b193a5c, 0x88c8851e, 0x4ded8ffd, 0xf6fa0e5e, 0x301e55c0, 0x76d63407, 0xff13710a, 0xd5b652fc, 0x7d752882, 0x52b661db, 0x8d04f089, 0x0d5ddacc, 0xd446e69a, 0xcafbcf9e, 0x8984d559, 0xe5f1b626, 0x3b5c50d9, 0xc31ee252, 0xbea795d4, 0xc38d417d, 0xb251db7f, 0x47a34e74, 0x9206e323, 0x54b9cbef, 0x3a75e8eb, 0x7fc75a73, 0x1caef0d8, 0x6ab98ab9, 0x1393b948, 0x76f65ee7, 0xfc06b913, 0xabdf6bad, 0xf2bb9912, 0x33ac9e6d, 0xed99663a, 0xc1836f53, 0x3b335f02, 0xbc182f96, 0xcc08cf04, 0x44480a49, 0x16c3c484, 0x021bf543, 0x61cf1463, 0xb8e48de4, 0x2e4060c1, 0x236b7beb, 0x5f8edb74, 0xddb89132, 0x789eefd0, 0x9e7803b1, 0xd532c268, 0x9cac6c7d, 0x8014de6c, 0x131bdea2, 0x1120b21a, 0x1b15981b, 0x42534699, 0x6861226b, 0x2e9d8fd6, 0xa7e0aef8, 0xc3f1e570, 0xcc9a1c03, 0x86aeeed0, 0x7e924572, 0xdec310e2, 0x954fcdfd, 0x346e3e41, 0x02a1f1b3, 0x2f27f9b8, 0x526dfc4b, 0x80b40b77, 0x6ccec2ac, 0x37dec1cd, 0x92541501, 0xa4651211, 0xa83a3cc9, 0x5c45690c, 0xa84a250a, 0xff229d49, 0x59fafe96, 0x80befb64, 0xd10fed27, 0x3099205c, 0xec749098, 0x3d4782e1, 0x33abf36f, 0xa177b36a, 0x3336cb98, 0x0dfedbfe, 0x3b842d28, 0x8ee07c01, 0xca4cff3d, 0x46532f70, 0x5b2b6c63, 0x24fbddda, 0x42bf598c, 0x630534bc, 0x7d03dad9, 0xaa51a19a, 0x0fd04003, 0xc54bebf9, 0x4f14a0e0, 0x4c4bea10, 0x288b310d, 0x09491bb6, 0x5c71f942, 0xec94f643, 0xdc5e73af, 0xffe266bc, 0x502f465f, 0x73105451, 0x381d61e8, 0x27e4ba3d, 0x29584901, 0xc4f2ca16, 0x22bf6406, 0x3a2e99c9, 0x8f2bfeed, 0xc08451b3, 0xd58fe12f, 0xd6ef140f, 0xc2d7155d, 0x427ad78b, 0xb22c2143, 0x98aaf5f3, 0xea452758, 0xfeb3f1dc, 0xfdc91816, 0xf06204ae, 0x4da5f8c0, 0x9455d83f, 0xd2476703, 0x2d8b6d09, 0x639bd681, 0x6d883cee, 0xc31b85eb, 0x95ca6161, 0xf22a041d, 0xed7a716d, 0x93a20f80, 0xbcbf3170, 0xd5f1eaff, 0xeadf9724, 0xe0d10439, 0xe6238f2e, 0x07dcb3e5, 0xfc2afa24, 0x16c240a0, 0x0db2c767, 0x6b2b30ae, 0x510ada0e, 0x3a52712b, 0xcb3be546, 0xab99bb22, 0xeabfb9e4, 0xc6b1e8c0, 0x738e42f2, 0x2a38792f, 0x287d4b71, 0x2c61ecd9, 0x4f340c96, 0x4af5475e, 0x21ed8d1a, 0xd3199ce9, 0xa5691a07, 0x783736e4, 0x3f5747be, 0x5d163656, 0x78eaa4b7, 0xaf5ad571, 0x34d50980, 0x576fedd4, 0x633a961b, 0x4ce50eed, 0xfc92dd27, 0xb9e5f2d2, 0x6172d7dc, 0x75cc035b, 0x51e7aa25, 0xd9550b09, 0x3f127b7d, 0xc9d1bfd7, 0xa50462ce, 0x2b4f72af, 0x7b2dc16a, 0x03df87cf, 0xfe70bacf, 0xcb323d1b, 0x936d66c0, 0xbe3673cf, 0x2b8c2aeb, 0xb106e415, 0xd67547f4, 0xb2764ee5, 0x45d4ddca, 0x2f850e32, 0xa0989ade, 0x72eeb982, 0x124ef405, 0xa7a33538, 0x8f932ec8, 0x4dde9770, 0x73f27c3f, 0xb42d15a9, 0xb35970af, 0x85627333, 0x2f637629, 0x4b41467f, 0xb619b967, 0x5508d2e4, 0xd120ec59, 0x7e5fac94, 0x4beafa1c, 0x047021f5, 0xc267a7cb, 0x14340175, 0x02bfc7ce, 0x490f54dd, 0x7e150a3a, 0x286c8609, 0x9f8b3ccb, 0xf9f861b7, 0x9ee7cb36, 0xb6845cb4, 0x1befeeb4, 0x6fd1c7ac, 0x0586d7b2, 0x0de95fef, 0xdeebf834, 0x024b5ff4, 0xe7c64262, 0x7c301fc3, 0xbe065f69, 0xebb0fc83, 0x8166ddfc, 0x0d4d81ef, 0x067df564, 0xc385fd42, 0xca4692e0, 0xcdff7338, 0x8e3f341b, 0x96e2fe05, 0xc8c0c4af, 0x658dc9c9, 0xd832dbc7, 0x64fec5d5, 0x2f2f7caf, 0x414ee5cd, 0x42b5aaf7, 0xab6a13f2, 0x880f5d3e, 0x0b34eb1f, 0x9631f705, 0x8a8367f3, 0x9b616dba, 0x07d61dad, 0xd1ba86c1, 0x5a15f6d4, 0x7c077a68, 0x98a7ef75, 0x76bdc227, 0x42bf3960, 0x3f7fc05c, 0xaf149096, 0x1b98432f, 0x46605986, 0xf3eb5d4e, 0xcacb4b3d, 0x241cd50b, 0x4433f4af, 0x345c46d6, 0xc15558d9, 0x33ff625c, 0x25a1369f, 0xf5b65320, 0xa37d4531, 0x8c64828f, 0x090eecfd, 0x61cd8d5e, 0x1284a410, 0x0209bde2, 0x7aa72267, 0x88861f85, 0x8ffd16c3, 0x22bbd8f2, 0x35adc920, 0xe68689af, 0xb68bda90, 0xe01ac730, 0x4ec9e07e, 0x0422c21c, 0x98ebd14e, 0x548c30f3, 0x3cebe0bb, 0x1d9c8e39, 0xfb2fbd63, 0x8e8a3bf2, 0xbcb72a02, 0x001a001c, 0x76485181, 0x331106c2, 0xcf8bd670, 0x2e38fb73, 0xadfb0c7f, 0x2610b3e8, 0x12baf9d0, 0x3d6e1b9f, 0x26637035, 0x0467fd38, 0x1597cf95, 0x0e19ffc8, 0x573748bb, 0xf2abea20, 0x19f33e4a, 0xd32e4e42, 0xf897b887, 0x8952556c, 0x5e27274b, 0x4a7be189, 0x0964a079, 0xeb6c3655, 0xea2b8aa9, 0xac8dc1ae, 0x6273663f, 0x7520a7a4, 0xbde674bf, 0x455c8311, 0x673c6dd3, 0xa8e70ce1, 0x27eba8a0, 0xe3992185, 0xf03ad983, 0xfe0b4c83, 0x112debec, 0xfe77538a, 0x06520e54, 0x629c12ea, 0xea3c46c1, 0x78122826, 0x86613c06, 0x509060f0, 0xc92a48b3, 0x8a8de595, 0xfd2e28dc, 0x0d4136c1, 0xea566c5b, 0xbeedb674, 0x7c9d545b, 0xb7379ebf, 0x98e7a5da, 0xb620278c, 0x3f79c6f6, 0xf9b726d9, 0xe6fc4389, 0x0473db65, 0xff3bba4d, 0xe24d016c, 0x5d64fb35, 0x905ec12d, 0x5f2e9216, 0x1cf64a72, 0x1bf7d07d, 0xcb715b1f, 0x4df41208, 0x9e13c159, 0x52c37c45, 0x16a32dec, 0x414ecd38, 0x321d4bbc, 0x734c4436, 0x6e58440e, 0xaef95d11, 0x07cce4ea, 0xed5126a1, 0xd871e390, 0x3d7eb569, 0xdbbbe3ed, 0xf3c5d9a5, 0x91c36995, 0x75a2a625, 0x3fb3b4b0, 0xab5282a7, 0x46db12c1, 0x38dd91a9, 0xc4aa9202, 0x93a1fa9c, 0x8c88a4e9, 0xeca7e496, 0xa55d6ce3, 0x222559d5, 0xdeeef730, 0x3ead7cd9, 0xb5c8bb34, 0xc544c260, 0x13a107ce, 0x6a913618, 0xfdbf2ad8, 0x97230e9f, 0xa8c4dff7, 0x91084281, 0x4a9c79ce, 0x0f508c83, 0xd190d6ec, 0x85d11a18, 0x4add635d, 0x4932f289, 0xa399e5c9, 0xdb8fffd3, 0x46fa1f63, 0x7a0bf590, 0xdda9c298, 0xa4a2aabd, 0xb107a3be, 0xf67ab868, 0xdae50206, 0xbf9be351, 0xf1ee6a34, 0x70f7207f, 0xe78e0257, 0x8c1e8fd3, 0xd75cd8e3, 0x4807878b, 0x228bee66, 0x4db82643, 0xf7523656, 0xf8287153, 0x8db87ff9, 0x354cac8f, 0xf58733ea, 0xa17f7bed, 0x278f20b0, 0xbc0bf059, 0xd9c043b6, 0x5f223f88, 0x2580cb7d, 0x3a3305d6, 0xa264d169, 0x1f58f5c0, 0x355951f8, 0xe1f34129, 0x5f8504bc, 0xb6811335, 0x94cd73ab, 0xa741d4d9, 0x3eedf6e3, 0x0f9bb6e7, 0x70037c6f, 0x1d19f627, 0x71e747d3, 0x8d97a695, 0x16146769, 0x4b69e106, 0xb2ea0bb4, 0xa6613f5d, 0x7bcbf0db, 0xadc48b26, 0x44f7ed7b, 0x76487961, 0x8be65c2b, 0x65adaa3c, 0x56a449ec, 0xb677ffac, 0x5836265d, 0xcc8241bf, 0xcc9894f2, 0xd4167eb5, 0x380befb6, 0xa0def654, 0xb9f89a1f, 0xdb4ccc8c, 0xcb3fdefb, 0x94e82e16, 0x6deba24f, 0xf4bcf81b, 0x7d9a1c63, 0x9c2218ec, 0xc3d4a196, 0xccc8adbc, 0xe6423bad, 0xff6d9cb7, 0x9001f125, 0xf9c00ee1, 0xa47e535e, 0xfbde542f, 0x22f82e52, 0xb2fa0fd4, 0x522e0c10, 0xee01c885, 0xecf7e7e5, 0x63f8215b, 0x116e7113, 0xb324b06d, 0x6a947310, 0x71b3c33d, 0x574db9bb, 0xae3894ca, 0x8a0d69c3, 0x965d7648, 0x742d4770, 0x6c6167c7, 0xbc2af085, 0x4a7d548c, 0xa81752ef, 0x90a8618f, 0xad792cd7, 0x2abcac40, 0x38dfc8ad, 0x8eb7bd70, 0x2c0429f5, 0xf6864304, 0x67495b70, 0x73657de6, 0xbc72610c, 0xba12e5b7, 0x55697d48, 0x32d231b1, 0x673d35b3, 0x92d121a8, 0x4beeaf0b, 0x1b24e6d1, 0x76848372, 0x0658f61c, 0x1e2a5a78, 0xdb9c0b89, 0x65363d88, 0x4e2fad94, 0x75643b24, 0x8f7f9e81, 0x602cb6a1, 0x35d5be4e, 0xd4050f93, 0x136920e0, 0xb672a2d0, 0x58f06280, 0x77cc0d9d, 0xea0cc233, 0x410c812d, 0x7bbee557, 0xc6b2512f, 0x6d6d42f8, 0x12d998c3, 0xcd232a71, 0x81504d3f, 0x309975b0, 0xd064afa5, 0xf692e014, 0x292c8f42, 0xe05d017c, 0x47bcd24c, 0x600d518e, 0x9cf8dc52, 0xfcd47806, 0x7ebc9254, 0xf22891eb, 0x5cf3de69, 0x10e65bda, 0x47b0aae2, 0x378f642c, 0x40871560, 0xde04e735, 0x18a5a62f, 0xc4b11717, 0xc49af5b4, 0xfff7c080, 0xd3900171, 0xd58dacd3, 0xc49dbeb9, 0xdcaac902, 0x70a4af34, 0x4e5ba48b, 0xe87ff6d2, 0xd754acdf, 0x1b34d507, 0x670b7caf, 0xe86140c2, 0xfd69f505, 0x722f8595, 0xcca56c00, 0xbfb42ec4, 0xde89b25f, 0x423f9a5c, 0x03cb2150, 0x9b0996b3, 0xc647d8c4, 0xc13a3be4, 0x2a3f6a3e, 0x70448244, 0x16413317, 0xf6ad5dd1, 0xa0d7995c, 0x91eab041, 0x34f860b3, 0xc2de16b9, 0x9cab91e9, 0xeb8d3965, 0x85454198, 0x263c89bc, 0x98075f64, 0x6244249f, 0x67ec4fb2, 0x78754956, 0x09c6a9a3, 0x3b09f4e6, 0x65b5b924, 0xb64cefde, 0xeb4f6d1c, 0x6de3a15b, 0x314eeb71, 0x11e991ad, 0xb48ac1d2, 0xdf61abb7, 0xdd3f2416, 0x89100beb, 0x291482fb, 0xfa729f5f, 0x31caa8c0, 0x65b51953, 0xa047de5c, 0x0bfa540f, 0x19b1f7b8, 0xef74910c, 0xc8a86ac7, 0xdf15c2eb, 0x08ab20d5, 0x4349f2f3, 0x63d7ee22, 0x730ef44a, 0x4878641d, 0xa9b4c629, 0x2734df6a, 0x70681724, 0x9ee3d02c, 0xcc35ad39, 0xb501da60, 0x6cf734a3, 0xcb518339, 0x1ba711d5, 0xeb4bb07a, 0x581a4528, 0x2fc0fd40, 0xa67a9505, 0x3ca34fc4, 0x9d6232ae, 0x076402a2, 0x5eb2f08a, 0x2f283ebb, 0xa5373549, 0x144a431e, 0xe2686576, 0xb91672db, 0xff8f328c, 0x79e6d632, 0xb5c5383f, 0x31c1d79e, 0xafb83193, 0x793f92ae, 0x2bee63a2, 0x3592a67b, 0x763389bf, 0x92015d60, 0x00924224, 0xb9a158bd, 0x633f6b3e, 0x74329dae, 0xf36aa400, 0x449f9207, 0x80e99a4d, 0x366d33a8, 0x83b87bd8, 0x54a16838, 0xbc603f96, 0xfcca8f1b, 0xf1486745, 0x1beaf2a2, 0x972079e5, 0x43c07483, 0x4de76d88, 0x86a088d9, 0xafc425e8, 0x12c29942, 0x9bade757, 0xc78b9bb4, 0xf2055c60, 0x82a1aa6b, 0xef5ae0c2, 0xffe95a1f, 0xc12df407, 0x6b5c3697, 0x381f9756, 0xcbe5ffd1, 0xd7e4e45d, 0x2d6f88ad, 0xa6e47c25, 0x1d010c37, 0xca7592bb, 0x99f7129b, 0xf35465a8, 0x7d8e6639, 0x9ffd595a, 0x509de716, 0xce107f7e, 0x5d7a1591, 0xdca73f0b, 0xbbad72d2, 0xa0e0cdd5, 0xd9ce71de, 0x42060b17, 0x5c375f25, 0x8d0f2c01, 0x8558e0fb, 0x25d79b7e, 0x13a7ee4a, 0x3827dac6, 0xcc7a37b8, 0x1dea23ec, 0x5b9eeaec, 0xb9a1106c, 0x42f0773c, 0x410598db, 0x1922101e, 0x792ed235, 0x65fa8731, 0xbd5bc8fc, 0x046e0d7f, 0x0f568443, 0xb87a5ba7, 0x0c6f614c, 0xe5a267b9, 0x302aeb8f, 0x458c5e43, 0x462df72d, 0x915d5116, 0xa8b05c54, 0xfc62efe5, 0xb224a7a2, 0xfa91da46, 0xaa7d6a0a, 0x20b789a9, 0x0239f034, 0x581cbd1f, 0xb5314846, 0x0ecf5a13, 0xf9c347db, 0x90d3c58f, 0x548aaa17, 0x9d020ae5, 0x8119cd74, 0x0937343e, 0x107c35f4, 0x730ea08a, 0x0ab30092, 0xc89c15f9, 0xfda60285, 0xade29d38, 0x55f81711, 0x43cd0b96, 0xe63dc843, 0xc54d1d3d, 0x2c4acb0c, 0xbd4f0a28, 0xca1c2b19, 0xb41d5405, 0x8fa83a6a, 0xd0209cdb, 0x4ac06696, 0xe322e653, 0x12eee7fc, 0x18184ffd, 0xb6ab3464, 0x42d7e76e, 0xfed40cdd, 0x26eb7cf1, 0xc117c59c, 0xe40c6141, 0xa6fa7f07, 0xa9b701be, 0xae5c58b7, 0xa59469dc, 0xf74e7485, 0x5a7af0fe, 0x5ed7b77a, 0xa80cc8e6, 0xc9c6be0f, 0x7d5c0680, 0xe7c0420d, 0x8165f25a, 0x946ca3cd, 0xaf15ff66, 0x21d0a50d, 0x1861945c, 0x21d804fc, 0x683fc3ea, 0x6911942a, 0x967aff93, 0x4bc411ec, 0x1be874d4, 0x38f83c15, 0x71f647e1, 0x6fd64938, 0xd5312075, 0xb3c5c7a9, 0x52fc8882, 0xf8d1045f, 0x32c07581, 0x0e9b3d22, 0xfe02d431, 0xef051cf0, 0xe36ae0e0, 0xf357d93d, 0xf8ba290f, 0xa8e45921, 0x4f558f8a, 0x6a9c3399, 0x67a79778, 0x56359069, 0x89a8779f, 0x28dc5622, 0xad5caf9d, 0x518d9809, 0x788204a6, 0x5f2853bd, 0x99ae3972, 0x88214ef7, 0x0a3e9f01, 0xed6e7113, 0x28ff2dd4, 0x167892d1, 0xc1159f83, 0xa6a6585e, 0x939ce279, 0xf8cbc44e, 0x0205172b, 0xad4c49e7, 0xdfb8bea4, 0x454bd571, 0xb0a436d6, 0x73238978, 0xcd51e8d0, 0x27917ada, 0x5b951147, 0xecc08e55, 0xadbab7b5, 0x0f91729a, 0xf3c9dd44, 0x8288a531, 0x34ae0598, 0x094971cc, 0xa94eea2d, 0x4ee14e30, 0x9ae16080, 0x5c60135d, 0x3cafbe3f, 0xb0dcaa89, 0xa1ad4300, 0x3c6b38a1, 0x937f826e, 0x6354c0e3, 0x53cfb25c, 0xf44903d6, 0x4a6a2603, 0xb577e6ff, 0x31da1546, 0x0a96ad06, 0x8d240123, 0x7fe78131, 0x291c2be0, 0xdc70841f, 0x14c2613f, 0x7242fe48, 0x1cb0ec08, 0xebc0c66e, 0x48ada81f, 0x2ad97b0f, 0x8857a7c4, 0x912f1372, 0x79b8f601, 0xfc88c020, 0x2c40dff6, 0x7314fa94, 0x2e8a8d6f, 0x2a9ee539, 0xaceafc39, 0xff83a201, 0xdc42b76d, 0xdff9707f, 0xb7ba790c, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well19937c(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitInPoolIsFunctional(Well19937c.class, 19937); } }
3,039
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/MultiplyWithCarry256Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class MultiplyWithCarry256Test { /** The size of the array seed. */ private static final int SEED_SIZE = 257; @Test void testMarsaglia() { final int[] seed = { 0x000587c4, // initial carry 0xff710353, 0x1b427020, 0xc9c59991, 0x96e511e0, 0xf1d06013, 0xe0216c68, 0x98999e3d, 0xce158f68, 0xb8d320ef, 0x905ddbf0, 0xda9717c9, 0x78498c30, 0x4681a0ab, 0x781347a8, 0x62eafcb5, 0x0a9bdc68, 0x6229d227, 0x74066600, 0x8bed09c1, 0xed6e6c00, 0xdd94abc3, 0x250f7008, 0x035bdded, 0x156c4108, 0x0f74e6ff, 0xa6921870, 0x81da1839, 0x7779efb0, 0xfecbd75b, 0x5af100c8, 0xa40fd3e5, 0x02a06008, 0x310f4db7, 0xaabaa6e0, 0xfb7c2331, 0x73b1aa60, 0x7ce8d2f3, 0x0217d868, 0x4f98355d, 0x435302e8, 0x1f47130f, 0x3e1c62f0, 0x69ac8aa9, 0xd8da50b0, 0xebe8414b, 0x39059928, 0x497a4c55, 0x049ae8e8, 0x4b0e81c7, 0xdb024540, 0x05aed2a1, 0xd29e4e40, 0x8d4aad23, 0xcf594e48, 0x30490d8d, 0xc36989c8, 0x6104809f, 0x97a6af70, 0xcd2f1b19, 0x0bf3d7b0, 0x0099c97b, 0xbe921008, 0xda423c85, 0x3604eec8, 0x92516757, 0xfdefd9e0, 0xe8adc411, 0x6313a060, 0x7fa3e4d3, 0x28314068, 0xa0f0807d, 0xd0a9d668, 0x008045af, 0xc25245f0, 0x2691e189, 0x9ff794b0, 0x25869f6b, 0xd347d828, 0xd4666475, 0x74179e68, 0x69c57a67, 0x1ccfc380, 0x7ac37381, 0xa9325780, 0x6ab03683, 0xfcaeeb88, 0xb892adad, 0x62b41888, 0xf28a1fbf, 0x27122d70, 0x16c8a0f9, 0xb6543630, 0x01cb9c1b, 0x052baec8, 0x277db425, 0x19cb1c88, 0x96061ff7, 0x9d030ae0, 0x10cbd8f1, 0x6a334960, 0x0501d233, 0x1b7f7568, 0xff5da59d, 0x5eadf9e8, 0x26ec97cf, 0xe208d2f0, 0xe934e169, 0xb7bd6930, 0xc1dc400b, 0xdc45f6a8, 0xe89ab515, 0x710932e8, 0xcac46e07, 0x44b63440, 0xc8e42c61, 0xde16e540, 0x96e107e3, 0x565bfd48, 0x5738cf4d, 0x70a49bc8, 0x85136f5f, 0xaab3e670, 0x4eb1efd9, 0xbc5bb630, 0x2f30423b, 0x4a1e8f88, 0xe8d40ec5, 0xbe50f9c8, 0xe7284f97, 0x15c447e0, 0xf2c741d1, 0x30f93360, 0xc04c1c13, 0xcbf14d68, 0xcfe94abd, 0x31a9d268, 0x7e17986f, 0x5e877ff0, 0xb1998449, 0xf7f8ed30, 0xdca7b22b, 0xf55427a8, 0xe153b735, 0xc2530968, 0x0586f4a7, 0xdb458d00, 0xb11dbd41, 0xc6fb9b00, 0x6fd5e143, 0x61b28708, 0xd013196d, 0x10b75a08, 0x00f40c7f, 0x40db3370, 0x126992b9, 0x793f35b0, 0xdb6d62db, 0x255836c8, 0x19d12e65, 0x3821a508, 0x9d468137, 0x373a0ce0, 0xe81672b1, 0x9c18e560, 0x56f7fe73, 0xfb2c4768, 0x05a599dd, 0x461c6ee8, 0x1f93d48f, 0x5ac9a8f0, 0x0a82c329, 0x0d28c0b0, 0xbfae26cb, 0x47dba628, 0xf6ec80d5, 0xdf6548e8, 0x67487247, 0xa1110e40, 0x718f7621, 0xbed4b940, 0xc8d2b3a3, 0x25d50d48, 0x29e6110d, 0x5ac1a1c8, 0x1aca0b1f, 0xb968e470, 0x3e051999, 0x4edb95b0, 0x9de098fb, 0x757ff708, 0x39054205, 0xfc9190c8, 0x3594dad7, 0xc5ffd9e0, 0x9baeeb91, 0xd5285f60, 0xab207853, 0xe4805f68, 0x50a9befd, 0x7d6f7768, 0xf8fec52f, 0xc8bdf5f0, 0xbeb6c609, 0xc00f94b0, 0xb13438eb, 0x911af728, 0x99d4b2f5, 0x5f7b7f68, 0xe27118e7, 0x8f6a1780, 0x07f36701, 0xfd3f3f80, 0xc88fce03, 0x4c7fcb88, 0x0038712d, 0xc7b12488, 0xd88e773f, 0xac980870, 0x57124b79, 0x87709b30, 0xbc032b9b, 0x2d2356c8, 0x9050c0a5, 0xcb59de88, 0xba506177, 0x0b157ee0, 0x2f9ebc71, 0x09974060, 0xb308d7b3, 0xfbfa7c68, 0x587a6c1d, 0xea8397e8, 0x6b187d4f, 0xdcbbfef0, 0xb42825e9, 0x8a4b3230, 0x1f528b8b, 0x41e03ea8, 0xa8d73195, 0x5e1b94e8, 0x7e990087, 0x92526740, 0xd1747fe1, 0xbb8a2c40, 0xd062bc63, 0x4eec1048, 0xaa5c52cd, 0x5875e7c8, 0x11ace7df, 0x919ee970, 0x0f2e2259, 0x55c10b30, 0xd60ae1bb, 0x2ab27788, 0xcda47a45, 0x667acdc8, }; /* * Data generated from code snippet provided here: * http://school.anhb.uwa.edu.au/personalpages/kwessen/shared/Marsaglia03.html */ final int[] expectedSequence = { 0x257c6890, 0x2a638c7d, 0x24e92547, 0x3f021e24, 0x59cd3f37, 0x92c71967, 0x6d55aab2, 0xa662f2ad, 0x8633e15a, 0x19d38d42, 0xbbe8381e, 0x8312f976, 0xee4b2e57, 0xbe88c5d5, 0xb291b54e, 0x531ab614, 0xb6627311, 0x8ca090fd, 0x27e62e4a, 0x57b6879b, 0xce50ecc1, 0x41cf8cc8, 0x5a12c57a, 0xf0d9051d, 0x6cd3bdda, 0xa41671c6, 0x642bd28c, 0x1cce0718, 0x31dfe0b6, 0xd3733244, 0xa1c1b352, 0x54d4408b, 0xa8f4eba7, 0x02e1397c, 0xeac4aad4, 0x46a63278, 0x98d27010, 0x4036a1f1, 0xe902f641, 0x26b14aa6, 0xaeafe31c, 0x66a327ad, 0x3683346f, 0x661f8efe, 0x997da2b7, 0x5f6bb2e9, 0x0d5b7307, 0x5a70c604, 0xef321a16, 0xdd3260c6, 0xd23a8c99, 0xbaf20620, 0xeda3454a, 0x80e40950, 0xaf7dca32, 0xc80c8c58, 0x513bf771, 0xb8754759, 0xf41dbf72, 0x3f8c1c68, 0x26a6e60e, 0x7044afb5, 0xda8b1683, 0xee1b21dd, 0x814c5f27, 0xcc0dd2ea, 0x61da4c4c, 0x5851b922, 0x3d042fee, 0xb4840125, 0x294f93e7, 0x086be5e9, 0x5aa1c3fb, 0x9fa95c59, 0xf6b2daba, 0xb1c9204d, 0x4baf6c0d, 0x9cc31c15, 0x2257fb1b, 0xd6a9c727, 0x2c71d6b1, 0x6fd0d553, 0x30d58655, 0x5370f15f, 0xb926edc9, 0x8835266c, 0x389d4d27, 0xe763f62e, 0x0b97f8a9, 0x3a731321, 0xf8c581b7, 0x71b7c8bf, 0x3904345a, 0x1bcaad61, 0xcc3abf3f, 0x91a26d23, 0x1f8e89ee, 0x8833e2c1, 0x04c11554, 0x39136f32, 0xd2b9856c, 0x9d00cc24, 0x4d760374, 0xc65ae148, 0xad1a9df0, 0xde1dd1c7, 0xdadb106c, 0x7b081b19, 0x6e65ee75, 0x009c5298, 0xb8b82940, 0xd6181fc3, 0x3692c0b2, 0xa03d1e1f, 0xb35a1df5, 0x784bdfc0, 0xa431fe7f, 0x00c6d1ad, 0x7ef0f3ef, 0x68945b04, 0x8dcc4d02, 0x2d334d53, 0xe7751bb6, 0x911f4622, 0x703e8049, 0xed071832, 0xc4c0b6f2, 0x9449ba2d, 0x6537639f, 0x47d7b18e, 0x92a6650f, 0x8d42c84b, 0x4f2ce769, 0xe1d0247c, 0xc7fba06b, 0x198c07d9, 0xb81fd816, 0xcab28386, 0x17a2b884, 0xdbf55e34, 0x134179f7, 0x0088523b, 0x808fd3b8, 0x5736a030, 0x061abf09, 0xdffd5db5, 0x2858f517, 0x2f3709b5, 0xc56e87f0, 0xa29aa85b, 0xb64befc8, 0x89649761, 0x3b5d0058, 0xb026a416, 0xe9deb08c, 0x4c99a0fb, 0xee323b43, 0x3234a297, 0xfc7d8a82, 0xb5941989, 0x4a61ddbf, 0xadcac556, 0x8b7a2731, 0x5b12c8ad, 0xb3b327c5, 0x88cb626c, 0xc14ad815, 0xf3054fd7, 0x0faf1cbc, 0x3b1db6d4, 0x01e6e03e, 0x58c214ce, 0x35819aee, 0x238aa2aa, 0x62d94d21, 0xb0ba531e, 0xc2e4d6f2, 0x6f01d233, 0xd82461e2, 0x1ccfd5b2, 0xa516f619, 0x717ca8f1, 0x16c2200a, 0x8694cb14, 0x39212bc5, 0x76a261ba, 0xbe6cafdf, 0x1ab30291, 0x61aeca24, 0xff55bc4c, 0x4d71915c, 0x2ad797e7, 0x83e01a94, 0xc2d3ac81, 0x47a0ea0e, 0xa8137f15, 0x48e12d0e, 0xe654097d, 0x436bee53, 0xc8dfe9b3, 0xe7b51a7c, 0xdba4ea69, 0x0e5d71ca, 0xaad571d6, 0xe8f3f5c4, 0x60c3fb0b, 0x72bac665, 0x844e1404, 0x516839de, 0x7039fd09, 0xa089303c, 0xc42555ab, 0xc02120b8, 0x31d9a671, 0x7da2f597, 0x0047e938, 0xf3365c4a, 0x8a2c6c60, 0xff6e5799, 0x2c5f7a74, 0x9d3cfe7b, 0xc8c57f3d, 0x1d3473f6, 0xe75bcc43, 0xdd33ba4f, 0x90668ad4, 0xf67ef996, 0x300fe9a4, 0x584059ef, 0x8fbe8104, 0x3e259db6, 0x9df0b7fb, 0x52746195, 0x6a417ef5, 0x3ff9ad21, 0x8102516f, 0xda054624, 0x2477abf6, 0x1d8e5443, 0xdaaa5259, 0x07d015e3, 0x27d68c1b, 0x930689a9, 0x075ba657, 0x915f2f8a, 0xc1911283, 0x391af175, 0xfcd2a3bc, 0xe566bb6a, 0x5f6b4191, 0xd4b80bd2, 0x686cc681, 0x46eb852b, 0x8a6cba82, 0x873d26fe, 0x7fd36f7b, 0x638c020e, 0xe547311d, 0x280e508d, 0x88c9cc68, 0xc9cb9d3a, 0x50256a56, 0xd7a2e984, 0x1322e7d2, 0x016952e5, 0xadaee9e3, 0x040ad740, 0xe7b210ae, 0x659e8e59, 0x6cf0b4be, 0x367c7bf5, 0xa98bb1f8, 0xaf8492cd, 0x793b43cc, 0xc2255740, 0xdb3efd85, 0x351b4ade, 0xa3386b57, 0xa53856e2, 0xb0a74272, 0xffde7652, 0x3bcf325d, 0xc12b43d6, 0xaf84cfcb, 0x6bdac7e8, 0x15ff5fe2, 0x724c469d, 0xe19896ea, 0x43cd6aa6, 0x2b7260db, 0x041c1d52, 0xd5ec2c45, 0xa1e12fc9, 0x6be494a0, 0xc64f6ef0, 0x26eea794, 0xdfe7e36c, 0xc7d53723, 0xaca00dd0, 0x4affdf5d, 0xbd9f0df6, 0x75382815, 0x25c57824, 0x31ba4039, 0x6c75480c, 0xa0fa2bfa, 0xbba48f53, 0x09db90e9, 0x1dc1dc19, 0xe4663531, 0xdf3d2ed7, 0x6f54e741, 0xe6082c01, 0x2baabe7e, 0x67b2086c, 0x95d8ec2b, 0x7b4d87af, 0x43e3aae5, 0x1787dfba, 0xcc2eb97a, 0x3428f0df, 0x1e406776, 0x485ac033, 0xccee0de0, 0xa7010382, 0x0f96c5ed, 0x8c80257b, 0x0d983da2, 0xa7fcb482, 0xe5d0f841, 0x72b4b4fe, 0x18e06be1, 0xd98f371d, 0x040f8016, 0x7ee5b27f, 0x189be27f, 0x179ddbba, 0x107b6e08, 0x3ea4ee95, 0x54d8d162, 0xff8b29fe, 0x562d0834, 0x87f37d17, 0x88391d07, 0x5812f518, 0xf34be33f, 0xa9c4f645, 0x1c89446b, 0xf2111482, 0xf9ee61f8, 0x129603f8, 0x3e2e086a, 0xecd0a435, 0x9840d54c, 0xf9664d4b, 0xce6730f8, 0xbc9ce06b, 0x0aa150d7, 0x5bbe5e90, 0xe2564486, 0x66105cce, 0x7f38b7f3, 0xe63d1130, 0x62ac16ee, 0x77c57e00, 0x3b0226a1, 0x72ec3aea, 0x36019afc, 0xc645ff0d, 0x974f4880, 0x91da1212, 0xe370db8b, 0xadcf17e4, 0x944ab002, 0x5b0d716c, 0xbc01086d, 0x27211776, 0x652a32d4, 0x16be38e1, 0x6f2ea0bd, 0xd8858d93, 0x216a458c, 0x78bae9e0, 0xad14557e, 0xbba176e8, 0x45c0d59d, 0xa6ec2833, 0x9a72408d, 0x68582f7b, 0x1631f9f8, 0x215abad0, 0x17be213c, 0x05746888, 0x9779a0df, 0x0d9d28cf, 0x1d253cd7, 0xd06c9962, 0x00a84316, 0xc7a7ebce, 0xa4f1cb0f, 0x08028d29, 0xaebf1b50, 0x5782537b, 0xd4d7e06b, 0x846524e3, 0x9b82ab20, 0x49288ba5, 0x58734936, 0xc17aa021, 0xa8fd173d, 0xc20fbc6e, 0x8bbd9a5a, 0x7da87b4e, 0xafc23110, 0xdc727d0a, 0x5ede2442, 0xfd185051, 0x18dc56ec, 0x4a0a814c, 0x906f8016, 0xda39909b, 0xd664ebf5, 0x9e58b3fd, 0x09769997, 0x4656559e, 0x25980db9, 0x99ad5e86, 0xdda9fff3, 0x4b3b5c51, 0x85f75ff2, 0xe2712e34, 0x2d3dcaa7, 0xcdbe3226, 0x8b709d9a, 0x6f6a35cf, 0x5dada053, 0xc81e3023, 0xa2d20dc8, 0xedb116c2, 0xaca74c89, 0xeedeb314, 0xb865935f, 0xb9e99d2a, 0x23b1517b, 0x97cdd346, 0x4a14886e, 0x28195367, 0xaeed6a06, 0x902e40ec, 0x4f9d50a4, 0xfcad4859, 0x8ec8d46b, 0x950f385d, 0xae293d56, 0x8eba6d86, 0xf4c7b9f0, 0xa7e44d23, 0x340f66d9, 0xe04962b1, 0x3a6cb3b1, 0x51f0f544, 0x2102ade1, 0xdb72bac5, 0x7d754363, 0x9c3d6466, 0xceaa6494, 0x55431cfc, 0xc6905603, 0x6f0726ba, 0xb08a516e, 0x1c501439, 0x1323c172, 0x2bcdef90, 0xf8bf1c92, 0x4b6db4b4, 0x3d4e76f2, 0x823060d9, 0x80886870, 0x2e44e197, 0xb0f6a034, 0x409d98d2, 0x52f8c0de, 0xd30cdebf, 0x179757c5, 0xf5bb5dad, 0x49c600bb, 0x712f9419, 0xe5aa8c01, 0x6694ccc6, 0x6cad53a2, 0x4db659ae, 0x41d7b9c5, 0x26f5c897, 0xfe3df467, 0x8ed323bf, 0xbf8127b2, 0x81d40377, 0x00e447b3, 0xf1ca1e7d, 0xb4ae053c, 0xd815cd65, 0xd522eaf4, 0x0d300f47, 0x23204bf1, 0x3e86d3ed, 0x269c4485, 0x18bae574, 0x63f7157f, 0xeb56b507, 0x0e4e9f1d, 0xc162a93e, 0xa3f08b8b, 0xb595246c, 0xd02701fe, 0x13a054c5, 0x2cd29d84, 0x66fc748d, 0x23029126, 0x022bd7bc, 0xff04526e, 0xac70e3d3, 0x60dfbb69, 0x6beffb09, 0xae396478, 0x23507c03, 0x6d508bbd, 0xdd4b9a03, 0xefd11007, 0xde7026de, 0x68129e59, 0x0ab76704, 0x16f918de, 0xacc2b388, 0xf5f666fc, 0x1b519c64, 0x2cc36ca7, 0xe9dc07d6, 0x4a670a68, 0x6c797bb1, 0x813b77d8, 0x0decd048, 0x6fe1157d, 0x6725a946, 0xb131c2dc, 0x0dc96308, 0x4ed6b9d7, 0xc3cd7a9f, 0x886b41b6, 0x3d886073, 0x414b66d2, 0x0c8db580, 0x6e11a51e, 0x647dfd2e, 0x57df7d76, 0x4d679969, 0x36a61424, 0x34b45092, 0x6f0aa0a3, 0xfe52c2e4, 0x10aa6aee, 0xb1bb73ab, 0x30ddbfde, 0x22898b9d, 0x21ba51b2, 0x927e526c, 0xe1a0a535, 0xd0355862, 0xb296051b, 0x2258e82a, 0x2f017185, 0x4e037f71, 0x455e9f7b, 0x70be2aaa, 0x14f7dd2d, 0x8fe5a857, 0x888251af, 0x41887950, 0xb1cd8c85, 0xcf88077a, 0x658834ed, 0x6de3ae38, 0xb49fa2b4, 0xbba5c792, 0xf382f47f, 0xa829d21f, 0x35f31d13, 0xc645243f, 0xde1ee296, 0x20cb92b5, 0xcf66d510, 0x05e80309, 0x5f9f1ed0, 0x0b5b7f0e, 0x6a53938f, 0x5e0abcb5, 0x73ff6593, 0x004d4dca, 0x39997c7b, 0x23a78298, 0xc7c08cb3, 0xff1ee291, 0xea40f316, 0x69132b3e, 0x164b6b39, 0x0ebc2fb1, 0xe5adc964, 0xb234bed6, 0xc8bb950c, 0xd4cbdbfc, 0xdcc3f925, 0x99772a38, 0xe7cc4d82, 0xaa5747a6, 0x7bf358a9, 0x32f37e61, 0xb1ee2868, 0x1b432ed6, 0x8458ebc1, 0xbf116582, 0x3715233e, 0xf3917de9, 0x0e96120f, 0x105ed566, 0x359abd45, 0x8e892c78, 0x6ea63f81, 0xce1e1d0d, 0x89732bc3, 0xdb135bf7, 0x07622b9a, 0x467b58f1, 0x71ec958f, 0xe2e235d6, 0x7bbb202e, 0x9c854a05, 0x0f89d960, 0xbd09bcd1, 0x482a48cf, 0xb05afb69, 0x913bf292, 0x489b5bd6, 0x8d68812b, 0xf8a3720a, 0x0c87aef4, 0x76823918, 0x05e3a35a, 0xcf0486e7, 0x53c02103, 0xde9dd286, 0xf54f6fdb, 0xefa7877b, 0xb4661141, 0xf9d2581c, 0x4bbca497, 0xcbd2d029, 0xfab736a2, 0x7f90c973, 0x64d0e7cc, 0xb1882fa3, 0xec5b23eb, 0xf388dbb6, 0x352b7f43, 0x4a0021b3, 0xb25f8a02, 0xccc29b12, 0xa3573f70, 0x13c86bd1, 0xb9a1ea5b, 0x04bbff46, 0x234ca740, 0x64d36324, 0xc1c7b698, 0xd0b3b0c0, 0x27470071, 0x86b6c003, 0x55daab1d, 0x3ae11383, 0x9ddde0b4, 0x4cbfa4e3, 0xc45b229f, 0xe62999ce, 0xcf388be2, 0x76cf1184, 0x016c2c30, 0xafd34574, 0xb98a148f, 0x05277b87, 0x5562a329, 0x366be4b5, 0xcaa8ac90, 0x23339579, 0x31ad226a, 0x8a72f170, 0x3945e5e9, 0x34238130, 0xf5647741, 0x74b2be0a, 0x3168c90d, 0x1f7e75b1, 0x3001deaf, 0xf23a0992, 0x10ac859d, 0xc980d2af, 0xbffeda39, 0x7254660b, 0xb0fa2a45, 0xa72b1d49, 0x06919c1f, 0x33ae4774, 0x3b35137a, 0xb13998d9, 0x08df0a6a, 0x3e09ef58, 0xe1c0da12, 0x004520c8, 0x0e3e1a83, 0x75be6d84, 0xdef828b4, 0x02ba1195, 0xc0723e50, 0x2b576c84, 0x32a75f93, 0xc787fa9d, 0x072eaeac, 0x4c28c3fe, 0x0c96e328, 0xc32ac0a4, 0x5b39b458, 0x1362cf03, 0x42b7d8e3, 0x95098036, 0xcbdd6d82, 0x4b177650, 0x7b170cec, 0xf009e03b, 0x1d6b3ce6, 0x598bc6ed, 0x6d83457e, 0x9b9d7632, 0x371eb318, 0x0a0b504b, 0x8a5fc450, 0x342cddb7, 0x27abdd43, 0x1e5619a1, 0xe1b0d98a, 0x060538a1, 0x7e4c3af6, 0x88327248, 0x3973708b, 0x98461e31, 0xe986a6b0, 0x5773b681, 0xb62f0d41, 0x6700f403, 0xa4b1d0e1, 0x80b4d98b, 0x2c4132ee, 0xa658d3b3, 0xba1b770e, 0x60904cfd, 0xc445a139, 0xdb0ef27a, 0x4ace46cf, 0x2f8ac09e, 0x9dd71d29, 0x0fed2a2c, 0x29df377a, 0x79d096da, 0xd5b3c519, 0xcc6b5755, 0xd4b4d312, 0x811fb101, 0xcae0d28a, 0x674d1b64, 0x19e985cf, 0xcb2d22e6, 0x2785cedc, 0x518f9239, 0x1a66e5a1, 0xc25a529c, 0x8ddf8919, 0x557e2d1a, 0x601958fa, 0x2cec958b, 0x2c4a1996, 0xceebe93a, 0x15583c2f, 0x16915c85, 0x38625bb7, 0xc40c45b6, 0x695f4ca6, 0x3300a3c0, 0x5a10cc45, 0x1285f17e, 0x86d60d0e, 0x61894067, 0x98ef1dd5, 0x66d410df, 0x25896c66, 0xf33d63ce, 0x9c28db0b, 0x8f50b049, 0x3d5fbf07, 0x898c4ab2, 0x16ecf2b9, 0x391cc660, 0xc83caf84, 0xf839ad80, 0xab64b874, 0x220839a9, 0x43aa4e89, 0xbd89da32, 0xa43b21ed, 0x1c01bfb6, 0xbcda7042, 0x00beab05, 0x93fa2926, 0xf7f38a29, 0xcba3e372, 0x3646218d, 0xbede27dd, 0xe5ee44a1, 0x01e0eb66, 0x9982c956, 0x08f74ba4, 0x908297b2, 0x7b8c6ad9, 0x2ff6d71b, 0x5593404d, 0xcdf14fe5, 0x3ea33f7c, 0x2640eb50, 0xcb2fcd6f, 0x4a2586b1, 0x63f225e8, 0x1a106462, 0xc7e0f101, 0xb86af8bc, 0x78f359e2, 0x784f946c, 0x90add600, 0x24c61b14, 0x6445fd9d, 0xcd0e503e, 0x2b28f79d, 0x51b832bb, 0xb42e3783, 0x62b5de9f, 0x150cfa4c, 0x30d9624d, 0x7b722b66, 0xd2f64f21, 0x9ab71b02, 0x4973968d, 0xac292292, 0x7752d5e3, 0x5ae44d00, 0x453fb388, 0x582db40a, 0x4e70c468, 0x87d5b7cf, 0x6fba662f, 0x5fc7b4c7, 0xec7e3a56, 0x75bd65f5, 0xcddcd8bd, 0xb7cee4ca, 0x9f808f92, 0x6eef126b, 0x87e5710f, 0x7b27b3c3, 0x1d8b7f60, 0x36e849f8, 0xbfc46814, 0x5ab00b3b, 0x12e30b30, 0x67b46983, 0x6cbe95ea, 0x498b8bcf, 0x553caec8, 0x48dd6ee0, 0xd4896ff7, 0x42242b53, 0xa5346f85, 0xabbd2373, 0xc00c2318, 0xc6f283f3, 0x89fd13d3, 0x5a4d8bfc, 0x94125136, 0xedeacd7d, 0x741b5462, 0x1b40489f, 0x25aceb45, 0xdf0538c4, 0x58af3fa8, 0x78118eaa, 0xc15e746f, 0xe890d26f, 0x826645b6, 0x315f7310, 0xf0ce725b, 0x837f8cd0, 0xaf55bdd8, 0xa7f19807, 0x605b6830, 0x7393a6c7, 0x1726f278, 0xc9f01a15, 0x3e6d7040, 0x5ce86985, 0xeb0ac581, 0xaea9aee0, 0x52fc0e08, 0x7ec22c7e, 0x5173aedd, 0x6add6fca, 0x0ac5043b, 0xa5ff3c54, 0x907c747c, 0xd9ada8bb, 0xf2fa68f9, 0x1521e21b, 0x4e32b234, 0xf96826ce, 0x4c89a35f, 0x628be219, 0x9dfeb826, 0xb5b8475f, 0x74dec36b, 0x8a3e92ba, 0xf619f494, 0xe3ffe1d6, 0x5ad677ff, 0x3a2678d2, 0x5f562dba, 0xd7509de3, 0xea134b0d, 0x16df70e0, 0xa5de499f, 0xf61461bd, 0xbadcb3c5, 0xef72eaaa, 0xc22faf5c, 0x248875f7, 0x250f026a, 0xcf929df9, 0xc2374a6a, 0xfc4ec03d, 0x38d46c25, 0x0a3d64ea, 0x6d82727b, 0x1c15f45a, 0x88cd0860, 0x424ef845, 0x953ad237, 0xf6d9487c, 0x83d1c69a, 0x9e1133d5, 0x553bd331, 0x168bdfac, 0x307d610b, 0x813c7fab, 0xbb9f9a78, 0x80c2d8a9, 0xad830f8f, 0x75c24862, 0xb29f399c, 0xee3a4a5d, 0x031a7f0d, 0x10727cb0, 0x08a85558, 0x5f94fb2b, 0xc064ee4d, 0x88ba1026, 0xcc3366b7, 0x76ecfdb2, 0x78ad049a, 0xcdb88483, 0x4419b7b7, 0x5706ae34, 0xcb3b2f6d, 0x0182cbaf, 0x9f52c655, 0x6d1029da, 0xe7bf2f32, 0xfb8dbe3a, 0x55e30359, 0x5534ed84, 0x690b0d1c, 0xeada16ac, 0xc1f43f00, 0x07af318c, 0xb5d2f6a3, 0xf3786fa8, 0x3f086f0a, 0xef283067, 0xb9a86d48, 0x6c619d98, }; RandomAssert.assertEquals(expectedSequence, new MultiplyWithCarry256(seed)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new MultiplyWithCarry256(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(MultiplyWithCarry256.class, SEED_SIZE); } }
3,040
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/PcgXshRs32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class PcgXshRs32Test { @Test void testReferenceCode() { /* * Tested with respect to pcg_engines::setseq_xsh_rs_64_32(x, y) from the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0xba4138b8, 0xd329a393, 0x75d68d3f, 0xbb7572ca, 0x7a48d2f2, 0xcb3c1e37, 0xc1374a97, 0x7c2c5bfa, 0x8a1c8695, 0x30db4fea, 0x95f9a901, 0x72ebfa48, 0x6a284dbf, 0x0ef11286, 0x37330e11, 0xfeb53893, 0x77e3adda, 0x64dc86bd, 0xc8d762d7, 0xbf3fb80c, 0x732dfd12, 0x6088e86d, 0xbc4e79e5, 0x56ece5b1, 0xe706ac72, 0xee798018, 0xef73de74, 0x3de1f966, 0x7a36db53, 0x1e921eb2, 0x55e35484, 0x2577c6f2, 0x0a006e21, 0x8cb811b7, 0x5f26c916, 0x3990837f, 0x15f2983d, 0x546ccb4a, 0x4eda8716, 0xb8666a25, }; RandomAssert.assertEquals(expectedSequence, new PcgXshRs32(new long[] { 0x012de1babb3c4104L, 0xc8161b4202294965L })); } @Test void testReferenceCodeFixedIncrement() { /* * Tested with respect to pcg_engines::setseq_xsh_rs_64_32(x) from the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0x5ab2ddd9, 0x215c476c, 0x83c34b11, 0xe2c5e213, 0x37979624, 0x303cf5b5, 0xbf2a146e, 0xb0692351, 0x49b00de3, 0xd9ded67c, 0x298e2bb9, 0xa20d2287, 0xa067cd33, 0x5c10d395, 0x1f8d8bd5, 0x4306b6bc, 0x97a3e50b, 0x992e0604, 0x8a982b33, 0x4baa6604, 0xefd995eb, 0x0f341c29, 0x080bce32, 0xb22b3de2, 0x5fbf47ff, 0x7fc928bf, 0x075a5871, 0x174a0c48, 0x72458b67, 0xa869a8c1, 0x64857577, 0xed28377c, 0x3ce86b48, 0xa855af8b, 0x6a051d88, 0x23b06c33, 0xb3e4afc1, 0xa848c3e4, 0x79f969a6, 0x670e2acb, }; RandomAssert.assertEquals(expectedSequence, new PcgXshRs32(0x012de1babb3c4104L)); } }
3,041
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/LXMSupportTest.java
/* * 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.rng.core.source32; import java.util.SplittableRandom; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; /** * Tests for {@link LXMSupport}. * * <p>Note: The LXM generators require the LCG component to be advanced in a single * jump operation by half the period 2<sup>k/2</sup>, where {@code k} is the LCG * state size. This is performed in a single step using coefficients {@code m'} and * {@code c'}. * * <p>This class contains a generic algorithm to compute an arbitrary * LCG update for any power of 2. This can be bootstrap tested using small powers of * 2 by manual LCG iteration. Small powers can then be used to verify increasingly * large powers. The main {@link LXMSupport} class contains precomputed coefficients * {@code m'} and a precursor to {@code c'} for a jump of 2<sup>k/2</sup> * assuming the multiplier {@code m} used in the LXM generators. The coefficient * {@code c'} is computed by multiplying by the generator additive constant. The output * coefficients have the following properties: * <ul> * <li>The multiplier {@code m'} is constant. * <li>The lower half of the multiplier {@code m'} is 1. * <li>The lower half of the additive parameter {@code c'} is 0. * <li>The upper half of the additive parameter {@code c'} is odd when {@code c} is odd. * </ul> */ class LXMSupportTest { /** A mask to clear the lower 5 bits of an integer. */ private static final int CLEAR_LOWER_5 = -1 << 5; @Test void testLea32() { // Code generated using the reference c code provided by Guy Steele. // Note: A java implementation requires using the unsigned shift '>>>' operator. // // uint32_t lea32(uint32_t z) { // z = (z ^ (z >> 16)); // z *= 0xd36d884b; // z = (z ^ (z >> 16)); // z *= 0xd36d884b; // return z ^ (z >> 16); // } final int[] expected = { 0x4fe04eac, 0x7bc5cb6c, 0x29af7e05, 0xf80de147, 0xb90bc13a, 0x6fbce371, 0x3dbbfab0, 0xcf366cd9, 0x90c9c2a2, 0x988ea20f, 0x75fc2207, 0x58197217, 0xdbc584a5, 0x5d232d06, 0xd4faec13, 0xfa1fb8fd, 0xea45a9d4, 0xb2bdb43c, 0x0502b325, 0x2ebca83c, 0x3337cf53, 0x5531f920, 0x3edf02c5, 0xa9b79cf7, 0x80ff2c21, 0xaba7498f, 0xa7dd9739, 0xb85c77ea, 0x4e846134, 0x99461d4e, 0x87c027b1, 0xc8c37ec7, 0x457301bc, 0xa8d33a18, 0x87420fcc, 0x1c5a5b6d, 0x44b0e3ff, 0x45459875, 0x99d6ef70, 0x12e06019, }; int state = 0x012de1ba; final int increment = 0xc8161b42; for (final int e : expected) { Assertions.assertEquals(e, LXMSupport.lea32(state += increment)); } } @ParameterizedTest @CsvSource({ "235642368, 72987979, 792134597", // LXM 32-bit multiplier: // -1380669139 == 0xadb4a92d "-1380669139, 617328132, 236746283", "-1380669139, -789374989, -180293891", "-1380669139, 67236828, 236784628", "-1380669139, -13421542, 42", }) void testLcgAdvancePow2(int m, int c, int state) { // Bootstrap the first powers int s = state; for (int i = 0; i < 1; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 0), "2^0 cycles"); for (int i = 0; i < 1; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 1), "2^1 cycles"); for (int i = 0; i < 2; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 2), "2^2 cycles"); for (int i = 0; i < 4; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 3), "2^3 cycles"); // Larger powers should align for (int n = 3; n < 31; n++) { final int n1 = n + 1; Assertions.assertEquals( lcgAdvancePow2(lcgAdvancePow2(state, m, c, n), m, c, n), lcgAdvancePow2(state, m, c, n1), () -> "2^" + n1 + " cycles"); } // Larger/negative powers are ignored for (final int i : new int[] {32, 67868, Integer.MAX_VALUE, Integer.MIN_VALUE, -26762, -2, -1}) { final int n = i; Assertions.assertEquals(state, lcgAdvancePow2(state, m, c, n), () -> "2^" + n + " cycles"); } } @Test void testLcg32Advance2Pow16Constants() { // Computing with a addition of 1 will compute: // m^(2^16) // product {m^(2^i) + 1} for i in [0, 15] final int[] out = new int[2]; lcgAdvancePow2(LXMSupport.M32, 1, 16, out); Assertions.assertEquals(LXMSupport.M32P, out[0], "m'"); Assertions.assertEquals(LXMSupport.C32P, out[1], "c'"); // Check the special values of the low half Assertions.assertEquals(1, out[0] & 0xffff, "low m'"); Assertions.assertEquals(0, out[1] & 0xffff, "low c'"); } /** * Test the precomputed 32-bit LCG advance constant in {@link LXMSupport} can be used * to compute the same jump as the bootstrap tested generic version in this class. */ @Test void testLcgAdvance2Pow16() { final SplittableRandom r = new SplittableRandom(); final int[] out = new int[2]; for (int i = 0; i < 2000; i++) { // Must be odd final int c = r.nextInt() | 1; lcgAdvancePow2(LXMSupport.M32, c, 16, out); final int a = out[1]; // Test assumptions Assertions.assertEquals(1, (a >>> 16) & 0x1, "High half c' should be odd"); Assertions.assertEquals(0, a & 0xffff, "Low half c' should be 0"); // This value can be computed from the constant Assertions.assertEquals(a, LXMSupport.C32P * c); } } /** * Compute the multiplier {@code m'} and addition {@code c'} to advance the state of a * 32-bit Linear Congruential Generator (LCG) a number of consecutive steps: * * <pre> * s = m' * s + c' * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the accumulated multiplier and addition for the * given number of steps expressed as a power of 2. Provides support to advance for * 2<sup>k</sup> for {@code k in [0, 31)}. Any power {@code >= 32} is ignored as this * would wrap the generator to the same point. Negative powers are ignored but do not * throw an exception. * * <p>Based on the algorithm from: * * <blockquote>Brown, F.B. (1994) Random number generation with arbitrary strides, * Transactions of the American Nuclear Society 71.</blockquote> * * @param m Multiplier * @param c Constant * @param k Number of advance steps as a power of 2 (range [0, 31]) * @param out Output result [m', c'] * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ private static void lcgAdvancePow2(int m, int c, int k, int[] out) { // If any bits above the first 5 are set then this would wrap the generator to // the same point as multiples of period (2^32). // It also identifies negative powers to ignore. if ((k & CLEAR_LOWER_5) != 0) { // m'=1, c'=0 out[0] = 1; out[1] = 0; return; } int mp = m; int a = c; for (int i = k; i != 0; i--) { // Update the multiplier and constant for the next power of 2 a = (mp + 1) * a; mp *= mp; } out[0] = mp; out[1] = a; } /** * Compute the advanced state of a 32-bit Linear Congruential Generator (LCG). The * base generator advance step is: * * <pre> * s = m * s + c * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the update coefficients and applies them to the * given state. * * <p>This method is used for testing only. For arbitrary jumps an efficient implementation * would inline the computation of the update coefficients; or for repeat jumps of the same * size pre-compute the coefficients once. * * <p>This is package-private for use in {@link AbstractLXMTest} to provide jump functionality * to a composite LXM generator. * * @param s State * @param m Multiplier * @param c Constant * @param k Number of advance steps as a power of 2 (range [0, 31]) * @return the new state * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ static int lcgAdvancePow2(int s, int m, int c, int k) { final int[] out = new int[2]; lcgAdvancePow2(m, c, k, out); final int mp = out[0]; final int ap = out[1]; return mp * s + ap; } }
3,042
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/PcgMcgXshRr32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class PcgMcgXshRr32Test { @Test void testReferenceCode() { /* * Tested with respect to pcg_engines::mcg_xsh_rr_64_32 of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0x25bc3e38, 0xb0693d58, 0x155b98f0, 0x047e13d7, 0xcfb227b3, 0x66601632, 0x71c6e68b, 0x16e2d4a7, 0x65412358, 0x6d39102c, 0x545cebed, 0x577695ef, 0xc851c202, 0x743d50b6, 0xe1876a24, 0x274ae9e1, 0x4087af7b, 0xd4738e89, 0x6ae6e6cf, 0xf8716a43, 0x933ed380, 0x3edb0d15, 0xa3716e23, 0x2d5f81f2, 0x5a921ac5, 0x795ec1cf, 0x42595831, 0x55a39a40, 0x9a21afda, 0xc03fa331, 0x9192fd98, 0x87eb7041, 0x2d9e338e, 0xf924d873, 0xf8c6a7a7, 0x2dfe78bf, 0xd443c0a9, 0xe567f8ed, 0xa4e09491, 0x3c91d8fd, }; RandomAssert.assertEquals(expectedSequence, new PcgMcgXshRr32(0x012de1babb3c4104L)); } }
3,043
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/MiddleSquareWeylSequenceTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.apache.commons.rng.core.util.NumberFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class MiddleSquareWeylSequenceTest { /** The size of the array seed. */ private static final int SEED_SIZE = 3; @Test void testReferenceCode() { /* * The data was generated using the following program based on the author's C code: * https://mswsrng.wixsite.com/rand * * #include <stdio.h> * #include <stdint.h> * * uint64_t x, w, s; * * inline static uint32_t msws() { * x *= x; x += (w += s); return x = (x>>32) | (x<<32); * } * * int main() { * x = 0x012de1babb3c4104; * w = 0xc8161b4202294965; * s = 0xb5ad4eceda1ce2a9; * for (int i=0; i<10; i++) { * for (int j=0; j<4; j++) { * printf("0x%08x, ", msws()); * } * printf("\n"); * } * } */ final long[] seed = {0x012de1babb3c4104L, 0xc8161b4202294965L, 0xb5ad4eceda1ce2a9L}; final int[] expectedSequence = { 0xe7f4010b, 0x37bdb1e7, 0x05d8934f, 0x22970c75, 0xe7432a9f, 0xd157c60f, 0x26e9b5ae, 0x3dd91250, 0x8dbf85f1, 0x99e3aa17, 0xcb90322b, 0x29a007e2, 0x25a431fb, 0xcc612768, 0x510db5cd, 0xeb0aec2f, 0x05f88c18, 0xcdb79066, 0x5222c513, 0x9075045c, 0xf11a0e0e, 0x0106ab1d, 0xe2546700, 0xdf0a7656, 0x170e7908, 0x17a7b775, 0x98d69720, 0x74da3b78, 0x410ea18e, 0x4f708277, 0x471853e8, 0xa2cd2587, 0x16238d96, 0x57653154, 0x7ecbf9c8, 0xc5dd75bf, 0x32ed82a2, 0x4700e664, 0xb0ad77c9, 0xfb87df7b, }; RandomAssert.assertEquals(expectedSequence, new MiddleSquareWeylSequence(seed)); } /** * Test the self-seeding functionality outputs non-zero output from the initial state. * * <p>Note: The generator quality is dependent on a high complexity Weyl increment. This * test passes a seed without the increment set and expects the generator to work. The * statistical quality of the output is not tested.</p> */ @Test void testSelfSeeding() { final int warmupCycles = 0; final int testCycles = 3; // Do not pass the Weyl increment (the 3rd value in the seed array) RandomAssert.assertNextLongNonZeroOutput(new MiddleSquareWeylSequence(new long[2]), warmupCycles, testCycles); } /** * Test nextLong() returns two nextInt() values joined together. This tests the custom * nextLong() routine in the implementation that overrides the default. */ @Test void testNextLong() { final long[] seed = {0x012de1babb3c4104L, 0xc8161b4202294965L, 0xb5ad4eceda1ce2a9L}; final MiddleSquareWeylSequence rng1 = new MiddleSquareWeylSequence(seed); final MiddleSquareWeylSequence rng2 = new MiddleSquareWeylSequence(seed); for (int i = 0; i < 50; i++) { Assertions.assertEquals(NumberFactory.makeLong(rng1.nextInt(), rng1.nextInt()), rng2.nextLong()); } } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new MiddleSquareWeylSequence(new long[SEED_SIZE]), 2 * SEED_SIZE); } }
3,044
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/JenkinsSmallFast32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class JenkinsSmallFast32Test { @Test void testReferenceCode() { /* * Tested with respect to the original reference: * See : https://burtleburtle.net/bob/rand/smallprng.html */ final int[] expectedSequence = { 0x3b05df0d, 0xc1b222b1, 0xdc38504a, 0x5a929fee, 0x695f52ee, 0x49246926, 0xeaca3aaa, 0xb7ea1598, 0x6f946a66, 0xf4eddf53, 0x4235b7bf, 0x4b1eb3c6, 0xffa13fa2, 0x095ab9fc, 0x64dc8c5c, 0x3ad18ba8, 0xb5f8354d, 0x744ef6de, 0xff9d2943, 0xb3d54756, 0x096e9c74, 0x142a29c5, 0xcf090298, 0x71823d63, 0x587052d2, 0xb843e5ed, 0x670e0279, 0xc5bb26d5, 0xc28d61e0, 0xd31aedaf, 0x52fe2b77, 0x65f50ec7, 0x522a44c5, 0x25f4baf8, 0x9fd1d806, 0x3a24f3bc, 0x78f2aac1, 0xce496e14, 0x74d186b8, 0x34ff8809, }; RandomAssert.assertEquals(expectedSequence, new JenkinsSmallFast32(0xb5ad4ece)); } }
3,045
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/IntProviderTest.java
/* * 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.rng.core.source32; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; /** * The tests the caching of calls to {@link IntProvider#nextInt()} are used as * the source for {@link IntProvider#nextInt()} and * {@link IntProvider#nextBoolean()}. */ class IntProviderTest { /** * A simple class to flip the bits in a number as the source for * {@link IntProvider#next()}. */ static final class FlipIntProvider extends IntProvider { /** The value. */ private int value; /** * @param value the value */ FlipIntProvider(int value) { // Flip the bits so the first call to next() returns to the same state this.value = ~value; } @Override public int next() { // Flip the bits value = ~value; return value; } } /** * This test ensures that the call to {@link IntProvider#nextBoolean()} returns * each of the bits from a call to {@link IntProvider#nextInt()}. * * <p>The order should be from the least-significant bit. */ @Test void testNextBoolean() { for (int i = 0; i < Integer.SIZE; i++) { // Set only a single bit in the source final int value = 1 << i; final IntProvider provider = new FlipIntProvider(value); // Test the result for a single pass over the long for (int j = 0; j < Integer.SIZE; j++) { final boolean expected = i == j; final int index = j; Assertions.assertEquals(expected, provider.nextBoolean(), () -> "Pass 1, bit " + index); } // The second pass should use the opposite bits for (int j = 0; j < Integer.SIZE; j++) { final boolean expected = i != j; final int index = j; Assertions.assertEquals(expected, provider.nextBoolean(), () -> "Pass 2, bit " + index); } } } @ParameterizedTest @CsvSource({ // OK "10, 0, 10", "10, 5, 5", "10, 9, 1", // Allowed edge cases "0, 0, 0", "10, 10, 0", // Bad "10, 0, 11", "10, 10, 1", "10, 10, 2147483647", "10, 0, -1", "10, 5, -1", }) void testNextBytesIndices(int size, int start, int len) { final FlipIntProvider rng = new FlipIntProvider(999); final byte[] bytes = new byte[size]; // Be consistent with System.arraycopy try { System.arraycopy(bytes, start, bytes, start, len); } catch (IndexOutOfBoundsException ex) { // nextBytes should throw under the same conditions. // Note: This is not ArrayIndexOutOfBoundException to be // future compatible with Objects.checkFromIndexSize. Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.nextBytes(bytes, start, len)); return; } rng.nextBytes(bytes, start, len); } }
3,046
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well44497bTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well44497bTest { /** The size of the array seed. */ private static final int SEED_SIZE = 1391; @Test void testReferenceCode() { final int[] base = { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0, 0xdc8174cf, 0x3e73a4bc, 0x6fce0775, 0x9e6141fc, 0x5232218a, 0x0fa9e601, 0x0b6fdb4a, 0xf10a0a8c, 0x97829dba, 0xc60b0778, 0x0566db41, 0x620807aa, 0x599b89c9, 0x1a34942b, 0x6baae3da, 0x4ba0b73d }; final int[] seed = new int[1391]; for (int i = 0; i < seed.length; ++i) { seed[i] = base[i % base.length] + i; } final Well44497b rng = new Well44497b(seed); final int[] expectedSequence = { 0xf9eb8c4a, 0xb5388f7f, 0x31db15ce, 0x7436250c, 0x26ebff2e, 0x5e23be0e, 0x29b3d9da, 0xf79c8147, 0x4565a1d2, 0x1907aeb2, 0x15d9658f, 0x2e86830d, 0x6f4e5a56, 0xeb1ae997, 0xc6852d4e, 0xb11d1a69, 0x0b6a1884, 0x17c35178, 0x5d84b05f, 0x3be7e73f, 0x0bdd5123, 0x01da4da8, 0x0473cc3c, 0xafe51580, 0xb8238478, 0x9e5eb067, 0x1590d61b, 0x9a4f09cd, 0xe7f58193, 0x63509a7e, 0x86b4ef58, 0x49bcb046, 0x92bad8e9, 0x5e6dd096, 0x4d3a3ab1, 0x9776638f, 0x4802d849, 0x3772ee29, 0xd444615a, 0x0b7d2c7f, 0x64e0efd4, 0x66f41964, 0x7e89b0e3, 0xcfc74119, 0xc4de0b6c, 0xca9f2618, 0xe700e201, 0x9298d076, 0x7c8d36eb, 0x6224fb16, 0xb4d563bb, 0xc411da30, 0x60898d17, 0xaa4e15d2, 0x608a7efa, 0x4ea45741, 0x93b35791, 0xf70be8d4, 0xe164e214, 0x711e5ef4, 0x9104bb18, 0x96b6324c, 0x79465ac2, 0x22e86d22, 0x6f48696f, 0x298a7eb4, 0xa46ee147, 0xe095c5a9, 0xa3538ee4, 0xc094c30a, 0x3d1532b7, 0x6a38643c, 0x8fb03976, 0x07b0d0b0, 0x551c6bd0, 0xbb33f741, 0xad8edb14, 0xa1cafba2, 0x8e54bb70, 0xf9c8855f, 0x95d96c98, 0x8360b095, 0x9551aca2, 0xa5509bf7, 0x491cd2e9, 0x431ec7b4, 0x81cfb46e, 0xe534073b, 0xf2f2964f, 0xd68aae52, 0x0224244e, 0x529e9de9, 0xfa55eff7, 0xf5f27f81, 0x1a071e01, 0xbc2cc04c, 0x8619bb1b, 0xc2de2157, 0x07efa053, 0x03d40321, 0x8ac2d84e, 0x29c590ab, 0x1481ec1e, 0xc9b8a4ef, 0xf3511719, 0x2c21029b, 0xb45c2897, 0x5a773c5f, 0xd86970ae, 0xd73f48b8, 0xfbb61984, 0x052929af, 0xb6592e9c, 0xaaf1e6ff, 0xf611e92f, 0x0cc447d8, 0xaa599d24, 0x0defe09b, 0x2a5fe45c, 0x8c64d61f, 0xe627ad10, 0xbfde76f6, 0xa3a16956, 0xb1de5a73, 0xd9d125a6, 0xd676b448, 0xb393c232, 0xdf6d844f, 0xa5987910, 0xe15fe210, 0xe4ab6f8d, 0x2a415d07, 0xe39700e8, 0x8254c003, 0xffd43332, 0xa8f8643b, 0x1a05cc23, 0xc77054a6, 0x70bc1814, 0x73e263e1, 0x3929b37c, 0xb47a85ba, 0x9af6d890, 0x6e7df50f, 0x5f08fce8, 0x4bb42d77, 0x43ecb1ca, 0x6d85070b, 0xd5afa3e1, 0xe4babbfa, 0xcb1fa6df, 0xf197e418, 0x8f73f5e5, 0xd333dd04, 0x42e4a2d2, 0x472ee369, 0x3895ef2c, 0x35377394, 0xd0d70ff6, 0xf6fcd325, 0xccc8283d, 0x8a0a3f3e, 0x1349caaf, 0xc3148637, 0x9474a696, 0xe9011d75, 0x8130918e, 0xd50fdea1, 0xb1919549, 0x3b9570cf, 0x13f85595, 0x98b73d48, 0x92b862dd, 0x1fee5d99, 0xb363077b, 0x47ed233a, 0xbbf445ba, 0x2d49923d, 0xa0a97f5e, 0x8979923d, 0x3efb7cdf, 0xcd2ee58e, 0x3ef90ea9, 0x92d2a8d7, 0x2cf3b42b, 0xb2721c9a, 0x370209be, 0xa66f20f9, 0xc7635366, 0xc1c7ce0a, 0xbc3ba48e, 0x9dac078d, 0x500908c4, 0x64e31a83, 0xf81d9494, 0xbdf4ad5a, 0xff24f3a7, 0x9ff42f32, 0x6274df89, 0x344f1d7f, 0x50d55f1f, 0xd2c0ab2b, 0xb27c189f, 0xe7448a8a, 0xf3fdab8b, 0x74bf8873, 0x5a335504, 0xacbdf5d4, 0x50a09a94, 0x4419430d, 0x03283fde, 0xea3c5eab, 0x9f7f319d, 0xae6e21ca, 0x57a9ded1, 0xab4fd711, 0xcdb2f6aa, 0x9be3cb21, 0x5646407f, 0x0dfaf9c8, 0xa6261b2d, 0xaa92936d, 0xd76a7170, 0xe25bb9c7, 0x3348cc42, 0x0fb2cf79, 0x5b9dd48a, 0xdcb1655c, 0x5ea4b6bd, 0x68b468b5, 0x83f19147, 0xa3ca57e9, 0x2b23e0db, 0xcb51fc89, 0x713ddc59, 0xf00045d0, 0x3f190d73, 0x634c3ff5, 0x9ed2f9fc, 0xdb5bd9bb, 0x35205c55, 0x51988784, 0xba7d65af, 0x11a98540, 0x8e3a785a, 0x819d97d8, 0x4a3c82fd, 0x700a1b17, 0x9a51500c, 0x3cb31e05, 0x99c703f1, 0xbeab2cf2, 0x97b2c682, 0x7f02c7db, 0x12fe2dc6, 0x210e67e7, 0xe2d898e0, 0x94239d8f, 0x9acc05f8, 0xc7243c78, 0x87f56697, 0xf7203249, 0x6ca4cdfc, 0xa89a02c0, 0x0876bf6a, 0x7405a170, 0x4edbf02e, 0x33858c05, 0x8058c835, 0x5a09f5f1, 0x5786229a, 0x4c1e1469, 0xae748079, 0x0ac95262, 0x7f9acf59, 0xd6fcdb25, 0x30dbd3f8, 0xf6cc41ec, 0xb97d453d, 0x4aefb871, 0x530181b8, 0x5150bdab, 0x9697d51f, 0x13488e54, 0xdee71ef4, 0x1c438318, 0xb9bb635f, 0x809ab956, 0x6189ce01, 0x5362d94c, 0x4c183a61, 0x3c65f9a6, 0xc705d9d7, 0xa460f64d, 0xf5527572, 0x146fa8e2, 0x45e668c7, 0x2be30c16, 0xbd8faf41, 0x92d055bb, 0x8fcd9081, 0xe599a689, 0xb81672a0, 0x939b5df5, 0x01823c37, 0x7d2ffd8a, 0xa2da0e99, 0x690e6eaf, 0x918a8009, 0xbb4c867d, 0x87986510, 0xcfbee8ca, 0x32945f83, 0xe0107f7d, 0x6c567265, 0x50185a20, 0xdc1aa2a4, 0x67b53d54, 0xce4d5c8e, 0x1ddff0d9, 0xd47b7f07, 0x4895e595, 0xd75cf72d, 0xdb865d06, 0x3a5102ad, 0xa5974c1b, 0x3a12b2ec, 0xad2b6260, 0x7ca3a3c3, 0xd4a2ca86, 0x7e1fff27, 0x1035696e, 0x248d560b, 0x82cdfbe0, 0x0b533ee3, 0x9ede15e6, 0xaec3f979, 0x0a6fb9cf, 0xc94a5dbd, 0x884ba5eb, 0xe80ac741, 0x1e0dc4a8, 0x8ac934ef, 0x003ea808, 0x09d41ade, 0x0d802909, 0x2717b5a9, 0xdb9c1232, 0x0b4a740e, 0x3ef0f592, 0x010c99d4, 0x233a50f1, 0x433abb45, 0xa3d02843, 0x76dcb0ad, 0xd4d68be4, 0xd7d4ce74, 0x9cc21578, 0xdb8365d7, 0xd0ba35de, 0x8e779d5a, 0x2d278682, 0x835a35ab, 0x8170a266, 0xa57a6f52, 0xd024df91, 0xf2b496df, 0xa60b2ebd, 0x4742bda5, 0x7beefd43, 0xca1e835f, 0x95cbe131, 0x6d5f1f3f, 0xc6fc7b2e, 0xbb4d7869, 0xbd7a634a, 0xeb35b0bc, 0xacd4f3bb, 0xfa87e05b, 0xfedc52eb, 0x72daa0ed, 0xc3743841, 0x2089d702, 0x606e8e8f, 0x2abbfe91, 0x724477b7, 0xc5886d85, 0xf35343f1, 0x9e908621, 0x91425228, 0xa8d7b8bb, 0x4bebe2a2, 0x3f335406, 0xea725ab8, 0x33bb9a4d, 0x1ae4f982, 0x182fb6b8, 0x45740c3f, 0x6fe4453d, 0x7d5afa9b, 0x62027f3f, 0x4aaf3d89, 0x74ebb382, 0xa49eba50, 0x6ab830d8, 0xfdb8b73a, 0xb21ca957, 0x2a89d682, 0xa47474a2, 0x522449ef, 0xebd7b2b4, 0x62357202, 0xe38a72c7, 0x0c20e4a5, 0x8bc912de, 0xe1ffda73, 0xd9ee22f3, 0x1315a7c5, 0xaa6ec3ed, 0x36168979, 0xfb63343b, 0x20079960, 0xec0301a1, 0x8e08401c, 0x20a70ea6, 0xf28e61fd, 0x40efc57e, 0x1ba0b289, 0xf1aff0ec, 0xfae15799, 0x7a610931, 0xeedeb38d, 0x36f2c12b, 0xc85e0450, 0xb9beeb3d, 0xb86b10e6, 0xd7e9607f, 0x223cfd6e, 0x55ef1d97, 0x93d906f2, 0x09e12b58, 0xf5749c13, 0xba3413c0, 0x8171be86, 0xaa68d02b, 0x5ee1c306, 0xcb38ad98, 0xd0be1b39, 0x510e0cd6, 0x3de49e48, 0x872a8aac, 0x28f73c21, 0xf62276ee, 0x52eb3650, 0xad52d305, 0x2f5d9ae4, 0xcfe34233, 0x27ce23a2, 0xdf7e17cd, 0xae42926a, 0xd6ee353a, 0x59b182a5, 0xd320ca21, 0x8d8e657f, 0x8fa71adf, 0xeb1ac850, 0xf43faf71, 0xf52ad236, 0xd06923b3, 0xc968229c, 0x6abbc365, 0xea4dd803, 0x72a06c99, 0xb0f053c2, 0x9f604a7d, 0x88f1781b, 0x6b2d1aef, 0x85a00592, 0xf6786d58, 0xb34d2f70, 0x3e5f5a3b, 0x6d329107, 0xe8ceed25, 0x0445d819, 0xfb77f254, 0x7219738f, 0x6e846109, 0x33fc1282, 0x963df7b0, 0xddb7aaeb, 0x8d902ab4, 0x7a47607d, 0xb9e4feaa, 0xcb55a7eb, 0xb550041a, 0xb1e23ab4, 0x0ea1863a, 0x959dbdf8, 0xb153ba8b, 0x52188b39, 0x5151c6a1, 0xe7a3ecf3, 0x087bd5c2, 0x8ba1b86d, 0x9a7c5b58, 0x12da9436, 0x7907d11f, 0x1d201142, 0x95b51a86, 0x7e2db64d, 0xe67816cc, 0xfd706251, 0xe39d5dbe, 0xec9e4159, 0x9b3939c5, 0x32d49111, 0x019435be, 0x0514d509, 0xae0b88e6, 0xe9925cbf, 0x3a91e3f0, 0x2378ceb0, 0x166f75de, 0x5dd91d58, 0xa6045910, 0x8ead5a6b, 0xfc7b70a0, 0x155f4b0b, 0x145d6d8e, 0x0affe016, 0xdf29283a, 0x8eebe24c, 0x5f1d4441, 0xb6522f48, 0x780fbede, 0x86d0257a, 0xd81391d7, 0x8d1b344c, 0x3c3e9a54, 0xdd5282a4, 0x72957a76, 0xa92bdb8e, 0x9e9dfad5, 0x13434ceb, 0x77559a7d, 0x82e2c4f6, 0x1088b141, 0xd7f3713a, 0xe9158f33, 0x78e4e984, 0x15e5f429, 0x4bb3771d, 0xf5b2af8a, 0xd93a5c5b, 0xce6bba17, 0xb5301cbf, 0xf7cdb8a1, 0x718195f6, 0xff826c4d, 0x2a297c6a, 0xef7f745c, 0x03014f22, 0x58a31100, 0x5e0af435, 0x76e0e967, 0x49bfbefb, 0xdc1df615, 0x8e70ad1c, 0x8f018598, 0x45ea3ca1, 0x2fc045e1, 0x938cdc71, 0xdcf40c48, 0x882beecb, 0x9defd645, 0xcd48cd6b, 0x30b26934, 0x15518c13, 0x7494017a, 0x606f3a6d, 0xc3933ae6, 0x08bfb9bc, 0x35e4c708, 0xe2305d87, 0x7c5384f6, 0x724dfe37, 0x934b386d, 0x76dcd82c, 0x2f0a2e22, 0x69501a2a, 0x698a8366, 0x037842a5, 0x9ab1b83e, 0x14ac2a73, 0xb409d845, 0xffb27d8a, 0xf47d8921, 0x3cd07eeb, 0x94f4adca, 0x8bb54ada, 0xfd63eeee, 0xe99fcc7f, 0x3d332142, 0xf3a55116, 0x51559641, 0x5e1a79ee, 0x1b499e0f, 0xe029b1f4, 0xc13d6ddc, 0x8eec872a, 0x566ce2c4, 0x7a5b3e3a, 0xbbbe18c9, 0x57fe99a5, 0x16174c72, 0xec9d16fe, 0x00210ce0, 0x613db062, 0x3d8833e3, 0xb2eeb7f1, 0x941af60f, 0x2b41b27c, 0xbab2ee06, 0x29e132ea, 0x52b637b7, 0x3a857997, 0xded5e26c, 0xbdedd156, 0x95c6bf99, 0x5e503b89, 0x9788314c, 0x125008bb, 0x52a41a1b, 0xafe9bd96, 0x73726042, 0xb4955c6d, 0xeb62cae3, 0x3753d613, 0x0e3f9e6e, 0x6ed38f3e, 0xd86051e7, 0xceededcc, 0x14a97c3e, 0xcc489da0, 0x8e476def, 0x513545e9, 0x20413975, 0xf48336ca, 0x624f564e, 0x0e95be63, 0xe2e6f6ce, 0x0cc181c0, 0x791fc5fd, 0x9bf2b807, 0xf03a9e64, 0x4e0604c6, 0xd91c5f62, 0x6dfe0f81, 0xd609f698, 0xdf34d25a, 0xda6e9c24, 0x6a3cda5b, 0x62619c72, 0xbcc58c16, 0xac13df0c, 0xbd46f663, 0xa2a918d7, 0x8df19d57, 0x20c5ea0c, 0xabf02aec, 0x8ae89c36, 0x0d9b8767, 0xe6e7a58a, 0x72fba4fe, 0xf01754c8, 0x0fc8af4b, 0x884e7ad7, 0xbf22317c, 0x086b2f50, 0x6f89ab62, 0x0dfc3b53, 0x66c4975e, 0x61a6084b, 0xce1181ec, 0xd8c89b63, 0x0a04e924, 0x2a3c75b3, 0x8fd8df23, 0x941605fa, 0xf4777ee6, 0x70a6d765, 0x85c4c544, 0xd6a71110, 0xb581dd9e, 0x2ad7005b, 0xfb72fede, 0xf4c80925, 0x36e400da, 0x93df5cbf, 0x78016978, 0x8bd6fa2f, 0xf42c8a69, 0xb58f3a48, 0x467fad55, 0x49b7f13c, 0xb2a9e2ed, 0x8b442416, 0x55a70c3f, 0x1745f60d, 0x06bd4afc, 0x4a62b591, 0x7d578ccf, 0xa2ce8114, 0xf85c377c, 0xd30d6c22, 0xa8e03cde, 0x383e6e98, 0xc39ab72f, 0x0d301120, 0x5a3ff16d, 0x5f2f7939, 0x282fd388, 0x24bcdb5e, 0x6bf51773, 0x96d69519, 0x9d6e6cc3, 0x9a875e95, 0x1749467e, 0xf3832e33, 0xaca7f67d, 0xf905c873, 0x60e8acdf, 0x27484685, 0xa7260b61, 0x32751fdc, 0x0c8f43f5, 0x6e980de2, 0x66fb4d00, 0x40c62cf0, 0x421bb383, 0xc5458b4e, 0x93e3f361, 0xc719f6ea, 0xa54095b1, 0x458153db, 0xf4904fdb, 0xc17ceb84, 0xd895cdee, 0x453a6f0d, 0x59f2e469, 0xc92dacaa, 0xf6bc4ee7, 0x963e48ee, 0xea295409, 0x7c94fd24, 0xd6ab133d, 0x9b095054, 0x6e138ab6, 0x372f6fcd, 0x1c46846e, 0x0c7364f7, 0x16c7d5dc, 0xef21ad3e, 0x831ec948, 0x2290ffb1, 0x52e5650e, 0x96bb2647, 0x35abe2a9, 0xa65f4f3c, 0x3a1c98f2, 0xf99469c1, 0x16bac354, 0x46c41925, 0xccb4e29a, 0x1f3d2fc4, 0x5960a5d2, 0x0bc9818c, 0xd0dd4e12, 0xe7bee0f9, 0x8a725071, 0xd851e6bd, 0xf93ea075, 0x3dd1b884, 0xb95797f7, 0xd38fefb6, 0xdf708359, 0x768ca3c9, 0x8a41bafa, 0xba6e5b13, 0x2e5bf73c, 0xa2f6056b, 0xf7a3d268, 0xb5e03268, 0xace6f72d, 0xd135cee2, 0x129658af, 0xc2e094a3, 0xdbeb15c1, 0xb5670b77, 0x200514fe, 0x23a15e29, 0x91fbd460, 0xa33bfef7, 0xa720e8ab, 0x8c9bf361, 0x7cc84077, 0x71bde49c, 0xed336fa5, 0x50d8723c, 0x8742e76a, 0x258edcfe, 0x1076555f, 0x1e67dd86, 0xb48827ad, 0xf5dbaf44, 0xf6f7ae93, 0xe383d265, 0x71010d1f, 0x6f7b048c, 0xba44df93, 0x150ff25c, 0x7dd5fd05, 0x64ce5cda, 0x4e9ac4ea, 0xfa938c36, 0x36ea114d, 0x070c38e1, 0x15e00e64, 0x60d96990, 0x77da2866, 0x3392c20f, 0x66638b0b, 0xfb2fbc60, 0xd98eec37, 0x082a739c, 0xeba245a5, 0x8e4511c6, 0x8aa472c8, 0xeb5a638d, 0xf5051ff0, 0xe17e60fb, 0x81bf3ce6, 0xbf3d78e1, 0x6b0273e7, 0xda1f2048, 0x18c753b4, 0x32b9e850, 0xca145661, 0x8a72273b, 0xfc2a483e, 0x220b7838, 0x4a6f669b, 0xaf78e806, 0x1b4a9528, 0x6de25702, 0x66753fa6, 0x30954419, 0x6c0f9729, 0x157fa568, 0xaf24216a, 0x62d00eb0, 0x69de8db1, 0xd11b8d5e, 0xeb999cb9, 0xf1e4f30d, 0x2df4c02d, 0x36065d61, 0xeda369dd, 0x71b91ed6, 0x0c475138, 0xfd7cf108, 0x35ab11c6, 0xc94f4d1d, 0xed7dd424, 0x99dc1d8a, 0x7ada7170, 0xe9788411, 0x351fe602, 0xcf0e0bea, 0x138f12e0, 0x59a20fb8, 0x20e114d2, 0x83a5fd6d, 0x84964679, 0x056f879d, 0x9d6406d2, 0xc243f306, 0xebef97b3, 0xb744e79d, 0xa878f194, 0x7d8150f9, 0xcdd9ac63, 0xf8afa3b6, 0x8802eeed, 0x1d78e048, 0x85861f4a, 0xfbf338ed, 0x7217d0cf, 0x73be0e8d, 0xc2c57d37, 0xd2375e73, 0x18fb69f4, 0xef90628c, 0xf5c69d2f, 0x05d3a40e, 0xfef2a92f, 0xa1f01211, 0x3b52f126, 0xa1613cbf, 0x294ce2e3, 0x38b8c303, 0x702e962d, 0xa3d55a7c, 0x784e9d94, 0x9542e6c3, 0x5bedb6ea, 0xaa4f97c1, 0x1bfd5441, 0x9dc7c05e, 0xb16ca88c, 0x1da31c16, 0x2b51129d, 0x64eb2a8c, 0x835b097a, 0xf38aa47c, 0x07888143, 0x3effe9d4, 0x8280b58b, 0x2cf2277c, 0x05bfec4f, 0x04fd2d9f, 0x41a9f8b1, 0x5890501d, 0x210211fe, 0x712cedd9, 0xc01a192e, 0x55b7163d, 0x76c3c069, 0x44adf8e8, 0x3cc2aee7, 0xf5833267, 0xff99e670, 0x3afefdf9, 0x39b7fdc7, 0xb0b3d12e, 0x659fad47, 0xc47d1f6e, 0xc77dbfb8, 0x8d755ae9, 0xc5d5d968, 0x5114206b, 0xcbf9f825, 0x00c16f2b, 0x8e4574bc, 0x34aad010, 0x03e99c4d, 0xf40eaaaa, 0x52a6a678, 0xb8418751, 0x6ffa84ef, 0x19683f38, 0x12cc3e29, 0x7144a93d, 0x29f0f9fe, 0xd260d921, 0x3fc945a4, 0x536e7924, 0xd00fe6d6, 0x9f79a06e, 0x504d078e, 0x0dbf371c, 0x4130cf4b, 0x00d5efe7, 0x82691f7f, 0xc6488723, 0x9bea125d, 0x74cfeea1, 0x3f8526ad, 0x41017779, 0x4e2b1174, 0x339b99a2, 0x33db6686, 0x22386b7f, 0x6dd556c2, 0xb32835c1, 0x8deaa768, 0x63afd4ad, 0xca558005, 0xa52895c8, 0x0822e208, 0x8e211f6c, 0xaaa60cc7, 0xce819a50, 0xb18bdd0e, 0xd032d5b4, 0x5d05ecc6, 0xfb201016, 0x17f35371, 0xc76dd59e, 0x3842ba3f, 0xd4ecfb1a, 0xfa3a8e0f, 0xbedbafe2, 0x0dbe2c5c, 0xfbce7480, 0xacab2838, 0xed58b6ca, 0x57f11fe5, 0xdae58a71, 0x6303fb8c, 0xfedcfea6, 0x005bed8f, 0x57ccd4b9, 0x7a6bfefa, 0xafb9eff9, 0x587fb631, 0xa3774fbe, 0x4cb84cd7, 0x60257bfb, 0x35cb0648, 0xa65374c1, 0x165e83f3, 0xce2643ac, 0x7a0beeeb, 0x77d67d82, 0xe068e67c, 0x1f675f3d, 0x3a686024, 0x0585a72d, 0x4134dba8, 0x1f4c3309, 0xda8c596a, 0xc10ce519, 0xe0c4e0a3, 0xbed995a2, 0x22066d03, 0x2931625e, 0x4bb234f0, 0x3d19ceac, 0x0015da4a, 0x1dc87923, 0x0ce57832, 0x3c082de3, 0xedfe9112, 0xb64beb5c, 0xd50ba80c, 0x45ca2d3e, 0xee733644, 0xb3abd4b0, 0x7d13f1da, 0xe5d14ce4, 0x039eefd8, 0xac2bd487, 0x812b73c3, 0xba59f28b, 0xf58b13c9, 0x0390a480, 0x101958ca, 0x62d26619, 0x165d872e, 0x7bbae4f1, 0x88a601ca, 0xf39c7fcd, 0x825a5a01, 0x0d226238, 0xb549bb78, 0x7ccd6f2e, 0x6d095996, 0xb0567749, 0x8530f16e, 0xffca3848, 0x3656b104, 0x00339f0a, 0xaa8a55ef, 0x8b10e692, 0x04487622, 0x0e06c360, 0x9247d69c, 0xf8f8b2e6, 0x2a94b3de, 0x7ea1a275, 0xdc42660e, 0x7088c61f, 0xe1bb5030, 0xb3916fe9, 0xca390927, 0xad2d2cf2, 0xaf641b4e, 0xadf900e8, 0xb81d3550, 0xa47e02d5, 0xa65f55a6, 0xc2246bc8, 0x7984dcbb, 0x0868c0d5, 0x3b0ed447, 0xd4b08144, 0x712227f1, 0xe72de165, 0x77de4665, 0x0af66b5d, 0x9dc3bea7, 0xc9b1f568, 0x06851474, 0x6ba69abd, 0xfec47d33, 0xf9d69b1d, 0x8f369888, 0xd82b120b, 0x5680c0da, 0x073e0b4a, 0x3537e161, 0xadcbb641, 0xb5c25b08, 0xe6cf800d, 0x89ff9a79, 0xc5eb328b, 0x7d1e4a36, 0x008969a4, 0x357b1e10, 0x091a1eda, 0xea1b4ac8, 0xa13176e3, 0x98819356, 0x98f21f13, 0x9b554fb6, 0x2f094944, 0xde140250, 0x7d53c489, 0xe141a8c3, 0x97492441, 0xca77cd47, 0xc4fe200a, 0x05a4fa66, 0xc1d27eea, 0xb48f22c2, 0x716602bf, 0xeeeb1263, 0xb89b1694, 0xd2d6c684, 0x8e6114ab, 0xc6d231d9, 0x3b4a5203, 0x98915636, 0xd44e1c45, 0x38a49a3c, 0x50ac1894, 0x1476ca37, 0x4ca7c83b, 0x4fac4b1e, 0x9696e488, 0x33d23dbe, 0x8b6690fe, 0x477bd2e2, 0x9c5eeb6c, 0x61ff2b24, 0x70f0cc14, 0x6d1d4b90, 0xaaf09e21, 0x7aff0b07, 0xb38073b6, 0x714f90ff, 0x4f7ee1ea, 0xa8155211, 0xf715104d, 0x86af21f0, 0x749095a4, 0x2a193902, 0x5efe3247, 0xad12f051, 0xcc9e5acf, 0xe1da3419, 0xea960075, 0xa7266501, 0x497bf896, 0x88eb1358, 0xbf98853c, 0x38ead5c5, 0x457cad5e, 0xc7a0e918, 0xf9d7df8c, 0x3c077988, 0x5ea2299b, 0xe361ade4, 0x444c7598, 0xb9eb51cb, 0xc3ffc354, 0x4765840a, 0x8bf62084, 0xe86637f5, 0x12108084, 0x43c60680, 0x79c00291, 0x8a3c1407, 0x12d92b08, 0xc460875a, 0x642df87c, 0x658eb66e, 0xa948dce5, 0x84f2de13, 0xfafbf168, 0xf38a9a4b, 0xaa9dbfe1, 0xad52b4b5, 0x9098a4af, 0x8c3a41de, 0xb4e1790a, 0xf28f8fd8, 0x79b6e6b7, 0xda7eee27, 0x1a9a9e0c, 0x6f6cfe42, 0x8c0fbbca, 0x70235c93, 0xe31bdd59, 0x5101b653, 0xebd95907, 0x650adf02, 0x8a90d909, 0x8db9bade, 0x6021b521, 0xaef1ad35, 0xf1f4b33f, 0xce481c2e, 0xe03a17ea, 0x494539e0, 0x2d194f27, 0x2d51e110, 0x4a0de34d, 0x283432a5, 0xaee91b11, 0x4fc5953d, 0x7b0b50a7, 0xb3a844dc, 0x8eb874e8, 0xde6e3bb9, 0x6d245784, 0x9146b224, 0x8a15ef39, 0xedfda77b, 0x191d73ab, 0x01aa3e59, 0xb0202c46, 0x72b4349f, 0x34f92274, 0x9d937103, 0xed9d2e95, 0xa4fee698, 0xb225b25e, 0xcda478ac, 0x343858c2, 0x6e0614e1, 0xe7281c0b, 0xb22c9edb, 0xa8d2b08a, 0xe574adfd, 0x73e6e316, 0xa7961ce5, 0x2f96a88a, 0x07a4922f, 0xd3a27aab, 0xc7bdbc9c, 0x93593d41, 0x31f2972c, 0x6b050ef8, 0x25c371a8, 0xb251ddc3, 0xece0234e, 0x57316e1c, 0x5580ff4a, 0xe754fb22, 0xfa752ea1, 0xf29a7d8f, 0x837aa2e1, 0xf79a3db4, 0xe9a0b278, 0x0b7e9a4c, 0x039b7d53, 0x19700cdd, 0xe468e2d5, 0x4ef67429, 0x5f9a82b9, 0x14b1818e, 0x8ac4456f, 0xf4fcbd5f, 0xc25e5cbd, 0xca44fa25, 0x0cdbbc49, 0x1e292d92, 0xecac0c7c, 0x0d6abbea, 0xa75d3b7e, 0x6a0ebfac, 0xa4b4d4e7, 0x5d05e427, 0x4d25b42f, 0x2ee101b8, 0x7e6bbe0b, 0x2692810f, 0xa3bb41a5, 0x3c170aec, 0x64c29776, 0x9ec1de52, 0x073464d4, 0xd2334a19, 0xb11ecbfb, 0xc2fb081e, 0xe5d3f11a, 0xf575161c, 0x2d5c8d94, 0x16ee97c0, 0xd860049b, 0xd31b1d3e, 0x88e8d88c, 0xf8d2e782, 0xc5ce6d05, 0x52b3803d, 0xf668f35f, 0x4e5c5a25, 0x0f6ad5ec, 0xcef38344, 0x33ac3c28, 0xec7b00a7, 0x2255a18e, 0x77731e06, 0x9ed38a19, 0x5f408d79, 0x0005df1c, 0x3bf190f5, 0xe5cdbd6e, 0x33d7f6fd, 0xb539269e, 0xf8979097, 0xf773f555, 0xe3340f3b, 0x9eb34ff6, 0x9992249e, 0x5a49e867, 0x3ab333df, 0x14c08f75, 0x69db660a, 0x6a6b7da7, 0x0802cb82, 0x9df83473, 0xaa63b2af, 0x467cbfe6, 0x3a3da69e, 0x115ebe67, 0xba794938, 0x61ae6ceb, 0x12ddf6a5, 0x031dcbf5, 0xf5794054, 0x33ec59b7, 0x22761015, 0x2fca22c0, 0x3d7aede0, 0x31817cf7, 0x81625a1c, 0xfd459b67, 0xef53f4c2, 0xcf32c3ae, 0xf71212df, 0x017c285f, 0x45915348, 0xdcd3e3a3, 0x7b748de3, 0x85a214c3, 0x4a8c3466, 0xdfba975d, 0x186cdf92, 0x458838e4, 0x517ce04e, 0x009ffdb6, 0xea38a017, 0xd638dba6, 0x97a5afab, 0x3307c380, 0xcff6a21f, 0xe7f04e3b, 0x8458cdbb, 0x96817ff1, 0x70620b84, 0x9d9dd03c, 0xc1b7a406, 0xbfcede98, 0x0718bcd6, 0xbf9c53f5, 0x3972f290, 0x2b4046c2, 0x1836dc39, 0xda22e544, 0x3473c9a2, 0x565dc98a, 0x8070c75e, 0x3acd2646, 0xd7e67d98, 0xb8ab5499, 0xde9a6a6e, 0x06fd97d9, 0xcbc6a963, 0x766721d4, 0xd9c14299, 0x276debdf, 0xeb878ecc, 0x25959871, 0x323b5703, 0x6fbc0a93, 0x461cc59c, 0x440e4060, 0xc50f826b, 0xe7581f2b, 0x7fddb23f, 0xa8183462, 0x5a7d14a3, 0x63dae8eb, 0x71759f0c, 0x174fe030, 0x191b931a, 0xd77d14b2, 0x2d5d1afe, 0x8926c797, 0x7a71d07e, 0x123a1174, 0x825a0c0c, 0x4eb0f759, 0xbc5212ed, 0x84fbfbbc, 0x45bbf49f, 0x6a88813e, 0x02e1ccdd, 0x24ac0f33, 0x4b66f93d, 0x2fda0d07, 0x2304c24f, 0xff91ee3e, 0x672d7bc9, 0xcf30569a, 0x896c75d0, 0x70033596, 0x8d2f77ef, 0x84caf55c, 0x9a4912e1, 0x3abece82, 0x60d8ad30, 0xaefdd54b, 0xf8fd6fe8, 0x82a453c5, 0x0780b4d5, 0x2fdb9b42, 0xb2f1f375, 0xeb423633, 0x906fc154, 0x83fa194b, 0x37f38f6f, 0x5935340b, 0xac6c3195, 0xaaa2f683, 0xaec0ed11, 0x53dcbe65, 0xfc62691e, 0xfd63f19a, 0xbbff97e3, 0x676480a7, 0x1acbf25b, 0x40d2634b, 0x776e9335, 0x1678f0f1, 0x46bc6731, 0x7f52d21d, 0xc16982d5, 0x1bb96de3, 0x68cb511f, 0x829e142c, 0xf9fe3228, 0xfd62776e, 0x94857fc8, 0x70a9ca65, 0x42097c7b, 0x751a6c89, 0x513c69f1, 0x2efdbaec, 0x6623bf47, 0xc36d8035, 0xd0837409, 0xbe55a333, 0xab4f3bdf, 0xa5b046c5, 0xf7a37757, 0xe787f59e, 0x87913a54, 0x480a180d, 0x81a5ebd5, 0x511eb7ee, 0x12f5a8ab, 0x73447c1c, 0xa164c28c, 0x57900661, 0xba9414fa, 0x3bf9ed82, 0xd86a6acd, 0x5b09f797, 0xa5663ec3, 0xc86cbf84, 0xdcefc67c, 0x48a84c90, 0x5c745f28, 0xa8b236e0, 0xfbccb56a, 0x40383b3c, 0x6b238a05, 0x90eefdec, 0x7e951a6c, 0x940e63a3, 0xf98ef893, 0x8c977a6e, 0x71c42456, 0x5d61332f, 0xea297f4b, 0xa9b252c0, 0x3e97cb15, 0x48050881, 0xf5b264d2, 0x2be5347b, 0xef030ce9, 0x41f2c0bd, 0x04d24fe2, 0x06e7e1cf, 0x01548902, 0x32c32925, 0xfb27d2b1, 0x6a360112, 0xfc79125c, 0x3a6bacbc, 0x987eebf4, 0xfc565822, 0xf9ede83f, 0x4568ad7f, 0xf583840b, 0xa7eb1583, 0x90f031a4, 0x3b252d95, 0xb612a076, 0xb78783cf, 0xe1f616ae, 0xbec3df2f, 0xd0c58988, 0x00ab849e, 0xdec160fb, 0xb77b7645, 0x2e0c857e, 0x61c65b1e, 0xc4889032, 0x7112c73b, 0x114dbae6, 0xa0145c70, 0xb9958cd7, 0x7a318fb8, 0xebe7ae55, 0xdb9d9621, 0xe4f27eee, 0xe4435669, 0xc80091a9, 0xbaa569d3, 0xbe0e3758, 0x4097462e, 0xe1fec615, 0xd9e85d86, 0x84291b04, 0x1ccf2614, 0xadef18d2, 0x719639bc, 0x82794162, 0x0282420f, 0xabdc9b1e, 0xdbd734c9, 0xa89d747d, 0x91266431, 0x64a1d430, 0x5c3a1c2d, 0x0502cc98, 0xb2070762, 0x442dd308, 0xbf28564d, 0xc68815ed, 0x3e4eda5c, 0x128b113f, 0x49b41127, 0x9a84ec0a, 0xa9bdfb30, 0x8a99056d, 0xa95de301, 0x8d8b0982, 0x24f48723, 0xab3f376c, 0xcd89d8fe, 0x276b6c49, 0x15f00e90, 0x9f14da21, 0xe97407dd, 0xeb33ae49, 0xd0811453, 0x0a10eea7, 0x2588a497, 0x575c1f87, 0x5141ae48, 0x6eaaccbc, 0x159ce14e, 0x0a9d6ddb, 0xed91259a, 0x4b23e675, 0xd1671bcc, 0x28beae5a, 0xc6ed5424, 0x9e88e390, 0x214494c8, 0x23da4138, 0x87053bd5, 0xb9d03560, 0xf6a99473, 0x72a153c4, 0xf94c20b7, 0x70a7b274, 0xba274c89, 0x767f6e53, 0x638da5da, 0xa23bf5d3, 0xc0c3b92e, 0x0fb97f72, 0xd9a25e94, 0xba7e4365, 0x909cffc5, 0xc7bba836, 0xc2684d34, 0x2dee53e7, 0xdd75d5bb, 0xbd7458a7, 0x8e810115, 0xc5a84a82, 0x12c3edf4, 0x941d772c, 0x4a153ba6, 0xbdd1d73e, 0x3b7755b7, 0xa208da2a, 0xb2f24ecf, 0x22bc87b0, 0xd3baa4b4, 0xe033a60f, 0xfef480aa, 0xc4a3eb4b, 0x77a87092, 0x42f40429, 0xb09c304c, 0x640044a6, 0xff901800, 0x3b81b734, 0xf9926fdb, 0xb825317c, 0x6a2a1c29, 0x4f33ecc0, 0xe0a3e7cb, 0x8f2c3191, 0xe4c25344, 0x62d6d81a, 0x8deb3647, 0x8c8a8674, 0xcac73002, 0x4372b23d, 0xbe4a30e3, 0x25fd3466, 0x3202e33a, 0x0fbb0725, 0x1019bb09, 0x0e5c92af, 0x6e82ff5d, 0x39db8dc3, 0x901eeaeb, 0xa05712a9, 0x1630a239, 0xef88227f, 0x93fe8ec2, 0xe0e21c3b, 0x7200598e, 0x52d14e13, 0x84b35afe, 0xa32dd8a6, 0x47ea13bc, 0x280b5ce2, 0x99410c2e, 0x961e54fe, 0xda53ac4b, 0x7ce9a3d4, 0x50f03b76, 0x14325768, 0x8c0cc024, 0x2390ddb9, 0x026c576a, 0xc5881d73, 0xe4a9ffea, 0x6db0629a, 0xdd6c0421, 0xdccaaa6e, 0x1368c821, 0xbf593da9, 0x2cbf7423, 0x02167699, 0x74f9fae5, 0xb5374a5f, 0x8d794559, 0xaac940b3, 0xac8d2452, 0xf70f6d6d, 0xffc598a9, 0x2ffae518, 0x64a19d54, 0xd5d35698, 0xb0802198, 0x4aab10ea, 0x79aa57fd, 0xce39bcc1, 0x38866f22, 0x94982f77, 0x95ebf8c1, 0xa761fcff, 0xa7bf1657, 0xfdcad5cc, 0x7691f346, 0x12e6e3f5, 0x762b4344, 0xa2f14a2a, 0xa710ee06, 0x1675f6ed, 0xaec92100, 0x1a1a81cd, 0x7836dc15, 0x31128a6f, 0x94272ed4, 0x693553f2, 0xf72ee615, 0xf311074b, 0x2fd354a2, 0x2cf6d090, 0x9c87ae3a, 0xc8ead3c4, 0xce07766b, 0x425fa6d5, 0x3f9e735b, 0x0a865da1, 0x729e0347, 0xe450b189, 0xd5560f8c, 0x387de83d, 0x364794dd, 0x79c4fe6d, 0xee112e73, 0x09e99443, 0x5039d959, 0x7ba8d125, 0xc8588993, 0xd0810ccf, 0x13eb6b47, 0x89e8bae1, 0xbd061f53, 0xa409cfb2, 0xa4f7fbeb, 0x568e8046, 0x75693036, 0x9be660d4, 0xda3f8e86, 0x9e1b90dc, 0xc023e24d, 0x7e82e39a, 0x62f2ea10, 0x523f0663, 0xa7a72d64, 0x2636e7cf, 0x4dbe27a7, 0x64d1ed4a, 0xcb55f7a0, 0x06dae206, 0xb56e68ca, 0x3539b098, 0x0e395df1, 0x83b6bfe4, 0xb6cd4f6a, 0xab50a3bd, 0x72edfe15, 0x1f8515bd, 0xe495a19a, 0x9d46df4f, 0xf488013f, 0x4d1f9fdc, 0x971d292e, 0xc572615a, 0xe401572b, 0xd59a0b3c, 0x0658da8f, 0x60768ddb, 0xa9943e19, 0xc3a18a20, 0x285ec952, 0xc4776f99, 0x9f1eb4e3, 0xe42e9536, 0x887e4b47, 0xf9136d85, 0xbf533be2, 0x86d74a20, 0xe0d6c774, 0x552caece, 0xe7bc30e6, 0xfc80517a, 0x5e6bdd58, 0xb1c46c72, 0x0e8dc32a, 0xb5444136, 0xe9b46133, 0xb2f60032, 0x7dce7daf, 0xba4a0116, 0x03480b79, 0x3495a197, 0x2ecf3b56, 0x4fe86542, 0x2e83dbaa, 0x06906bf2, 0xd2d77b7a, 0x898117c1, 0x362b57c8, 0xe2257d0b, 0xeee583d2, 0x6fbf1761, 0xfb3a1d2a, 0x18ad48a1, 0x1ae0ab8b, 0x4fd0242e, 0x909eb19b, 0x759f039f, 0x43a2e2b2, 0x3c08c8ce, 0x8a6699a6, 0xa1c4bd34, 0xb21aafab, 0x472b70ee, 0x1e29db1c, 0x2762d2c7, 0xcd312ec0, 0xf653e8eb, 0x5ef39eb7, 0x0a3c253c, 0xc5fc1f49, 0x95894cc3, 0x7fb8bc3f, 0x716c7bdf, 0x794a2a9a, 0x8dcb3652, 0x35faf233, 0xcb605201, 0xf3ce4060, 0xf8bae3bd, 0x8feaa345, 0x3d52e33f, 0x2367b6b9, 0x6dae73c7, 0xc7247d46, 0xe44280cf, 0xbff900fb, 0x6a590053, 0xed4f734b, 0x45cfad5d, 0x8817d88d, 0x2ddc30ed, 0x324a17f9, 0xcdcbb3cb, 0x5ae05e7f, 0x89844b49, 0x31ea7a9e, 0x8982a99c, 0xe2a10114, 0xadb655a1, 0xd820908a, 0x828ca7c7, 0x44cad636, 0x70fadfe7, 0x5ebf1018, 0x72b586e6, 0x315e60b3, 0x9540cd5a, 0x2756b8c0, 0xc0d24fbc, 0xb4941c20, 0xfb2575c6, 0xacc7fc8b, 0x75b1f87c, 0x9dd7303f, 0xf1bcd180, 0x30930381, 0x957288c6, 0x829cd6b6, 0x65451d04, 0x17bc4afc, 0x221421ae, 0x5b33489a, 0x9d999adb, 0x04c89542, 0xe2a97f88, 0xd8dc0202, 0xbbb1fe02, 0xfa8cd2ff, 0x8b9f628d, 0x0f2ec94c, 0xe5dc328d, 0xe538b905, 0xa9512e86, 0x211bec69, 0xf54ef592, 0xd3b87def, 0x739d3e47, 0x2cf040fb, 0xa89f7e32, 0x2175f03d, 0x35aa1a3e, 0x7b7d1595, 0x8b118b51, 0x3f0e7231, 0x31bc24e6, 0x01057bc5, 0x3c074f2e, 0xf799896a, 0x189ee136, 0xdd07f1f8, 0x71f4afa9, 0x55f1ef6b, 0xe20e1209, 0x6142479a, 0xb9271402, 0xcc610aed, 0x7e3cab0e, 0xca27138b, 0x8b1cc8a3, 0xce0192c9, 0xac153ded, 0x55ca1523, 0xcaf7a020, 0xbfa450bd, 0xfd4c968a, 0xf18b7cd8, 0x36b2d54a, 0xdd569890, 0x559a8a83, 0xe69abaff, 0x1248baa7, 0xc4a10e27, 0xfbabffb3, 0x1a67d584, 0xaf197632, 0x515c7c01, 0x623e0185, 0x92407512, 0x5cf012ab, 0xa9329d01, 0xd5c5ad74, 0xe7e92fba, 0x287c0aaa, 0xa6466ada, 0x61072e3d, 0x63096658, 0x9c9f33a4, 0x208aaacc, 0x40afb9d2, 0x95182939, 0xd691b81b, 0x49810ea8, 0xdecbc1f3, 0x821283c6, 0x5dd139c9, 0x04f1c5ed, 0xd4cb9520, 0x3711aaff, 0x1b4a1cad, 0xf5982481, 0xf92bdd32, 0x062c3092, 0x908d1eb0, 0x597f5f99, 0xdb8b3203, 0x0fafd4f4, 0xd07212b1, 0x00a44adc, 0xbf2e210a, 0xfa9d9413, 0xa6fad16d, 0x1a8abe34, 0x51c1c640, 0xa4e2434d, 0xba04acfe, 0xf5b7b040, 0x3f237988, 0x26621ed6, 0xb964f65e, 0xf0d2079b, 0xb0499b7b, 0x85f487c2, 0x1e42fa26, 0xb0debd68, 0x483b9ebf, 0x8c7046a0, 0xb03e8050, 0xf18e3072, 0xdfd63c33, 0xd2f2b783, 0x833697a8, 0xf33b8fb6, 0xe73d9440, 0xff64a238, 0x353fc592, 0x5590801a, 0x2b79cde8, 0x54aacfa7, 0xccf98129, 0x23aad0c4, 0x6482d132, 0x206c6673, 0x2775a133, 0x8f8859ca, 0xcbb95277, 0x4ae57aaf, 0xa47cbf5f, 0xbcb0bc6c, 0x90f565fa, 0x1cd23ba5, 0x2ad6ef24, 0x8764cc36, 0xb684ef70, 0x7e8d6183, 0x87087822, 0x9f8c3f85, 0xcba572e9, 0xf8f84147, 0xe99ec2d8, 0xda1606fd, 0xaf04573f, 0xa551905b, 0xf41694b8, 0x986bbf8d, 0x07bfda33, 0x6e9951dc, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well44497b(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitInPoolIsFunctional(Well44497b.class, 44497); } }
3,047
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well1024aTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well1024aTest { /** The size of the array seed. */ private static final int SEED_SIZE = 32; @Test void testReferenceCode() { final Well1024a rng = new Well1024a(new int[] { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0, 0xdc8174cf, 0x3e73a4bc, 0x6fce0775, 0x9e6141fc, 0x5232218a, 0x0fa9e601, 0x0b6fdb4a, 0xf10a0a8c, 0x97829dba, 0xc60b0778, 0x0566db41, 0x620807aa, 0x599b89c9, 0x1a34942b, 0x6baae3da, 0x4ba0b73d }); final int[] expectedSequence = { 0xa7dc11e2, 0x9dea7324, 0x844c7605, 0x85025732, 0x92ad1e10, 0x968e8090, 0xfd92cb4e, 0x665e1202, 0x7eff3e03, 0x2eb25d85, 0x22002049, 0x5cfc119b, 0x26ef8f33, 0x519448b2, 0xfbb4d089, 0x3fd7de78, 0x37a84c6c, 0x018e7b90, 0x02f93e0a, 0x6bc587fb, 0x4125b170, 0x2cfe1251, 0x4fb0ea3c, 0x8989e9b0, 0xd6cd467e, 0x947b1d89, 0x423431c2, 0x45eeaa79, 0xe8b1d00e, 0x780e82cc, 0x0ac61f4d, 0xe92d8bfb, 0x8e43df27, 0x5e38245d, 0x406394b5, 0xf88487a8, 0x7cd7febf, 0x7f227485, 0xe5db8d04, 0xd2aec04b, 0x3f1292ed, 0x7a3cfb20, 0xa48a7893, 0xfc458532, 0x31253d6e, 0xcf354d5d, 0x9145cf5d, 0xd72f590e, 0xf6ab2301, 0xca30f9c5, 0xcb8021c6, 0xad4eb3a4, 0xb4b7e1d5, 0x1ab409c5, 0x0bfb99ba, 0x7306d009, 0xe4dba576, 0x281c99d3, 0x7736b135, 0xa3cd1046, 0x1a9a9fe2, 0x3bb4adae, 0xd183615c, 0xeb462c96, 0xaff62ad3, 0x61b9dece, 0xce3617d5, 0x59bd68e0, 0x15e00d2f, 0x86a72cac, 0x958249bf, 0x4a7d49f1, 0x0adfbdf0, 0x56198ad1, 0x6c33cb4c, 0x4f7fc05e, 0x11dc8281, 0xef07f51f, 0x7942882e, 0x54d60027, 0xb160dd94, 0x5cd24e29, 0xe576d046, 0xf0fdb2fa, 0x5f88934b, 0x3844da1e, 0xc32bf41e, 0x0f66052b, 0x28d826df, 0x6d9c60cf, 0xf6a95620, 0xc59a67e6, 0xdfe9ff7c, 0x0dfc5eea, 0x0c95ece6, 0xda1f0f70, 0xc234b213, 0xafad6be5, 0xde497dae, 0xaf03aacb, 0x1e50e6f3, 0xd12106eb, 0x7b77d295, 0x47f0b2e4, 0xd78853fe, 0x09fec179, 0x089fedc5, 0x6680db4d, 0x5deddb60, 0xaff0127d, 0x96b5cec8, 0x10fca09f, 0xd53ec956, 0x3534d053, 0xae70ae3d, 0xdb4d222c, 0xd47770c7, 0x5115fee3, 0x9094ef39, 0x69fe3b7c, 0xdd116917, 0xd64e3746, 0x03aae089, 0x91195149, 0xe1069acc, 0x6dbcbde5, 0x5cb8b9ed, 0xe828ccb7, 0xd0e447f2, 0x192ca3eb, 0x77af1ef9, 0x79e37fe6, 0x99f2710d, 0xf9dda18a, 0xd6a47494, 0x8f1b3489, 0xb3682658, 0xf321e2be, 0x05b64ca4, 0x60e803b8, 0xc10f74c0, 0x1e94c84d, 0x6ccb0d5e, 0xdf02d86c, 0x8d5bd3f9, 0x9091fef2, 0x21cf487c, 0x9796c3d2, 0x92c94ca9, 0xf98df7f9, 0xb1c8be62, 0x00d5ba0b, 0x32ad5936, 0xe935321c, 0x9831f624, 0xb179166d, 0x7420ecf6, 0x2cf10ae7, 0x3d49a2ab, 0x146a0bb4, 0x910037cb, 0x2b24721a, 0xf2098316, 0x5ff58eb1, 0xd0274270, 0x4e6e006b, 0x5598bbb5, 0x490076a0, 0x7fd35adf, 0x92545942, 0x0d667f1a, 0xa8e04323, 0xbf9a9b38, 0x61aaa5ba, 0xb92de80c, 0xec9e1fad, 0x97a6cd05, 0x95e10296, 0x29a6bd92, 0xc9dba5cc, 0x11ddc4b2, 0xf65d3ffa, 0x73861431, 0x2fb3902d, 0x03604221, 0xb7959946, 0xb59b2056, 0x6ca5ac44, 0x69f44409, 0xd52c49f2, 0x71bc35d5, 0x674c4b61, 0x6e60e6eb, 0x4c80f38f, 0x966921d4, 0x7acbda1f, 0x634f1d39, 0x7268895b, 0xe24ee616, 0x29940720, 0x6e987cac, 0xa165ce9d, 0x21b084b2, 0xa7e95f53, 0xc8d38139, 0xcfede657, 0xb8637eb9, 0xab175528, 0x6a9c1f4b, 0x04232f3d, 0x27182484, 0xb9e7fdc4, 0x017a3a38, 0xcca5ca4f, 0x1a32a17a, 0xf4a6386a, 0xe91f7fea, 0xe1af8929, 0x55ed33ef, 0x24401c76, 0x5bc26738, 0x37302912, 0x298e9336, 0x6ff45481, 0x8ab94db8, 0x9d1353bd, 0xf11e84b9, 0x327daa22, 0x17b50f84, 0xf8878bf3, 0xb34c5e65, 0x3c95fa54, 0xc4843530, 0x6b029593, 0x6ead61b5, 0x99acd553, 0xdd4c9e2d, 0xcb200d79, 0x1972f777, 0xdbb00a9b, 0xa933a748, 0xe32edea0, 0x3fbb9fd2, 0x39f33c90, 0xac262539, 0x747aa26d, 0x032309b3, 0x48492c89, 0x7e3584fc, 0x10c0d6f0, 0x2c32f649, 0xe335092e, 0x6e5296ae, 0xf1677c99, 0xefdba4a4, 0x6c55e637 }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well1024a(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(Well1024a.class, SEED_SIZE); } }
3,048
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/AbstractLXMTest.java
/* * 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.rng.core.source32; import java.util.SplittableRandom; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Function; import java.util.function.IntSupplier; import java.util.function.Supplier; import java.util.function.UnaryOperator; import java.util.stream.IntStream; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Base test for LXM generators. These generators all use the same basic steps: * <pre> * s = state of LCG * t = state of XBG * * generate(): * z <- mix(combine(w upper bits of s, w upper bits of t)) * s <- LCG state update * t <- XBG state update * </pre> * * <p>This base class provides a composite generator of an LCG and XBG * both using w = 32-bits. Tests for a RNG implementation must define * a factory for constructing a reference composite LXM generator. Implementations * for the 32-bit LCG used in the LXM family are provided. * Implementations for the XBG are provided based on version in the Commons RNG core library. * * <p>It is assumed the XBG generator may be a {@link LongJumpableUniformRandomProvider}. * The composite generator requires the sub-generators to provide a jump and long jump * equivalent operation. This is performed by advancing/rewinding the LCG and XBG the same number * of cycles. In practice this is performed using: * <ul> * <li>A large jump of the XBG that wraps the state of the LCG. * <li>Leaving the XBG state unchanged and advancing the LCG. This effectively * rewinds the state of the LXM by the period of the XBG multiplied by the number of * cycles advanced by the LCG. * </ul> * * <p>The paper by Steele and Vigna suggest advancing the LCG to take advantage of * the fast update step of the LCG. If the LCG and XBG sub-generators support * jump/longJump then the composite can then be used to test arbitrary * combinations of calls to: generate the next long value; and jump operations. * This is not possible using the reference implementations of the LXM family in * JDK 17 which do not implement jumping (instead providing a split operation to * create new RNGs). * * <p>The test assumes the following conditions: * <ul> * <li>The seed length equals the state size of the LCG and XBG generators. * <li>The LXM generator seed is the seed for the LCG appended with the seed for the XBG. * <li>The LCG seed in order is [additive parameter, initial state]. The additive parameter * must be odd for a maximum period LCG and the test asserts the final bit of the add parameter * from the seed is effectively ignored as it is forced to be odd in the constructor. * <li>If the XBG seed is all zeros, the LXM generator is partially functional. It will output * non-zero values but the sequence may be statistically weak and the period is that of the LCG. * This cannot be practically tested but non-zero output is verified for an all zero seed. * </ul> * * <p>Note: The seed order prioritises the additive parameter first. This parameter can be used to * create independent streams of generators. It allows constructing a generator from a single long * value, where the rest of the state is filled with a procedure generating non-zero values, * which will be independent from a second generator constructed with a different single additive * parameter. The difference must be in the high 31-bits of the seed value. A suitable filling * procedure from an initial seed value is provided in the Commons RNG Simple module. * * <p>The class uses a per-class lifecycle. This allows abstract methods to be overridden * in implementing classes to control test behaviour. */ @TestInstance(Lifecycle.PER_CLASS) abstract class AbstractLXMTest { /** * A function to mix two int values. * Implements the combine and mix functions of the LXM generator. */ interface Mix { /** * Mix the values. * * @param a Value a * @param b Value b * @return the result */ int apply(int a, int b); } /** * A jumpable sub-generator. This can be a linear congruential generator (LCG) * or xor-based generator (XBG) when used in the LXM family. * * <p>For simplicity the steps of obtaining the upper bits of the state * and updating the state are combined to a single operation. */ interface SubGen { /** * Return the upper 32-bits of the current state and update the state. * * @return the upper 32-bits of the old state */ int stateAndUpdate(); /** * Create a copy and then advance the state in a single jump; the copy is returned. * * @return the copy */ SubGen copyAndJump(); /** * Create a copy and then advance the state in a single int jump; the copy is returned. * * @return the copy */ SubGen copyAndLongJump(); } /** * Mix the sum using the lea32 function. * * @param a Value a * @param b Value b * @return the result */ static int mixLea32(int a, int b) { return LXMSupport.lea32(a + b); } /** * A 32-bit linear congruential generator. */ static class LCG32 implements SubGen { /** Multiplier. */ private static final int M = LXMSupport.M32; /** Additive parameter (must be odd). */ private final int a; /** State. */ private int s; /** Power of 2 for the jump. */ private final int jumpPower; /** Power of 2 for the long jump. */ private final int longJumpPower; /** * Create an instance with a jump of 1 cycle and int jump of 2^32 cycles. * * @param a Additive parameter (set to odd) * @param s State */ LCG32(int a, int s) { this(a, s, 0, 16); } /** * @param a Additive parameter (set to odd) * @param s State * @param jumpPower Jump size as a power of 2 * @param longJumpPower Long jump size as a power of 2 */ LCG32(int a, int s, int jumpPower, int longJumpPower) { this.a = a | 1; this.s = s; this.jumpPower = jumpPower; this.longJumpPower = longJumpPower; } @Override public int stateAndUpdate() { final int s0 = s; s = M * s + a; return s0; } @Override public SubGen copyAndJump() { final SubGen copy = new LCG32(a, s, jumpPower, longJumpPower); s = LXMSupportTest.lcgAdvancePow2(s, M, a, jumpPower); return copy; } @Override public SubGen copyAndLongJump() { final SubGen copy = new LCG32(a, s, jumpPower, longJumpPower); s = LXMSupportTest.lcgAdvancePow2(s, M, a, longJumpPower); return copy; } } /** * A xor-based generator using XoRoShiRo64. * * <p>The generator does not support jumps and simply returns a copy. */ static class XBGXoRoShiRo64 extends AbstractXoRoShiRo64 implements SubGen { /** * Create an instance (note that jumping is not supported). * * @param seed seed */ XBGXoRoShiRo64(int[] seed) { super(seed); } /** * @param seed0 seed element 0 * @param seed1 seed element 1 */ XBGXoRoShiRo64(int seed0, int seed1) { super(seed0, seed1); } /** * Copy constructor. * * @param source the source to copy */ XBGXoRoShiRo64(XBGXoRoShiRo64 source) { // There is no super-class copy constructor so just construct // from the RNG state. // Warning: // This will not copy the cached state from source. // This only matters if tests use nextBoolean. this(source.state0, source.state1); } @Override public int stateAndUpdate() { final int s0 = state0; next(); return s0; } @Override public SubGen copyAndJump() { // No jump function return new XBGXoRoShiRo64(this); } @Override public SubGen copyAndLongJump() { // No jump function return new XBGXoRoShiRo64(this); } @Override protected int nextOutput() { // Not used return 0; } } /** * A composite LXM generator. Implements: * <pre> * s = state of LCG * t = state of XBG * * generate(): * z <- mix(combine(w upper bits of s, w upper bits of t)) * s <- LCG state update * t <- XBG state update * </pre> * <p>w is assumed to be 32-bits. */ static class LXMGenerator extends IntProvider implements LongJumpableUniformRandomProvider { /** Mix implementation. */ private final Mix mix; /** LCG implementation. */ private final SubGen lcg; /** XBG implementation. */ private final SubGen xbg; /** * Create a new instance. * The jump and long jump of the LCG are assumed to appropriately match those of the XBG. * This can be achieved by an XBG jump that wraps the period of the LCG; or advancing * the LCG and leaving the XBG state unchanged, effectively rewinding the LXM generator. * * @param mix Mix implementation * @param lcg LCG implementation * @param xbg XBG implementation */ LXMGenerator(Mix mix, SubGen lcg, SubGen xbg) { this.lcg = lcg; this.xbg = xbg; this.mix = mix; } @Override public int next() { return mix.apply(lcg.stateAndUpdate(), xbg.stateAndUpdate()); } @Override public LXMGenerator jump() { return new LXMGenerator(mix, lcg.copyAndJump(), xbg.copyAndJump()); } @Override public LXMGenerator longJump() { return new LXMGenerator(mix, lcg.copyAndLongJump(), xbg.copyAndLongJump()); } } /** * A factory for creating LXMGenerator objects. */ interface LXMGeneratorFactory { /** * Return the size of the LCG long seed array. * * @return the LCG seed size */ int lcgSeedSize(); /** * Return the size of the XBG long seed array. * * @return the XBG seed size */ int xbgSeedSize(); /** * Return the size of the long seed array. * The default implementation adds the size of the LCG and XBG seed. * * @return the seed size */ default int seedSize() { return lcgSeedSize() + xbgSeedSize(); } /** * Creates a new LXMGenerator. * * <p>Tests using the LXMGenerator assume the seed is a composite containing the * LCG seed and then the XBG seed. * * @param seed the seed * @return the generator */ LXMGenerator create(int[] seed); /** * Gets the mix implementation. This is used to test initial output of the generator. * The default implementation is {@link AbstractLXMTest#mixLea32(int, int)}. * * @return the mix */ default Mix getMix() { return AbstractLXMTest::mixLea32; } } /** * Test the LCG implementations. These tests should execute only once, and not * for each instance of the abstract outer class (i.e. the test is not {@code @Nested}). * * <p>Note: The LCG implementation is not present in the main RNG core package and * this test ensures an LCG update of: * <pre> * s = m * s + a * * s = state * m = multiplier * a = addition * </pre> * * <p>A test is made to ensure the LCG can perform jump and long jump operations using * small jumps that can be verified by an equal number of single state updates. */ static class LCGTest { /** A count {@code k} where a jump of {@code 2^k} will wrap the LCG state. */ private static final int NO_JUMP = -1; /** A count {@code k=2} for a jump of {@code 2^k}, or 4 cycles. */ private static final int JUMP = 2; /** A count {@code k=4} for a long jump of {@code 2^k}, or 16 cycles. */ private static final int LONG_JUMP = 4; @RepeatedTest(value = 10) void testLCG32DefaultJump() { final SplittableRandom rng = new SplittableRandom(); final int state = rng.nextInt(); final int add = rng.nextInt(); final SubGen lcg1 = new LCG32(add, state); final SubGen lcg2 = new LCG32(add, state, 0, 16); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } lcg1.copyAndJump(); lcg2.copyAndJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } lcg1.copyAndLongJump(); lcg2.copyAndLongJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } } @RepeatedTest(value = 10) void testLCG32() { final SplittableRandom rng = new SplittableRandom(); final int state = rng.nextInt(); final int add = rng.nextInt(); int s = state; final int a = add | 1; final SubGen lcg = new LCG32(add, state, NO_JUMP, NO_JUMP); for (int j = 0; j < 10; j++) { Assertions.assertEquals(s, lcg.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); s = LXMSupport.M32 * s + a; } } @RepeatedTest(value = 10) void testLCG32Jump() { final SplittableRandom rng = new SplittableRandom(); final int state = rng.nextInt(); final int add = rng.nextInt(); final Supplier<String> msg = () -> String.format("seed %d,%d", state, add); int s = state; final int a = add | 1; final SubGen lcg = new LCG32(add, state, JUMP, LONG_JUMP); final SubGen copy1 = lcg.copyAndJump(); for (int j = 1 << JUMP; j-- != 0;) { Assertions.assertEquals(s, copy1.stateAndUpdate(), msg); s = LXMSupport.M32 * s + a; } Assertions.assertEquals(s, lcg.stateAndUpdate(), msg); s = LXMSupport.M32 * s + a; final SubGen copy2 = lcg.copyAndLongJump(); for (int j = 1 << LONG_JUMP; j-- != 0;) { Assertions.assertEquals(s, copy2.stateAndUpdate(), msg); s = LXMSupport.M32 * s + a; } Assertions.assertEquals(s, lcg.stateAndUpdate(), msg); } } /** * Test the XBG implementations. These tests should execute only once, and not * for each instance of the abstract outer class (i.e. the test is not {@code @Nested}). * * <p>Note: The XBG implementation are extensions of RNGs already verified against * reference implementations. These tests ensure that the upper bits of the state is * identical after {@code n} cycles then a jump; or a jump then {@code n} cycles. * This is the functionality required for a jumpable XBG generator. */ static class XBGTest { @RepeatedTest(value = 5) void testXBGXoRoShiRo64NoJump() { final SplittableRandom r = new SplittableRandom(); assertNoJump(new XBGXoRoShiRo64(r.nextInt(), r.nextInt())); } void assertNoJump(SubGen g) { final SubGen g1 = g.copyAndJump(); Assertions.assertNotSame(g, g1); for (int i = 0; i < 10; i++) { Assertions.assertEquals(g.stateAndUpdate(), g1.stateAndUpdate()); } final SubGen g2 = g.copyAndLongJump(); Assertions.assertNotSame(g, g2); for (int i = 0; i < 10; i++) { Assertions.assertEquals(g.stateAndUpdate(), g2.stateAndUpdate()); } } // Note: These test are duplicates of the XBGTest in the source64 package // which does have jumpable XBG sub-generators. The tests work even when // the jump is not implemented and is a simple copy operation. @RepeatedTest(value = 5) void testXBGXoRoShiRo64Jump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 2, XBGXoRoShiRo64::new, SubGen::copyAndJump); } @RepeatedTest(value = 5) void testXBGXoRoShiRo64LongJump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 2, XBGXoRoShiRo64::new, SubGen::copyAndLongJump); } /** * Assert alternating jump and cycle of the XBG. * * <p>This test verifies one generator can jump and advance {@code n} steps * while the other generator advances {@code n} steps then jumps. The two should * be in the same state. The copy left behind by the first jump should match the * state of the other generator as it cycles. * * @param testSeed A seed for the test * @param seedSize The seed size * @param factory Factory to create the XBG * @param jump XBG jump function */ private static void assertJumpAndCycle(long testSeed, int seedSize, Function<int[], SubGen> factory, UnaryOperator<SubGen> jump) { final SplittableRandom r = new SplittableRandom(testSeed); final int[] seed = r.ints(seedSize).toArray(); final int[] steps = createSteps(r, seedSize); final int cycles = 2 * seedSize; final SubGen rng1 = factory.apply(seed); final SubGen rng2 = factory.apply(seed); // Try jumping and stepping with a number of steps up to the seed size for (int i = 0; i < steps.length; i++) { final int step = steps[i]; final int index = i; // Jump 1 final SubGen copy = jump.apply(rng1); // Advance 2; it should match the copy for (int j = 0; j < step; j++) { Assertions.assertEquals(rng2.stateAndUpdate(), copy.stateAndUpdate(), () -> String.format("[%d] Incorrect trailing copy, seed=%d", index, testSeed)); // Advance in parallel rng1.stateAndUpdate(); } // Catch up 2; it should match 1 jump.apply(rng2); for (int j = 0; j < cycles; j++) { Assertions.assertEquals(rng1.stateAndUpdate(), rng2.stateAndUpdate(), () -> String.format("[%d] Incorrect after catch up jump, seed=%d", index, testSeed)); } } } } ///////////////////////////////////// // Tests for the LXM implementation ///////////////////////////////////// /** * Gets the factory to create a composite LXM generator. * The generator is used to provide reference output for the generator under test. * * @return the factory */ abstract LXMGeneratorFactory getFactory(); /** * Creates a new instance of the RNG under test. * * @param seed the seed * @return the RNG */ abstract LongJumpableUniformRandomProvider create(int[] seed); /** * Gets a stream of reference data. Each Argument consist of the int array seed and * the int array of the expected output from the LXM generator. * <pre> * int[] seed; * int[] expected; * return Stream.of(Arguments.of(seed, expected)); * </pre> * * @return the reference data */ abstract Stream<Arguments> getReferenceData(); /** * Test the reference data with the LXM generator. * The reference data should be created with a reference implementation of the * LXM algorithm, for example the implementations in JDK 17. */ @ParameterizedTest @MethodSource(value = "getReferenceData") final void testReferenceData(int[] seed, int[] expected) { RandomAssert.assertEquals(expected, create(seed)); } /** * Test the reference data with the composite LXM generator. This ensures the composite * correctly implements the LXM algorithm. */ @ParameterizedTest @MethodSource(value = "getReferenceData") final void testReferenceDataWithComposite(int[] seed, int[] expected) { RandomAssert.assertEquals(expected, getFactory().create(seed)); } /** * Test initial output is the mix of the most significant bits of the LCG and XBG state. * This test ensures the LXM algorithm applies the mix function to the current state * before state update. Thus the initial output should be a direct mix of the seed state. */ @RepeatedTest(value = 5) final void testInitialOutput() { final int[] seed = createRandomSeed(); // Upper 32 bits of LCG state final int s = seed[getFactory().lcgSeedSize() / 2]; // Upper 32 bits of XBG state final int t = seed[getFactory().lcgSeedSize()]; Assertions.assertEquals(getFactory().getMix().apply(s, t), create(seed).nextInt()); } @Test final void testConstructorWithZeroSeedIsPartiallyFunctional() { // Note: It is impractical to demonstrate the RNG is partially functional: // output quality may be statistically poor and the period is that of the LCG. // The test demonstrates the RNG is not totally non-functional with a zero seed // which is not the case for a standard XBG. final int seedSize = getFactory().seedSize(); RandomAssert.assertNextLongNonZeroOutput(create(new int[seedSize]), 0, seedSize); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testConstructorWithoutFullLengthSeed(int[] seed) { // Hit the case when the input seed is self-seeded when not full length final int seedSize = getFactory().seedSize(); RandomAssert.assertNextIntNonZeroOutput(create(new int[] {seed[0]}), seedSize, seedSize); } @RepeatedTest(value = 5) final void testConstructorIgnoresFinalAddParameterSeedBit() { final int[] seed1 = createRandomSeed(); final int[] seed2 = seed1.clone(); final int seedSize = getFactory().seedSize(); // seed1 unset final add parameter bit; seed2 set final bit final int index = getFactory().lcgSeedSize() / 2 - 1; seed1[index] &= -1 << 1; seed2[index] |= 1; Assertions.assertEquals(1, seed1[index] ^ seed2[index]); final LongJumpableUniformRandomProvider rng1 = create(seed1); final LongJumpableUniformRandomProvider rng2 = create(seed2); RandomAssert.assertNextLongEquals(seedSize * 2, rng1, rng2); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testJump(int[] seed, int[] expected) { final int[] expectedAfter = createExpectedSequence(seed, expected.length, false); RandomAssert.assertJumpEquals(expected, expectedAfter, create(seed)); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testLongJump(int[] seed, int[] expected) { final int[] expectedAfter = createExpectedSequence(seed, expected.length, true); RandomAssert.assertLongJumpEquals(expected, expectedAfter, create(seed)); } @RepeatedTest(value = 5) final void testJumpAndOutput() { assertJumpAndOutput(false, ThreadLocalRandom.current().nextLong()); } @RepeatedTest(value = 5) final void testLongJumpAndOutput() { assertJumpAndOutput(true, ThreadLocalRandom.current().nextLong()); } /** * Assert alternating jump and output from the LXM generator and the * reference composite LXM generator. * * <p>The composite generator uses an LCG that is tested to match * output after a jump (see {@link LCGTest} or a series of cycles. * The XBG generator is <em>assumed</em> to function similarly. * The {@link XBGTest} verifies that the generator is in the same * state when using {@code n} steps then a jump; or a jump then * {@code n} steps. * * <p>This test verifies one LXM generator can jump and advance * {@code n} steps while the other generator advances {@code n} * steps then jumps. The two should be in the same state. The copy * left behind by the first jump should match the output of the other * generator as it cycles. * * @param longJump If true use the long jump; otherwise the jump * @param testSeed A seed for the test */ private void assertJumpAndOutput(boolean longJump, long testSeed) { final SplittableRandom r = new SplittableRandom(testSeed); final int[] seed = createRandomSeed(r::nextInt); final int[] steps = createSteps(r); final int cycles = getFactory().seedSize(); LongJumpableUniformRandomProvider rng1 = create(seed); LongJumpableUniformRandomProvider rng2 = getFactory().create(seed); final UnaryOperator<LongJumpableUniformRandomProvider> jump = longJump ? x -> (LongJumpableUniformRandomProvider) x.longJump() : x -> (LongJumpableUniformRandomProvider) x.jump(); // Alternate jumping and stepping then stepping and jumping. for (int i = 0; i < steps.length; i++) { final int step = steps[i]; final int index = i; // Jump 1 LongJumpableUniformRandomProvider copy = jump.apply(rng1); // Advance 2; it should match the copy for (int j = 0; j < step; j++) { Assertions.assertEquals(rng2.nextInt(), copy.nextInt(), () -> String.format("[%d] Incorrect trailing copy, seed=%d", index, testSeed)); // Advance 1 in parallel rng1.nextInt(); } // Catch up 2; it should match 1 jump.apply(rng2); for (int j = 0; j < cycles; j++) { Assertions.assertEquals(rng1.nextInt(), rng2.nextInt(), () -> String.format("[%d] Incorrect after catch up jump, seed=%d", index, testSeed)); } // Swap copy = rng1; rng1 = rng2; rng2 = copy; } } @RepeatedTest(value = 5) final void testSaveRestoreAfterJump() { assertSaveRestoreAfterJump(false, ThreadLocalRandom.current().nextLong()); } @RepeatedTest(value = 5) final void testSaveRestoreAfterLongJump() { assertSaveRestoreAfterJump(true, ThreadLocalRandom.current().nextLong()); } /** * Assert that the precomputation of the jump coefficients functions * with saved/restore. * * @param longJump If true use the long jump; otherwise the jump * @param testSeed A seed for the test */ private void assertSaveRestoreAfterJump(boolean longJump, long testSeed) { final SplittableRandom r = new SplittableRandom(testSeed); final int cycles = getFactory().seedSize(); final UnaryOperator<LongJumpableUniformRandomProvider> jump = longJump ? x -> (LongJumpableUniformRandomProvider) x.longJump() : x -> (LongJumpableUniformRandomProvider) x.jump(); // Create 2 generators and jump them final LongJumpableUniformRandomProvider rng1 = create(createRandomSeed(r::nextInt)); final LongJumpableUniformRandomProvider rng2 = create(createRandomSeed(r::nextInt)); jump.apply(rng1); jump.apply(rng2); // Restore the state from one to the other RestorableUniformRandomProvider g1 = (RestorableUniformRandomProvider) rng1; RestorableUniformRandomProvider g2 = (RestorableUniformRandomProvider) rng2; g2.restoreState(g1.saveState()); // They should have the same output RandomAssert.assertNextLongEquals(cycles, rng1, rng2); // They should have the same output after a jump too jump.apply(rng1); jump.apply(rng2); RandomAssert.assertNextLongEquals(cycles, rng1, rng2); } /** * Create the expected sequence after a jump by advancing the XBG and LCG * sub-generators. This is done by creating and jumping the composite LXM * generator. * * @param seed the seed * @param length the length of the expected sequence * @param longJump If true use the long jump; otherwise the jump * @return the expected sequence */ private int[] createExpectedSequence(int[] seed, int length, boolean longJump) { final LXMGenerator rng = getFactory().create(seed); if (longJump) { rng.longJump(); } else { rng.jump(); } return IntStream.generate(rng::nextInt).limit(length).toArray(); } /** * Creates a random seed of the correct length. Used when the seed can be anything. * * @return the seed */ private int[] createRandomSeed() { return createRandomSeed(new SplittableRandom()::nextInt); } /** * Creates a random seed of the correct length using the provided generator. * Used when the seed can be anything. * * @param gen the generator * @return the seed */ private int[] createRandomSeed(IntSupplier gen) { return IntStream.generate(gen).limit(getFactory().seedSize()).toArray(); } /** * Creates a random permutation of steps in [1, seed size]. * The seed size is obtained from the LXM generator factory. * * @param rng Source of randomness * @return the steps */ private int[] createSteps(SplittableRandom rng) { return createSteps(rng, getFactory().seedSize()); } /** * Creates a random permutation of steps in [1, seed size]. * * @param rng Source of randomness * @param seedSize Seed size * @return the steps */ private static int[] createSteps(SplittableRandom rng, int seedSize) { final int[] steps = IntStream.rangeClosed(1, seedSize).toArray(); // Fisher-Yates shuffle for (int i = steps.length; i > 1; i--) { final int j = rng.nextInt(i); final int tmp = steps[i - 1]; steps[i - 1] = steps[j]; steps[j] = tmp; } return steps; } }
3,049
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/L32X64MixTest.java
/* * 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.rng.core.source32; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L32X64Mix}. */ class L32X64MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 2; } @Override public int xbgSeedSize() { return 2; } @Override public LXMGenerator create(int[] seed) { return new LXMGenerator(getMix(), new LCG32(seed[0], seed[1]), new XBGXoRoShiRo64(seed[2], seed[3])); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(int[] seed) { return new L32X64Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 17: * java.util.random.RandomGeneratorFactory.of("L32X64MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to int[] by filling the long bits sequentially starting at the most * significant byte matching the method used by JDK 17. A sign extension bug in * byte[] conversion required all byte indices (i % 4 != 0) to be non-negative. * See: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8282144 * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new int[] { 0x5a16253f, 0xd449657e, 0x5b46012d, 0x1d504d64, }, new int[] { 0xa6cc8f9b, 0xb9d3f0e3, 0xb8861d42, 0x9f8001a2, 0xaf1eea5b, 0x4e3bc947, 0x1c6378b8, 0x54ccc942, 0x4629da71, 0x0126a7e7, 0x6038f89c, 0xff72cb5c, 0x3853ae58, 0xc86ccf16, 0x02d32147, 0x78afed22, 0x432194a4, 0xf79809aa, 0x8c115507, 0x646b0445, 0xed63cc9c, 0x4e1d7b55, 0x699cf21d, 0x7d2adf74, 0xc2613634, 0x5ac9c96f, 0x1f2e59d2, 0x4c9878fb, 0x49c7a137, 0xd277cdf8, 0x369dd190, 0x1edeb2dc, 0x10bd09bd, 0x86e5140c, 0xe6bcb04c, 0x67f97775, 0x8f979b43, 0x36dfa266, 0x3acf5157, 0xe69aac08, }), Arguments.of( new int[] { 0xa3512c5b, 0x970f3100, 0xfc22313b, 0xa71f183a, }, new int[] { 0x971dc6d7, 0xd8200b05, 0x0093d467, 0x74f11b96, 0x8945aa8f, 0x88a3d635, 0xb1fcb212, 0x66ed8543, 0xd5970056, 0x7b8b2d89, 0xd2d3957b, 0xc4ecc777, 0xf8e295c3, 0x4ffb4a29, 0x509fc6b0, 0x9c5b1965, 0x5008c757, 0x3a19410a, 0xc3992498, 0x0c4a8f22, 0x3f6abfd8, 0xe943ac45, 0xb632fcd3, 0x3d1f3201, 0xb17bc9f5, 0x902a9b9c, 0x39a76388, 0xd8c856a0, 0x7422cf86, 0x3f776592, 0xef70a122, 0x053b628e, 0x0dec8c4e, 0x5b0d80c9, 0xf58d19fc, 0x741f1361, 0x01b747f7, 0x6b325a32, 0x714ed761, 0xf1facf8e, }), Arguments.of( new int[] { 0x17767167, 0x1b6e286c, 0xe84a2f12, 0x637a5034, }, new int[] { 0xd69fd4c1, 0x134945f8, 0x34c2fd4e, 0xd152b08f, 0xb5a7e2bd, 0xd8afab0d, 0xed15f9d2, 0x6baba06e, 0x9a1e8bcd, 0x8b392fbd, 0xb1e1cad9, 0x929f06d9, 0xc9467860, 0x3d492690, 0x84fd7d06, 0xb576b0f9, 0x860eac4c, 0xd3b7aa5b, 0x3ff4dd13, 0xfea00d01, 0x025143a0, 0xf1c5885a, 0x3041a8be, 0xbfbfb908, 0x186cd9cc, 0x1771875b, 0x9cc65fe2, 0xc9189702, 0x6063b09d, 0x20f0286b, 0x6094b83d, 0x8c3fe8f5, 0x2bf720eb, 0xe229e207, 0xe33f93db, 0x443b3849, 0x79cc70a8, 0xcbf0de84, 0xe57c6367, 0xe555ee21, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(int[] seed, int[] expected) { final L32X64Mix rng1 = new L32X64Mix(seed); final L32X64Mix rng2 = new L32X64Mix(seed[0], seed[1], seed[2], seed[3]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final int[] seed = new int[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L32X64Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextIntNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended int z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea32(z); z += LXMSupport.GOLDEN_RATIO_32; } final SplittableUniformRandomProvider rng3 = new L32X64Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextIntEquals(seed.length * 2, rng3, rng4); } }
3,050
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/DotyHumphreySmallFastCounting32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class DotyHumphreySmallFastCounting32Test { @Test void testReferenceCode() { /* * Tested with respect to PractRand::RNGs::Raw::sfc32 of the C++ implementation (v0.94). * See : http://pracrand.sourceforge.net/ */ final int[] expectedSequence = { 0x89b5c414, 0x7ee57639, 0xdbe18f7b, 0x94aa0162, 0xa22bff0a, 0x21c91fb8, 0x2c6fd6fe, 0xcda90d13, 0x019684ca, 0xe5b400c0, 0x459d8590, 0x3aec0a1e, 0x254dac77, 0xbe10ae80, 0x9ac27819, 0xd17d10c6, 0x71a69026, 0x4bb2bdda, 0x70853646, 0xda28f272, 0x879200d9, 0x8c2f8b5b, 0x8a87cb78, 0x27ffdced, 0x988a2b7b, 0xf220ef9b, 0x13b8984f, 0x345d1732, 0x8f5bc6fc, 0x092b09ff, 0x046bd2b0, 0xa5a99fc5, 0x19400604, 0xb76e7394, 0x037addd5, 0xe916ed79, 0x94f10dc6, 0xf2ecb45e, 0x69834355, 0xb814aeb2, }; RandomAssert.assertEquals(expectedSequence, new DotyHumphreySmallFastCounting32(new int[] { 0xbb3c4104, 0x02294965, 0xda1ce2a9 })); } }
3,051
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/PcgMcgXshRs32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class PcgMcgXshRs32Test { @Test void testReferenceCode() { /* * Tested with respect to pcg_engines::mcg_xsh_rs_64_32 of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0xb786f832, 0x6920834f, 0x5b88b399, 0x6b811447, 0x91230c70, 0x163c83b5, 0x8dd8bba9, 0xb8bcd10a, 0xe1964b6e, 0x40b9adc8, 0x75fbee87, 0xed3d1e5c, 0x82cb437b, 0xea94cea8, 0x76b1726a, 0x9275544a, 0xed015249, 0x9d46c1cc, 0xe6fddd59, 0x487a0912, 0xa709c922, 0xd15ac2a2, 0xba36e687, 0x3e40b099, 0x62ae602c, 0xec0ebb27, 0x94246eda, 0xa40c2daa, 0xd7e0abb5, 0xf8061587, 0x97f2132a, 0x861cfa5e, 0xc5b2280b, 0x5fc8ec4e, 0xa9e552ed, 0xbf2ee34f, 0x0a945eb3, 0x9e578662, 0x292cf72c, 0xc7e04668, }; RandomAssert.assertEquals(expectedSequence, new PcgMcgXshRs32(0x012de1babb3c4104L)); } }
3,052
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusPlusTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo128PlusPlusTest { /** The size of the array seed. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro128plusplus.c */ private static final int[] SEED = { 0x012de1ba, 0xa5a818b8, 0xb124ea2b, 0x18e03749, }; private static final int[] EXPECTED_SEQUENCE = { 0x083a6347, 0xaf13e949, 0xc170e7f6, 0x1fff4fb2, 0x683f45ee, 0x0447edcf, 0x42e85ced, 0xaf636b74, 0xb0087a5e, 0x75bf2669, 0xd1bce8bd, 0x421fc05e, 0x1c6c405f, 0x14ddbffd, 0xaeacb705, 0x977ae584, 0x2ac01aac, 0xc474ec71, 0xe0888022, 0xf94bc227, 0x32775b57, 0x44142b05, 0x525f6d9b, 0xa2721e61, 0x1bfe5c72, 0x17be23c2, 0x3231cc54, 0x8776866e, 0x9ede2587, 0x0f7f144e, 0xb6f2ff9d, 0x1556365b, 0x9e68aef3, 0x254010c3, 0x0b885bdd, 0x7c3f26bb, 0xc8266de6, 0xcd2e6587, 0x0cbec249, 0xa69b37ba, }; private static final int[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x4485d85f, 0x43f4d8a7, 0xe21ea064, 0x3eddd57d, 0x44f6149a, 0xde7e1c16, 0xa7410410, 0x6360a4a9, 0x34dab153, 0xfdf089b0, 0xa9b78551, 0xa2136cee, 0x0ad2126f, 0x67a62b78, 0xa3e8c1fa, 0x19eed39b, 0x83357624, 0xde015e70, 0xdc670b3b, 0x833dc245, 0x4d644c84, 0x30e7ea9f, 0x9dd22362, 0x70978ced, 0xf3d07dbb, 0xfdad08e5, 0x9118ebe0, 0x5dc55edf, 0xcf9abe08, 0x7a822c3b, 0xa1115ecf, 0x9f8cc327, 0x452c8954, 0xf920ef83, 0xcff75ece, 0x9622a70d, 0x6202e501, 0x10ae0703, 0x8b7ee1be, 0xc72c1cf6, }; private static final int[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x27906b5a, 0xe2ce9fb2, 0xd97f8c4f, 0x7609af7d, 0x5b91ddd8, 0x4134b769, 0x47f505f2, 0xacc18832, 0xeea7faf6, 0x50178ca9, 0xc15f4b36, 0xcbd206e6, 0x4f2273cb, 0xebaabeef, 0x51e6d76f, 0xaf1fdde8, 0x3cb5ced1, 0x04b42264, 0x2396256f, 0x9c0618ff, 0x95ecbb0c, 0xc88c952c, 0x820664ab, 0x5d2e6153, 0xb2003213, 0x71531ff6, 0x99d4bd53, 0xbd15fcc1, 0x90ad002b, 0x3d37b45d, 0x500b49db, 0x6f81b35f, 0x533f3bab, 0xe22a25fe, 0x114ca833, 0x4ab45586, 0x077ca93d, 0xd5cf0025, 0xbe019f55, 0x8ecbe4a8, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo128PlusPlus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo128PlusPlus(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo128PlusPlus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo128PlusPlus(new int[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo128PlusPlus rng1 = new XoShiRo128PlusPlus(SEED); final XoShiRo128PlusPlus rng2 = new XoShiRo128PlusPlus(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextIntEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo128PlusPlus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo128PlusPlus(SEED)); } }
3,053
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128StarStarTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo128StarStarTest { /** The size of the array seed. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro128starstar.c */ private static final int[] SEED = { 0x012de1ba, 0xa5a818b8, 0xb124ea2b, 0x18e03749, }; private static final int[] EXPECTED_SEQUENCE = { 0x8856d912, 0xf2a19a86, 0x7693f66d, 0x23516f86, 0x4895054e, 0xf4503fe6, 0x40e04672, 0x99244e34, 0xb971815c, 0x3008b82c, 0x0ee73b58, 0x88aad2c6, 0x7923f2e9, 0xfde55485, 0x7aed95f5, 0xeb8abb59, 0xca78183a, 0x80ecdd68, 0xfd404b06, 0x248b9c9e, 0xa2c69c6f, 0x1723b375, 0x879f37b0, 0xe98fd208, 0x75de84a9, 0x717d6df8, 0x92cd7bc7, 0x46380167, 0x7f08600b, 0x58566f2b, 0x7f781475, 0xe34ec04d, 0x6d5ef889, 0xb76ff6d8, 0x501f5df6, 0x4cf70ccb, 0xd7375b26, 0x457ea1ab, 0x7439e565, 0x355855af, }; private static final int[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xab597786, 0x9e11647e, 0x147c0f8e, 0xfe86b079, 0x8cda108b, 0x461f24ad, 0x00204529, 0xdfa240cf, 0xcc697f88, 0x11a49521, 0x5e6eb377, 0xe7dad980, 0xc522beed, 0x54049c0d, 0x5409c7de, 0x22148129, 0x2d7fc96e, 0x288d7114, 0xfb18c495, 0x689c4e2b, 0xd7219504, 0xb2d81d4d, 0xe66c9680, 0xeec149b3, 0x82fad922, 0x49e59804, 0x7be8f245, 0xbc193d57, 0xa542f0d5, 0x07474d51, 0xff3c7b3d, 0x2eda9beb, 0xca7657a3, 0xcf58554d, 0x5fe25af7, 0x5beb7d19, 0x58339082, 0x6f7ac9ed, 0xf07faa96, 0x7348dcbf, }; private static final int[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x84ea405d, 0xe43ec9b9, 0x7b43546a, 0x5aeca3cb, 0x54ec4005, 0x90511268, 0x63a1d86b, 0x56e93375, 0x64a6fd02, 0x559acfe7, 0xf4f18034, 0x70c3ae88, 0xfc5d0b08, 0xecba359e, 0x00784b22, 0x48627c78, 0xa971ad76, 0x07d938c2, 0x4db234d7, 0xcafbf946, 0x6b716c5d, 0xc0275fc2, 0x158f8407, 0x4e41c342, 0x1480ac03, 0x6932767a, 0x31eed7c1, 0x9cee78df, 0x2c5d98f5, 0x04d1aab9, 0xbd1a4b49, 0xa40820b9, 0x9384d31f, 0x35dab84f, 0xd6067813, 0xa45e9b4e, 0x13ec0f47, 0x1f3df575, 0x358f3a61, 0xf210c90e, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo128StarStar(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo128StarStar(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo128StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo128StarStar(new int[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo128StarStar rng1 = new XoShiRo128StarStar(SEED); final XoShiRo128StarStar rng2 = new XoShiRo128StarStar(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextIntEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo128StarStar(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo128StarStar(SEED)); } }
3,054
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoShiRo128PlusTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo128PlusTest { /** The size of the array seed. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro128plus.c */ private static final int[] SEED = { 0x012de1ba, 0xa5a818b8, 0xb124ea2b, 0x18e03749, }; private static final int[] EXPECTED_SEQUENCE = { 0x1a0e1903, 0xfde55c35, 0xddb16b2e, 0xab949ac5, 0xb5519fea, 0xc6a97473, 0x1f0403d9, 0x1bb46995, 0x79c99a12, 0xe447ebce, 0xa8c31d78, 0x54d8bbe3, 0x4984a039, 0xb411e932, 0x9c1f2c5e, 0x5f53c469, 0x7f333552, 0xb368c7a1, 0xa57b8e66, 0xb29a9444, 0x5c389bfa, 0x8e7d3758, 0xfe17a1bb, 0xcd0aad57, 0xde83c4bb, 0x1402339d, 0xb557a080, 0x4f828bc9, 0xde14892d, 0xbba8eaed, 0xab62ebbb, 0x4ad959a4, 0x3c6ee9c7, 0x4f6a6fd3, 0xd5785eed, 0x1a0227d1, 0x81314acb, 0xfabdfb97, 0x7e1b7e90, 0x57544e23, }; private static final int[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x65ddc942, 0x7e7c4d6b, 0x6745a785, 0x40897788, 0xfb60ce92, 0x121f2ee0, 0xd000bae8, 0x52b3ebfc, 0x62fc3720, 0xf880f092, 0x7753c1ab, 0x1e76a627, 0xe5de31e8, 0xc7b1503f, 0xa5557a66, 0x37b2b2cd, 0x656dde58, 0xdd5f1b93, 0xba61298b, 0xbd5d1ce2, 0xea4a5a73, 0x0f10981d, 0xc207a68c, 0x1897adca, 0x4d729b07, 0xf0115ee0, 0x953d9e4b, 0x3608e61c, 0x0c14c065, 0xf2ed7579, 0xcd96ef9b, 0xdb62d117, 0x844e4713, 0x763a8a76, 0x9ad37470, 0x211e4883, 0xc8682b75, 0xb1831941, 0xf0c50a84, 0x7321dc33, }; private static final int[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x93572bc7, 0xc46a6c83, 0x5803ee73, 0x525cc155, 0x45d27ce5, 0xfffbdf72, 0x764d4d47, 0xe94e5ee4, 0xf91b0e1f, 0x63138833, 0xb63f1a97, 0x5cf78346, 0xad979a8f, 0xcf4c0d3f, 0xccfb1798, 0xff978ea1, 0xc7744b7c, 0xfcae345e, 0x40618bc9, 0x55f2ffd7, 0x869ad599, 0x0101eba4, 0x5091a478, 0xc82b9461, 0x940e4e36, 0x49f41fbe, 0x6005f4af, 0x6cf46dab, 0x2de3bc75, 0x06530e45, 0x839ef4b3, 0xd2510032, 0x6053afee, 0xe67eb5e8, 0x2f25e700, 0x2de3212b, 0x41cf6954, 0xa66d8fd8, 0x6c348704, 0xb16b8da5, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo128Plus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo128Plus(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo128Plus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo128Plus(new int[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo128Plus rng1 = new XoShiRo128Plus(SEED); final XoShiRo128Plus rng2 = new XoShiRo128Plus(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextIntEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo128Plus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo128Plus(SEED)); } }
3,055
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarStarTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo64StarStarTest { /** The size of the array seed. */ private static final int SEED_SIZE = 2; @Test void testReferenceCode() { /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro64starstar.c */ final int[] seed = { 0x012de1ba, 0xa5a818b8, }; final int[] expectedSequence = { 0x7ac00b42, 0x1f638399, 0x09e4aea4, 0x05cbbd64, 0x1c967b7b, 0x1cf852fd, 0xc666f4e8, 0xeea9f1ae, 0xca0fa6bc, 0xa65d0905, 0xa69afc95, 0x34965e62, 0xdd4f04a9, 0xff1c9342, 0x638ff769, 0x03419ca0, 0xb46e6dfd, 0xf7555b22, 0x8cab4e68, 0x5a44b6ee, 0x4e5e1eed, 0xd03c5963, 0x782d05ed, 0x41bda3e3, 0xd1d65005, 0x88f43a8a, 0xfffe02ea, 0xb326624a, 0x1ec0034c, 0xb903d8df, 0x78454bd7, 0xaec630f8, 0x2a0c9a3a, 0xc2594988, 0xe71e767e, 0x4e0e1ddc, 0xae945004, 0xf178c293, 0xa04081d6, 0xdd9c062f, }; RandomAssert.assertEquals(expectedSequence, new XoRoShiRo64StarStar(seed)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64StarStar(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo64StarStar(new int[] {0x012de1ba}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final int[] seed = { 0x012de1ba, 0xa5a818b8, }; final XoRoShiRo64StarStar rng1 = new XoRoShiRo64StarStar(seed); final XoRoShiRo64StarStar rng2 = new XoRoShiRo64StarStar(seed[0], seed[1]); RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } }
3,056
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well512aTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well512aTest { /** The size of the array seed. */ private static final int SEED_SIZE = 16; @Test void testReferenceCode() { final Well512a rng = new Well512a(new int[] { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0 }); final int[] expectedSequence = { 0x61714569, 0x6fdd4898, 0xc5f47050, 0x810a6b1c, 0xc1af8b8c, 0x92a98227, 0x64cb1d8d, 0x24578071, 0x793b296a, 0x212298db, 0x0065f1a0, 0x74b6a737, 0x6f40110d, 0xa84d2dfb, 0x7a23fc8a, 0xf4895b35, 0x85732a6e, 0x30550ad9, 0x53bf4e25, 0xd16e0c02, 0x3c5dd4e7, 0xc1b55438, 0x2b6a28a4, 0x57d400ec, 0x115082ee, 0x13788a09, 0x784cd04b, 0x90240346, 0x49e86af5, 0x574c6790, 0x78b55a42, 0xf9f4cc61, 0x52b9b459, 0x0c92fddf, 0x5170fd1e, 0xe39997a7, 0x09215785, 0xa02d7d5d, 0xe1cbb539, 0x0110f5e2, 0x12b53330, 0xebb4504e, 0x22f816af, 0x4a2158af, 0x2ce457f5, 0xd9733322, 0xd3f0dc69, 0xf379d207, 0x915a36ba, 0xda8d94ca, 0x93f2e50b, 0x546ad758, 0x40407b6a, 0xcd862700, 0x523ed8a9, 0xdd6b1ad0, 0xffe3a271, 0x0c917702, 0x11a22e90, 0x58f9efd1, 0xaba08480, 0xeb71b156, 0x8c338ac3, 0x5c5ba7f6, 0xe8c8046f, 0x12bb238a, 0x00b3d31c, 0xb1d02bc8, 0x667db6d9, 0xd26cf82b, 0x4d4b6dea, 0xc4b5f996, 0x2eb8abe3, 0x193130c0, 0x171ae727, 0x48b2f7e9, 0x7fd0393c, 0xce46ebf6, 0xc2377f2a, 0x37c8d867, 0xde1bafe6, 0x4f200655, 0xdce597c6, 0x71d94a5a, 0xfd90a771, 0xf525efbe, 0x4da61ac2, 0x2043c247, 0x9dcf808e, 0x2ffebfb3, 0x99689f79, 0x5a829736, 0xc7371b28, 0xe62cba9c, 0x1e6106f6, 0xd1abf44e, 0x4c29d5ba, 0x10ee5570, 0x37cadeda, 0x7967a7ff, 0x749c7c0a, 0xb948ed30, 0x112768c8, 0x7f46436b, 0x2af1be27, 0x5972c9f0, 0xef3642f9, 0xb3d43956, 0x9ee7e9de, 0x4a13d96f, 0x86f5f652, 0x57d97072, 0x1a4647ed, 0xaf1d4e0d, 0x01b11b4c, 0x5f87bcd0, 0xd9c4026e, 0xdbb29175, 0x25e7a68c, 0x57caa480, 0x58079f5b, 0xd36ad1d6, 0xa13561e1, 0xcb4b8944, 0xe8c518fb, 0x84cc51c7, 0x40cbe128, 0xb64d53ae, 0x01e30df7, 0x6d6acb68, 0xe4142fda, 0x80e2998c, 0xe61863ad, 0x0f657837, 0x6e6228e9, 0x9ed60ce9, 0xafdac4e7, 0x755fabf7, 0x24ec4206, 0xf59afad0, 0x0c5dd31f, 0x9f736292, 0x4f275f0a, 0xfafebf4e, 0x1a94af26, 0x958848f3, 0x005fd078, 0xdd5eea29, 0x5c6cf930, 0x77994735, 0x106f8cd7, 0x1b2912e7, 0x78613865, 0x427de838, 0xb1bb6096, 0xe13b3537, 0x04655f8d, 0xad998410, 0x17c1feb4, 0x4eabe4b0, 0xa0e57f92, 0x5d658608, 0x988b897c, 0xa6062a85, 0x56e9f7d8, 0xdf40e295, 0xdb1d72e0, 0x5a65da93, 0x714b40c3, 0xa952a890, 0x398cbcef, 0x1d1bb4f9, 0x9094d514, 0xcfae96f0, 0x853f9e65, 0x56b44baf, 0x4f38c51d, 0xf2291fd4, 0xc4b18d31, 0x6ea0056e, 0xe50dd70c, 0x5d51a093, 0x2d4a45da, 0x49fecc33, 0xad1f5301, 0x7403b755, 0x86dd42e5, 0x28bff1a9, 0x35ff1b42, 0x7636ebc2, 0x07273732, 0x1f5a65e1, 0x8bf4f088, 0x0c3820bb, 0xb214f04c, 0x7f042fd2, 0x7a5c38a1, 0x0b50b9ab, 0x6aa9449f, 0x1835a535, 0xbbbdd7d5, 0x0081d11b, 0x4c205acf, 0x58f78f22, 0x77cb6a1b, 0x9effab72, 0xcd4683d9, 0x93075934, 0x74fb3e2a, 0x7f93e2af, 0x0e1f57fd, 0xebe628f4, 0x4b4e3ee7, 0x1ecd5d44, 0xebfcff9f, 0x52edfbe1, 0x1b2db0f1, 0xa91fc938, 0xd8cdb99f, 0x1cf26705, 0x6907c2f7, 0x47389180, 0x30bf2bb9, 0x13245cd2, 0x07d1548e, 0x114d497e, 0x902066bc, 0x9679d297, 0x4cfc4c1d, 0xc42d9fa2, 0xa1cfe127, 0x5a9fcc85, 0xe8352005, 0xfe8cfa54, 0x1b0c8554, 0xd8b570cd, 0xf3bdecf1, 0x1bcea156, 0x5617c091, 0x46383f89, 0xbb057aa6, 0xdc183254, 0xae4d4105, 0x60415d33, 0x6545c6ab, 0x6f25e281, 0x709080d6, 0x9967e49e, 0x6c0cc73f, 0x27dd6ad3, 0xee47d20f, 0x7ef804a5, 0xba4908fc, 0x36692450, 0x871b5058, 0x4a308706 }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well512a(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(Well512a.class, SEED_SIZE); } }
3,057
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well19937aTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well19937aTest { /** The size of the array seed. */ private static final int SEED_SIZE = 624; @Test void testReferenceCode() { final int[] base = { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0, 0xdc8174cf, 0x3e73a4bc, 0x6fce0775, 0x9e6141fc, 0x5232218a, 0x0fa9e601, 0x0b6fdb4a, 0xf10a0a8c, 0x97829dba, 0xc60b0778, 0x0566db41, 0x620807aa, 0x599b89c9, 0x1a34942b, 0x6baae3da, 0x4ba0b73d }; final int[] seed = new int[624]; for (int i = 0; i < seed.length; ++i) { seed[i] = base[i % base.length] + i; } final Well19937a rng = new Well19937a(seed); final int[] expectedSequence = { 0xdb784719, 0xead77ddc, 0x926f567b, 0x95cf8aff, 0x1097e8d5, 0x486a7ca8, 0x97893622, 0xd2a69352, 0x99e777cb, 0xf19f7dea, 0xf7558586, 0x1d8b0188, 0xe1570edd, 0xb43dd614, 0x9e3f95f3, 0x329ee317, 0xdf3bcb8e, 0xb80fe779, 0xdf761444, 0x3064ffba, 0x9cb6fb9f, 0xa515e0ae, 0x546a34d2, 0xf6de0926, 0x447a5908, 0xec8f7324, 0x66d11776, 0x6abf70cc, 0x9dbb253e, 0xe5d49a5d, 0x06b459d9, 0xa78b2bc0, 0x8fde0f04, 0xb4468449, 0xb783bfff, 0x5e1f43b0, 0xb705349b, 0x7fd12154, 0xe73109bf, 0xa4d9eb27, 0x61185ec8, 0x09cd3c3d, 0x59d1e914, 0x375261fc, 0x5597a0ab, 0x336178f2, 0x21621b89, 0x8f82d5ad, 0xe15779e6, 0x676b70ea, 0x1c5975af, 0x68a76af3, 0x119c2f29, 0xf6bccbc7, 0x003c9d3f, 0x518d1774, 0xe446bd32, 0x9f47c1e5, 0xf154d298, 0x6b34eef0, 0x8c879e92, 0x9aa381f4, 0xee83cb52, 0xca8e7a11, 0x837d689c, 0xf469f58c, 0xe36073ce, 0x9f8760e7, 0x8d4259c7, 0xf70bdd26, 0x64128cad, 0x7f01ebec, 0xe30f254a, 0xa6a3a46b, 0x55420864, 0x8f063a2a, 0x0c8c333a, 0x6a2a0e07, 0x8c50ee8f, 0xb6341941, 0x1cf696fd, 0xf9d8f054, 0x25c229cf, 0xda45fedd, 0x297073d5, 0x9cf3ee98, 0x108be4ef, 0xc6e0de5e, 0x0f513d0d, 0x6e48e142, 0xec7c7454, 0xf2f180ce, 0x5249af0d, 0xb72e7d84, 0x4f9fa9c8, 0xdf837741, 0xddb5bea9, 0xb2c6591d, 0xf7a6e146, 0x3f60b635, 0xb31170e7, 0xd0e36df6, 0xf5d460cf, 0xb849ee17, 0x56e84704, 0xb7b6da6b, 0x5c2ca062, 0x7dec45fd, 0xa8cbf340, 0x93f5f2f0, 0xcb5668f2, 0x436af93e, 0x03b2b035, 0x7f4e061a, 0xb8ee4b2d, 0x05dce0b2, 0xb01151e3, 0xe3abc189, 0x9eb9242d, 0x6805975c, 0x0a0cc3b1, 0xc97667e1, 0x105b73eb, 0xcb778e36, 0x0a028fac, 0x8a3ca344, 0xca1ea85a, 0xae4870cc, 0xdcdf3019, 0x6a50166a, 0xde04cbde, 0x0442eed1, 0x0b974635, 0xb5db8403, 0xaca4b8ee, 0x0d36bd49, 0xfcada53e, 0xd9eae74e, 0xd2113c5c, 0x8e0e821e, 0xee6719fd, 0x11f8095e, 0x7ab455c0, 0x289eb707, 0xa3bd740a, 0x9c9244fc, 0x4d712982, 0x6250e4db, 0x112a7489, 0x8435dccc, 0x4722e39a, 0xcd19c89e, 0x07285159, 0x3eb1a526, 0xb332d4d9, 0xf67ee352, 0x106f97d4, 0xe361d77d, 0xdbf14c7f, 0x808f5c74, 0x47ae7223, 0x55115cef, 0x8e5f7deb, 0xb6a3cb73, 0x64c0f4d8, 0x2bb31eb9, 0x2f59bd48, 0xbd92cde7, 0xa08a3813, 0x3a37fdad, 0x5a779812, 0x7cae086d, 0x3eb5733a, 0x7265ee53, 0xf49d5e02, 0x6b9c2c96, 0x2beccd04, 0xc10a8e49, 0xd4a3c684, 0xd8b17443, 0x2f8d8563, 0xbe289fe4, 0x3e20e0c1, 0xfe89eeeb, 0x7268c974, 0x71788032, 0xcf78efd0, 0x5fb093b1, 0xf05cd668, 0xeec6fa7d, 0x2b76c86c, 0xbc7dcfa2, 0x2c6eb71a, 0x171f1d1b, 0x4137c299, 0x7d2fb76b, 0x09598cd6, 0xf0a8baf8, 0xd51bf570, 0xa6d49d03, 0x71c8eed0, 0x78385472, 0x36c301e2, 0xf7e55bfd, 0xaf20be41, 0x9a0160b3, 0x57c5edb8, 0xe845794b, 0x257a9877, 0xada2d4ac, 0x937a47cd, 0xdc9e9501, 0xa92d9211, 0x9272b8c9, 0xace56f0c, 0xdece200a, 0x31aa1949, 0x0296fd96, 0x3d58e964, 0x83a37e27, 0xe49d265c, 0xe03a9498, 0x984f12e1, 0xae0f646f, 0x78b1a66a, 0x52d4cf98, 0x001eccfe, 0xad0c3928, 0xd08efc01, 0x35ee693d, 0x31dd3f70, 0x6945fd63, 0xae13d8da, 0xca175f8c, 0x790322bc, 0xf0295ed9, 0x7a97a49a, 0xcfb0c103, 0x34e17ff9, 0xfb5cb0e0, 0x592fea10, 0x9047b70d, 0xe4cf08b6, 0x809df842, 0x33be7743, 0x01bae4af, 0x88cc70bc, 0x7749c15f, 0xdd7ad451, 0xe8b375e8, 0x5ee42c3d, 0xedbcc901, 0x0594c916, 0xf49f6706, 0x72a41dc9, 0x100968ed, 0xe864c0b3, 0xc523762f, 0x1ca1930f, 0x4c19935d, 0xa9d6528b, 0xc6eca043, 0x860864f3, 0x99c12358, 0x425ff7dc, 0x95c71b16, 0xd66c13ae, 0x31c9f8c0, 0x3c754f3f, 0x81a5e603, 0x1b6be909, 0xa8b15681, 0xb38a2bee, 0x819710eb, 0x0522f161, 0xd46c821d, 0x319ee76d, 0x90240f80, 0x203f2170, 0x20d97dff, 0xe51f8524, 0x269d9039, 0x81e9982e, 0x3a5e21e5, 0x414ce824, 0x72ea50a0, 0x0a1a5467, 0x772f27ae, 0x7862dd0e, 0x62b4e42b, 0xbddbe646, 0x165daa22, 0x5679abe4, 0x36d5e8c0, 0x32a653f2, 0x56d8ee2f, 0x6991db71, 0x9e4168d9, 0xcd300f96, 0x6953405e, 0x272d881a, 0xdd1908e9, 0x2c279907, 0x633724e4, 0xd8f950be, 0x421e3556, 0x0ae237b7, 0x41f84571, 0x94510980, 0x018befd4, 0x6836131b, 0x0baf98ed, 0xf6364e27, 0x648df3d2, 0x2e9cd1dc, 0x700a865b, 0xa4e93825, 0x18978f09, 0x46f2ed7d, 0x765d3cd7, 0xb42065ce, 0xb62be5af, 0x1bc3d46a, 0xa45900cf, 0x43783dcf, 0x35fab81b, 0x244f66c0, 0x43daf4cf, 0x5ac6bfeb, 0x23266615, 0xb1dd55f4, 0x351adce5, 0xcb3cd8ca, 0xe88b1f32, 0x49d09dde, 0x6a22b882, 0x28667605, 0xbd272138, 0x5c932ac8, 0x421c8770, 0xa990eb3f, 0x5ae981a9, 0xaf3de7af, 0xdc82e233, 0xb407e229, 0xcc2fd17f, 0x6a9f2a67, 0x786ac0e4, 0x43006859, 0xe81bae94, 0x1282fc1c, 0x50bab3f5, 0xb1e322cb, 0x70fe9375, 0x2559c0ce, 0x6361d2dd, 0x7b1f1f3a, 0x8f680209, 0xe5c3b9cb, 0x6912f2b7, 0x9b09d836, 0xfca04eb4, 0xc883fcb4, 0xe83fd1ac, 0x2e64c6b2, 0x820bc8ef, 0x028dea34, 0x6def4df4, 0x82ee5362, 0x93fe9ec3, 0x55e6cb69, 0x359e7d83, 0x4b84cbfc, 0x6dc116ef, 0xd89de764, 0xfd6bfc42, 0x430682e0, 0x70596738, 0x3473b11b, 0x69ce7c05, 0xaeee53af, 0x612f4dc9, 0x159c58c7, 0x425e47d5, 0x114debaf, 0xb7e863cd, 0x17bb39f7, 0xc6ea02f2, 0x46ad4a3e, 0x5a946c1f, 0xc9997505, 0xbd69f6f3, 0xedc978ba, 0x8d148bad, 0x96b806c1, 0x657df4d4, 0x65256e68, 0x8f0b7d75, 0x37975127, 0xde3b2960, 0x3b1bc65c, 0x035e9396, 0x3a74d42f, 0x2ace5a86, 0xd9415a4e, 0xcb23dd3d, 0x6eb0500b, 0xde1563af, 0x937c45d6, 0xcd3fdcd9, 0xc6db645c, 0xdaefb19f, 0xfc164320, 0xe197d531, 0x2d60058f, 0x9b247afd, 0x670f8a5e, 0x60c6a410, 0xfccface2, 0x0fedb167, 0x274a9d85, 0x44b797c3, 0xcedfc9f2, 0x1541d920, 0xe20e1eaf, 0xdfe7da90, 0x03f0d730, 0x9ea5f77e, 0xa546c41c, 0x1007d64e, 0x28cca1f3, 0xccc575bb, 0x5e941a39, 0xa5a92c63, 0x130e2af2, 0xedb32b02, 0x041e061c, 0x3ea0d181, 0xb01307c2, 0x20e9c670, 0xf39c6a73, 0xefb19b7f, 0xdfd6a7e8, 0x2a56f9d0, 0x74a69c9f, 0xbe01e235, 0xde87e938, 0x96334d95, 0x75f1fbc8, 0x735dddbb, 0x83c7fa20, 0xe6fb3b4a, 0xd4084f42, 0x24993b87, 0x63f4436c, 0x2de3a24b, 0xbedd6589, 0xbd6a3479, 0xf024b455, 0xeb211ea9, 0x0c61d6ae, 0x751df13f, 0x02e8b5a4, 0x6780e3bf, 0x20dc0311, 0x91daecd3, 0x6ea99ce1, 0x77a7b8a0, 0xd75ba385, 0xb8965883, 0x586dcd83, 0xe0c5fdec, 0x93d9568a, 0xa5560c54, 0xaf9a07ea, 0x4d5ec6c1, 0x6c1c3b26, 0x386d3f06, 0x04b870f0, 0x6946d9b3, 0x1c2d6795, 0x692e2edc, 0x920bb6c1, 0xf836e95b, 0xe1ada474, 0x96f1d15b, 0x3c3509bf, 0x0a23a0da, 0x65a6218c, 0xbc11d5f6, 0x0af3a2d9, 0x631ac789, 0x6dd54965, 0x66333c4d, 0x02c1176c, 0x24c26935, 0xf0f2572d, 0x76649116, 0xf9da5b72, 0x73d3467d, 0x0699dc1f, 0x20fa1608, 0x5eb54559, 0x8caffe45, 0x80293bec, 0x83a6d938, 0x73b55dbc, 0xd1645736, 0x487e430e, 0x2417dd11, 0xd5a0f1ea, 0x7a1bb6a1, 0x8991e390, 0xc3fa2169, 0x4a1775ed, 0xdb634ba5, 0x8125eb95, 0x22aa3425, 0x45f1a4b0, 0x8e5811a7, 0x0f9392c1, 0x945b05a9, 0x2de29302, 0x6ec9fc9c, 0xde8430e9, 0x1ec7e796, 0xb733fde3, 0xcecbdbd5, 0x410ce730, 0xc4c5f8d9, 0xa80ea934, 0xc02cd260, 0x902700ce, 0x359d3218, 0x28bd2ed8, 0x3461899f, 0x03604cf7, 0xb468c281, 0x52347ece, 0xad560d83, 0xbaf6c0ec, 0x68db1e18, 0x5f39e55d, 0x541c7689, 0xb53d61c9, 0x40217ed3, 0xc93c8e63, 0x80e1f590, 0x38cfc698, 0x71aa3cbd, 0x44cbb4be, 0xea38ac68, 0xbbe70106, 0x2a3b6351, 0x80c07834, 0xa4bbb77f, 0x82cc8157, 0x4b980ed3, 0x7b7849e3, 0x0b83028b, 0x51e7fd66, 0x3afaa743, 0xc85a3556, 0x80c0f053, 0xb258ebf9, 0x834e2b8f, 0x6c0126ea, 0x7c9feded, 0x778730b0, 0x24257459, 0xfc6a50b6, 0x00aa3b88, 0x20285d7d, 0xf8b306d6, 0xcac24569, 0xc5b2f5c0, 0x59ff45f8, 0xe51dd529, 0x198b12bc, 0x9f438135, 0x892be6ab, 0x896d50d9, 0x41cf67e3, 0xd09b25e7, 0xca61eb6f, 0x82396527, 0xf205c6d3, 0x5e972495, 0xe1f8f369, 0x1f8be206, 0x136419b4, 0xf9abb95d, 0x03e575db, 0xa80a9826, 0xf25f787b, 0x2aace961, 0x4186c92b, 0x94e5bc3c, 0x520c5fec, 0xed95e9ac, 0xeb70a05d, 0xe86ed6bf, 0xa2da85f2, 0x6f72ecb5, 0x4be7fcb6, 0x7bb6f454, 0xd0f01d1f, 0x5d2eca8c, 0x441f4bfb, 0x63ec2d16, 0x98ef254f, 0xac967d1b, 0x93dc8d63, 0x94220eec, 0x5316a296, 0xde02bbbc, 0x3f82adad, 0xd1690fb7, 0x48a36325, 0xbe8e9ee1, 0x4dd2545e, 0x35b8c32f, 0xd5fe2f52, 0x157e0dd4, 0x54280c10, 0x6e274a85, 0xdb5f75e5, 0xf73ea45b, 0xc986f013, 0x2b20266d, 0xf73c7310, 0xf41f553d, 0x2bcf2cbb, 0x847491ca, 0x7ee9e8c3, 0xad717248, 0x37875770, 0x3f81e4c7, 0xa4007285, 0xe053528c, 0x8173c5ef, 0xa44ce68f, 0xbb3dafd7, 0x5cfaac40, 0xd8f75ead, 0x3471ad70, 0x58cabbf5, 0x77244104, 0x8aed4b70, 0x09c16ee6, 0xccd6670c, 0xecba76b7, 0x2fc17948, 0x2a12a1b1, 0x19ffa4b3, 0x265f35a8, 0x7c6e2a0b, 0x080666d1, 0xd34c9272, 0x9d36f01c, 0x93084e78, 0xfe5e8f89, 0xbbbe3988, 0xb8ebaf94, 0x0ce22924, 0x24711e81, 0xff2e26a1, 0xea9fb94e, 0xb7c38e93, 0x236130e0, 0x2732a2d0, 0xa9d06280, 0xf10e8b9d, 0x6b245333, 0x8180172d, 0xed1e6657, 0xee56c62f, 0xcc4356f8, 0xfad519c3, 0x786fba71, 0x47bada3f, 0xae3b65b0, 0x27e83da5, 0x86f0e214, 0x4eea8e42, 0x00d1177c, 0x2ed0d44c, 0xa8a1568e, 0xd696dd52, 0x80fa7b06, 0x97f69054, 0xbeae04eb, 0x37d34a69, 0xbd4c5eda, 0x32fcbbe2, 0x65a1722c, 0xca050560, 0x09ac7535, 0x4fab312f, 0xeb799417, 0xfa7ae7b4, 0xbf97c080, 0x77589171, 0x838b2dd3, 0x9b9d2ab9, 0xf84ec802, 0xc768bd34, 0xf85f218b, 0xf315f7d2, 0x65562bdf, 0xd59c5607, 0x5969ebaf, 0x484141c2, 0x87c17705, 0xb4af0795, 0x1e836c00, 0xecb02cc4, 0x8785355f, 0x6b779c5c, 0x374b2150, 0x140b07b3, 0x4a29dac4, 0x9cbc29e4, 0x3b177d3e, 0xf1068044, 0x4b8bb017, 0x5c43ddd1, 0x2c1f9f5c, 0x69e23041, 0x6490f1b3, 0x0d9a82b9, 0x102d05e9, 0xd74bab65, 0xe5e34598, 0x46f49fbc, 0xd3a34d64, 0x7002a39f, 0x44425eb2, 0x98d34a56, 0xbd4a38a3, 0x0569e7e6, 0x5d73ab24, 0x61aee8de, 0x9ded6b1c, 0x5d65245b, 0x40e27b71, 0x3d6d07ad, 0x906ac0d2, 0x2aef38b7, 0xcb3f2716, 0xecda9eeb, 0x2c1a17fb, 0x15f8185f, 0xe58ea8c0, 0x49799853, 0xaf2dd85c, 0xc194d30f, 0xa255e3b8, 0x23b8970c, 0x3dcee9c7, 0x5a3b57eb, 0xfce9a2d5, 0xfe6963f3, 0xb4b9ff22, 0xc962f14a, 0x5e1ee21d, 0xee985229, 0x4cd4ca6a, 0x9fe80524, 0x5687c62c, 0x1abd3939, 0x1c65ca60, 0xb6b5a5a3, 0x2edf1739, 0x376d93d5, 0xf70da57a, 0x1ab65128, 0xb12efd40, 0xccf21705, 0x3f074dc4, 0xa46625ae, 0x226c13a2, 0x22dcf58a, 0x1068abbb, 0xdbfbb149, 0xf1ee441e, 0x90c07676, 0x8478f7db, 0x2683348c, 0x3284c732, 0xed85af3f, 0xbe2fd09e, 0x1770b093, 0xd47b85ae, 0x1a4672a2, 0x629e337b, 0xb2bb1ebf, 0x58a74d60, 0xc5b85024, 0xd1c1cebd, 0xb2977c3e, 0x1ef08aae, 0x6528a400, 0x0d951107, 0xe9e11c4d, 0x4beb27a8, 0x9a127fd8, 0x808b7c38, 0x43ec3c96, 0x5b4a0a1b, 0xa6a8e545, 0x468ae3a2, 0x6fc6ebe5, 0x9daaf583, 0x7f476988, 0xc6a20cd9, 0x194831e8, 0x1e0e9842, 0x0c0d6457, 0x6a4d89b4, 0x78234c60, 0xd7ab3f6b, 0x7b3ae1c2, 0x3681dd1f, 0xdb057707, 0x7014b597, 0x53d79456, 0x744b7fd1, 0x25c2625d, 0x0d671ead, 0xfc80ee25, 0x3b4d9f37, 0xe37307bb, 0x54b9979b, 0x05f871a8, 0x0ee2f239, 0xb7575c5a, 0x677de416, 0x15b2687e, 0x97b09591, 0xe36fba0b, 0x628573d2, 0x82444fd5, 0x616276de, 0xa3c08817, 0x37d3cd25, 0xbb49ac01, 0x357675fb, 0xac178c7e, 0xe0c7eb4a, 0x1549d9c6, 0x33fe23b8, 0x2c6635ec, 0x2af6fcec, 0xf1a5066c, 0xf956613c, 0x4d0f1ddb, 0x952e171e, 0x740c4035, 0x46701731, 0xbd33defc, 0x46a49a7f, 0xad540543, 0x759cc8a7, 0xf8cb674c, 0xd24ef3b9, 0xe58a6c8f, 0x2ee2df43, 0x198b612d, 0xf9f75216, 0xe69c5e54, 0x0bce7de5, 0x45eeb6a2, 0x97ffd946, 0x9f5f6f0a, 0xa43d1da9, 0xbe5be234, 0x02963a1f, 0xb5174b46, 0x47a3db13, 0x7a27c2db, 0x3275428f, 0x818c2917, 0xb84c98e5, 0xe3f5df74, 0x1331233e, 0x6efc27f4, 0xc748a58a, 0xcab30192, 0xe65881f9, 0x9ce48085, 0x27228938, 0x5e329711, 0xe64b0896, 0x62194943, 0x8b8f8b3d, 0xe9aacd0c, 0x1c451e28, 0x3fdeaf19, 0x1e75d605, 0xd2aa2f6a, 0xde2819db, 0xb9e06596, 0xf0006753, 0x8100f1fc, 0xfbf2d9fd, 0x68ab2664, 0x5539f06e, 0xbc968add, 0xf88decf1, 0xa3f1c39c, 0x90ece141, 0x3d16fc07, 0xad3b16be, 0xa23acbb7, 0x35346fdc, 0x6d26f685, 0x461ee7fe, 0x4513a27a, 0x8862dbe6, 0xb6cc390f, 0x5a5e0680, 0xa6a4c40d, 0x1c0bf75a, 0x61e825cd, 0x54f1ec66, 0x9356230d, 0x962f925c, 0xc3d612fc, 0x09d1d6ea, 0x4355812a, 0x6dd47e93, 0xe34007ec, 0xe1c076d4, 0x62b4be15, 0x761cd7e1, 0xcb7c5d38, 0xe17bb275, 0x146953a9, 0x16b88982, 0x3a97835f, 0x2c2af581, 0x901d2c22, 0xf4204431, 0xe5030cf0, 0x7302f0e0, 0x9bf74f3d, 0xcc7aae0f, 0xa000c921, 0x8c958a8a, 0xd352b799, 0xcc638378, 0xde330469, 0xb246f09f, 0xc7fe4722, 0xdade299d, 0x19871c09, 0x3e8a17a6, 0x32ccc5bd, 0x052a2872, 0xaf4dddf7, 0xc1b01f01, 0x3586f013, 0x9e7b2fd4, 0xbf7012d1, 0x0e9b1e83, 0xea8e5f5e, 0xc2b27679, 0x3eadc34e, 0x69c1822b, 0x6da2dae7, 0xa0fcaca4, 0x2be94571, 0x6fac35d6, 0x17e99d78, 0xdd35e8d0, 0xfabb7fda, 0x775f9247, 0x0fc40c55, 0xb23a25b5, 0xd2bf779a, 0xf925df44, 0x90023531, 0x322a0198, 0x71e577cc, 0x98667c2d, 0x29cf5e30, 0x6ac16080, 0x91ac955d, 0x63ad293f, 0x45de2e89, 0x240d4300, 0xa02fa8a1, 0x1233956e, 0xc77c51e3, 0x8a83b45c, 0x91cb00d6, 0xdd28a703, 0x025971ff, 0x9b521646, 0x3852ae06, 0x69ec9023, 0x5b6f1131, 0x78903be0, 0xfe76031f, 0x002ef63f, 0xed2afa48, 0x6ed0e808, 0xacaed16e, 0x9cad2f1f, 0x3331fc0f, 0xdb97a5c4, 0x5caf0272, 0x22927601, 0x9ce0d020, 0xe7a4ccf6, 0x4e7af894, 0x2c001a6f, 0x38327139, 0xd2ca6839, 0x2e832201, 0x27c4216d, 0x839de77f, 0x2f127f0c, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well19937a(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitInPoolIsFunctional(Well19937a.class, 19937); } }
3,058
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/PcgXshRr32Test.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class PcgXshRr32Test { @Test void testReferenceCode() { /* * Tested with respect to pcg_engines::setseq_xsh_rr_64_32(x, y) of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0xe860dd24, 0x15d339c0, 0xd9f75c46, 0x00efabb7, 0xa625e97f, 0xcdeae599, 0x6304e667, 0xbc81be11, 0x2b8ea285, 0x8e186699, 0xac552be9, 0xd1ae72e5, 0x5b953ad4, 0xa061dc1b, 0x526006e7, 0xf5a6c623, 0xfcefea93, 0x3a1964d2, 0xd6f03237, 0xf3e493f7, 0x0c733750, 0x34a73582, 0xc4f8807b, 0x92b741ca, 0x0d38bf9c, 0xc39ee6ad, 0xdc24857b, 0x7ba8f7d8, 0x377a2618, 0x92d83d3f, 0xd22a957a, 0xb6724af4, 0xe116141a, 0xf465fe45, 0xa95f35bb, 0xf0398d4d, 0xe880af3e, 0xc2951dfd, 0x984ec575, 0x8679addb, }; RandomAssert.assertEquals(expectedSequence, new PcgXshRr32(new long[] { 0x012de1babb3c4104L, 0xc8161b4202294965L })); } @Test void testReferenceCodeFixedIncrement() { /* * Tested with respect to pcg_engines::setseq_xsh_rr_64_32(x) of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final int[] expectedSequence = { 0x0d2d5291, 0x45df90aa, 0xc60f3fb7, 0x06694f16, 0x29563e6f, 0x42f46063, 0xf2be5583, 0x30360e91, 0x36385531, 0xddd36cd9, 0x5f4a6535, 0x644d10c0, 0xaca075d7, 0x33781706, 0x4e1f9f34, 0x0676e286, 0xaca5eeb2, 0x7315cc93, 0xa6dfefe2, 0xd480e065, 0xda9da26f, 0xda0f27b7, 0x045c0844, 0x22acfa0f, 0xcd7ecd75, 0xb97fd692, 0xac96dd03, 0xf59c7174, 0x488947fe, 0x64a3d543, 0x90963884, 0x4adee0bb, 0x993cf7c0, 0x8545b3f2, 0x409b542d, 0x6bf0a247, 0xfd59f9b4, 0x8f50b06e, 0x1bbcf6f5, 0xe1fdd29c, }; RandomAssert.assertEquals(expectedSequence, new PcgXshRr32(0x012de1babb3c4104L)); } }
3,059
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/KISSRandomTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class KISSRandomTest { @Test void testMarsaglia() { final int[] seed = { 0x159a55e5, 0x1f123bb5, 0x75bcd15, 0x16a81cc0 }; /* * Data generated from code snippet provided here: * https://programmingpraxis.com/2010/10/05/george-marsaglias-random-number-generators/ * Test code was modified w.r.t the above (because the latter uses * calls to functions not defined for "KISS"). */ final int[] expectedSequence = { 0x9bddf92e, 0xd5a41e38, 0xb2f6ff02, 0x181980c1, 0x1a48acf3, 0x5877789e, 0x08477a08, 0xabd9fdcd, 0x20d27205, 0xd55e1658, 0x6625bdda, 0x46e92162, 0x5dc0ac63, 0x0be5e04a, 0x2c8cde38, 0xdfe87d7b, 0xa1372cc8, 0x49908cc8, 0x150b6104, 0xd5d397ad, 0x44651a2d, 0xc65d6e70, 0x053ea995, 0xb004ca00, 0x8f99c850, 0x989a6aeb, 0x5199028f, 0xc90a92ab, 0x6a37785e, 0x50bb032e, 0x169cc118, 0xff23b9b1, 0x70df92f1, 0x599079b3, 0xe573c766, 0x3a7a5675, 0x76a5bda2, 0x583d8e7f, 0xcb27109e, 0x4d781f1b, 0x65ff5588, 0x2378e795, 0x7e79ee6d, 0x2b1a9240, 0x66d8d164, 0x1c3c5a1e, 0x75ef17ae, 0x91c4fade, 0xf58fa074, 0x51349bd0, 0x4245e5fc, 0xf707d6e2, 0xf05b600c, 0x08383b08, 0x3101e3d7, 0x1d7eb87e, 0xcef5e87d, 0xe6020ce2, 0x8d77a3ae, 0x9144b8be, 0xcb4c2d54, 0xba907fc2, 0xce4c0a47, 0x08b91835, 0x275b22a9, 0x9ccffa6b, 0xa795f7f3, 0xed433852, 0x1c90b2bc, 0xf6e92296, 0xfd4ad01d, 0x88b8620f, 0xfb869d24, 0x054cde71, 0xcecdf83c, 0x89365122, 0x04cbc72d, 0xa5ca868c, 0x27eca37c, 0x6c2e75c2, 0x71b5f2b8, 0xac9a6a2e, 0x0d1feff0, 0xcbdac51d, 0xb269f578, 0x7e6352af, 0x5641e244, 0xffbc597b, 0xaaf45646, 0x0ad41f8b, 0x63af1b42, 0xba964845, 0x0b80dc89, 0x0f0d03b1, 0x92ffbfa7, 0xb1914cdb, 0x862f58ea, 0xc937de2a, 0x70778304, 0x28b95c4c, 0x479c1517, 0x28a1ab78, 0xef70cdcc, 0x6a370f40, 0xf53ce32b, 0xfbd70fdf, 0xfffb2f3d, 0x61b0ee67, 0x10a166b7, 0x9cd0a674, 0x4227ffee, 0x5320e5e5, 0x219b7bcb, 0xa3651b50, 0xfb130991, 0xa507097c, 0x4c81a5a5, 0xe30ed15b, 0xa621b2db, 0xbdc27a16, 0x084cd486, 0xe26a6904, 0x242b4d3f, 0x5cef1f8f, 0x20f42ebe, 0x052c67fd, 0xa8c0904d, 0x879626af, 0x0e1b0f5d, 0x1aea6221, 0xcdc30d90, 0xafcc3abb, 0x2a5829f2, 0x382d9706, 0xe8477b46, 0x0c465cdf, 0xba7a34fd, 0xf54cda0f, 0xede336c2, 0xbd304106, 0x91ccc614, 0x092adb4d, 0x2c1f9e13, 0x5f2009b0, 0x2eacc097, 0xe29e5b23, 0x65d191af, 0x3fcc259d, 0xe7abe14f, 0x3594c702, 0x7a7cedc7, 0xc8937289, 0x467e5fb1, 0x5bcb15d2, 0x8dd2050d, 0x314c559c, 0x4709cb40, 0x9242d0c2, 0x2a9203a6, 0xc34eeabe, 0x119bac5a, 0xbefdedc9, 0xbda6feaf, 0x6e082aa2, 0xb7158b61, 0x3c99488f, 0xd0295429, 0x2fbb2ded, 0xdac17086, 0xc94c04fa, 0xbd7242cf, 0x0ba4b590, 0x86aef6c8, 0x6b8f7469, 0x48c8d85e, 0x2f1e48ab, 0x21046a46, 0x3eaf3223, 0xf16665e7, 0xb99d3e5d, 0xf19d4960, 0xe85762ed, 0x35cf14d0, 0x2a480cbc, 0x8f05f0c9, 0x6316bc0b, 0xac66305b, 0x76935c15, 0x1ad7ec46, 0x1d3f4c34, 0xb2797ff3, 0xb18394d5, 0x4acbd91e, 0x503ad042, 0x1da06817, 0xdf588cc3, 0x1173c7bc, 0xfb073a27, 0x980f1951, 0x0154020a, 0x5a10993c, 0xa3fcaaef, 0x5cd2672e, 0xa4307ae6, 0x91d9102c, 0xb40032d6, 0x34c7db2a, 0xb1dc6829, 0xe21cc2c7, 0xc9d90b56, 0x10cdbfd0, 0x6f60cded, 0xbf1930cb, 0xcfaad21d, 0x4531f7da, 0x04d15688, 0x1b5d3587, 0x5095feb0, 0x546a956c, 0x4981241c, 0x80bf8a10, 0xdaded8b1, 0x6d7f4f5e, 0xcc76c5c2, 0x31206cbc, 0xd5557cb2, 0x42acb28e, 0x0c9522dd, 0xa0bf7520, 0xe7fbf236, 0xe8aad8d1, 0x419f297d, 0x49278249, 0xd2103da1, 0x094ca07b, 0x19a3632b, 0xb1fa28b4, 0x1efe8423, 0x36fb4716, 0x8fb6eca6, 0x6bfe8f8b, 0xc9617275, 0xb2ad50e1, 0xe8a9561e, 0xfa1e59fc, 0xc843b229, 0xf40d9ac5, 0x63794c70, 0x230b6311, 0x2388749e, 0x7536cfa5, 0xd5ae3437, 0xaff04a09, 0x63e3bdeb, 0x5128e726, 0xee4da3c9, 0x628c9033, 0xea3c6792, 0xbaf665a5, 0xc936dfb1, 0x4686e3ac, 0xcc668c46, 0xb6c94620, 0xaf5409ba, 0x7e436a18, 0xb48deb9b, 0xbbbd41da, 0xc525c3a2, 0xdff2d190, 0xa4b39b98, 0xa876cebf, 0xcc86d8da, 0x17b28124, 0x7b564204, 0xea42d2e5, 0x2e8ae4f5, 0x75615c56, 0x74aad2f0, 0x850897ad, 0x71538c73, 0x08b697f3, 0x085af9e7, 0x86b65b18, 0xd447660e, 0x3e5f6bdc, 0xbf4edff7, 0xd6a75ac5, 0xcb2ae1d9, 0x5bf8dea8, 0x4c74312d, 0x90f02a05, 0x4da50b30, 0x1e814a53, 0x9035acef, 0xf441623f, 0xd61a7507, 0x46177245, 0xdfad40e2, 0xe73b4d54, 0x2c873c84, 0x3cbe4d72, 0x1ab0e26f, 0x42a40530, 0x5f4cb559, 0xa7fc2436, 0x28fab25f, 0x4946c242, 0xc9af000a, 0x0cb05860, 0x64f4409c, 0x32fb4257, 0xfe8b2919, 0x7fca3bb8, 0x66ae0e89, 0x9c78de62, 0x52a55a80, 0x9b49bd06, 0xc940e8e3, 0x4a0d2df5, 0xc138d6d4, 0x0e3faa39, 0x9d5f9fef, 0x7388cf17, 0xc884bd00, 0x3cc0cdda, 0x433af830, 0x8a37d7f6, 0xef2f6cc0, 0x66caded9, 0x93df50bd, 0x28b25b81, 0x637c8ab5, 0x68bb221f, 0xf7a3216a, 0x729c4b2a, 0x241c71da, 0x2892b0f4, 0x9bef56ff, 0x4eb61e9f, 0x0f847c98, 0x48f45142, 0xdb6c93bc, 0x23caefc1, 0xf348a188, 0x8f65bed7, 0xdaa1096b, 0x881764ef, 0x976b30b2, 0x4679cad9, 0x5dd89bfa, 0xa3869d81, 0x7c6862d2, 0x9109547c, 0xd0db345b, 0x3cb74993, 0xe4c3af19, 0xfd5f2837, 0xfdec8040, 0xf9a45b20, 0x486e96de, 0x0a0b7762, 0x790ddce0, 0xfc3be3f0, 0x4b671260, 0xccb5f958, 0x33e94796, 0x1ee4f26d, 0x5508b616, 0x8da48af5, 0xaa8731a7, 0xd32ab831, 0x803726e0, 0x9c189aeb, 0x39a33842, 0xadca2595, 0xbf5d1296, 0xc46ff6b5, 0x734b751b, 0x57661d71, 0xce360f69, 0xeab3c559, 0xd52bfb30, 0xe3613946, 0x32e3c4b7, 0x29033e15, 0x1e2d9131, 0xf539e2cf, 0x8dd8a7d2, 0x519c1852, 0x5ecadeb2, 0x61eda0cd, 0x26f24244, 0x2ee72248, 0xd2681224, 0xb9e186ad, 0x605340b2, 0xfc3f9a24, 0x972edf65, 0xaf2e37b3, 0x3275ef7c, 0xd4c363e7, 0x102098b0, 0x687273cb, 0x10e24570, 0x8b9eef36, 0x48d85e3b, 0x8917f19a, 0xdab2a6c8, 0x8ac77b69, 0x16b22036, 0x5d4b23d4, 0x2ad9a70b, 0xbe3e53a7, 0xf0e64bfd, 0xab054c49, 0x2903bb0b, 0x4d170bdf, 0x83cb27eb, 0x2c1ea00b, 0x79e18fd5, 0xed7338d1, 0xe262c6b9, 0xae427025, 0x8ddd23fb, 0x8ff25d3e, 0x3945776b, 0xed590a9f, 0x2219a23a, 0xfc56d783, 0xa20f7ca2, 0x07bf3698, 0xb96528e7, 0x29cc982d, 0x0987dc2d, 0x5111622a, 0xa830c39b, 0xe3dac526, 0x6fd9540b, 0x2b766bb6, 0x836ce238, 0x03f90d60, 0x5932da0c, 0x16df830d, 0xc4ff2bef, 0xfefba81a, 0x70c63f59, 0x52c2c2e0, 0xb16b3327, 0xab5c7537, 0xdf437ec7, 0x6d1f837a, 0x7a8e4166, 0x0e7bb54f, 0x37abe65a, 0xc62c2798, 0x79ec2d1b, 0x5ac9bc1a, 0xd151e3c6, 0x72b18873, 0x6cec7e6e, 0x3bc7db87, 0xa2f74c99, 0xff68ce99, 0x27c1113c, 0x2a55bea8, 0xd6c0164b, 0x2df44b5a, 0xe2fbdc41, 0x699f0e4d, 0xb8c3978f, 0xa0704979, 0xbc881700, 0x84c4fd5b, 0x3193c851, 0xdbabaa40, 0xc6a3cab0, 0x07ce6dc2, 0x2de90a7b, 0x0b9f08bf, 0x1bb1a3cb, 0x3bc4d798, 0xdf5843ee, 0xfc8751f9, 0xbb322216, 0x431b8ee9, 0x54a5ec08, 0xc1982bb8, 0xe370d7ff, 0x27e7f1ec, 0xaa489ce1, 0x49c9c2e5, 0xef5ddecf, 0xbef9b424, 0x2a178797, 0xb028ad66, 0x622824f9, 0x8365d72f, 0x0f0febb2, 0xe204682b, 0xaf1cbe29, 0xed2b8bc5, 0xe37e25d7, 0x16c23b8f, 0x8fb91cac, 0xb403a3f0, 0xf5fbc869, 0xe7be64d6, 0xdeb1182a, 0xc731e53d, 0x1a3fb823, 0xf4efeee8, 0x9d42f7d6, 0x881c28a8, 0xbb83f0c8, 0x2682d4c8, 0x942a3303, 0xfb481ea0, 0xc1aa5c19, 0x60bdb8d3, 0xbb811544, 0x09506b13, 0x9aae4f2e, 0xd87042c5, 0x019d794c, 0x24873ec7, 0x21aa867c, 0x840d366f, 0x582cb845, 0x7ed15d84, 0x1a332a6d, 0xed57d3a0, 0x09decf47, 0x3200a861, 0x9bade8aa, 0xd9172ed8, 0xcbd4e7dc, 0x5eab57b0, 0x8b691e98, 0xc1ba9545, 0xf519ef86, 0xd355769a, 0xcbb4cbcd, 0x53207adb, 0x3dc07d77, 0x88d0cbda, 0x8b5539c7, 0x1a743f7e, 0x36e674f8, 0xb84e545e, 0x6c0e2e2b, 0x5a35cb7d, 0x4f893b20, 0x8a5a9fde, 0x2cd37895, 0xb7cf463a, 0xacc45d88, 0xf5e79c65, 0x3ff8d56e, 0xf611c876, 0x0fa9fc25, 0x9085e3f9, 0xec40b7f2, 0x0aee3ae0, 0x760ee357, 0xa4696b0d, 0x0d8c8b8b, 0xa3b5d99f, 0xe94f6256, 0xa3f83775, 0xad4d9c0c, 0x15b8d73d, 0x7e0b9b5c, 0x04099c0a, 0xd954fabc, 0x3864a4f4, 0x4437294d, 0x98cdacd9, 0xeebc7109, 0xc26b1c2e, 0xaf526699, 0xc77cab85, 0xb7a2452b, 0xa8854a5f, 0xe4109703, 0x37cecb2e, 0x0d80a309, 0x3278a84e, 0x5d04fdc7, 0x152c5303, 0xa6d0c819, 0x0da056ff, 0xccc77e72, 0x1b6de6f3, 0x65d17dbe, 0xc7ff6dfe, 0x8767b6de, 0x022aae5b, 0x920cf0be, 0xc01df095, 0xa264ed4b, 0xd0dcb856, 0x9908e09a, 0x87a4cae1, 0xf91d5def, 0x35386ccf, 0x706ad02f, 0x406165a9, 0x2e2b7d0b, 0x7ec6af67, 0xc48b4739, 0x7ecd0626, 0x93bc872d, 0x30fc75e4, 0x9a6b22f7, 0x335b67da, 0x57843db1, 0x1aa218ff, 0x208e7184, 0x87562ffd, 0xbbd4d912, 0x49871553, 0x98eb26fd, 0xdc542e6a, 0xfeb7884e, 0x686812b1, 0xe783ce01, 0xbf085f53, 0x9006ed3c, 0xc263f011, 0xe7d2652c, 0x21e04885, 0x9abf1bcd, 0x21f240be, 0xe7579fce, 0x0a18527f, 0x1d809e11, 0xa7659c30, 0x61904220, 0xf2947c55, 0x1e10bbaa, 0xf0743c35, 0x1be461f0, 0xd6ea77c5, 0x8526acab, 0x3e33cd90, 0xf32e47a9, 0x20595910, 0x7378fb98, 0xcee5ca21, 0xc00450c0, 0x4d36d07e, 0xe3668286, 0xa9a9b5f6, 0xfa152172, 0x26342971, 0x5bad7152, 0xeac38189, 0x46f30c67, 0x5d792534, 0x22f9e10d, 0x62f147fd, 0x9ab4a355, 0xebc9b70c, 0xea79fdc5, 0x89ad6392, 0x27d140ad, 0x63a14fac, 0x558c6169, 0xc31aee58, 0x44e518b5, 0x6b395095, 0x32b02b4c, 0x4bb68956, 0x707ecbd9, 0x795d3b16, 0xb02bec7e, 0x4a2b69d0, 0x08860261, 0x47338bc5, 0x3d8ddd94, 0x01162025, 0x58f7666d, 0x8824fb8b, 0xf5ac2bdb, 0x399a14e1, 0x7a9e86a9, 0x09e20963, 0xde1ce097, 0x18fd4597, 0x458fb7d1, 0xaf266dc9, 0x38a03289, 0xdbf9405e, 0x22f72b15, 0x11cde58f, 0xa8268bd6, 0xcfe1eaad, 0x0be9106f, 0xa7fa59b6, 0x25a77083, 0xb0fbadda, 0xd8b1c6a5, 0x348bd9de, 0x16d7bc9e, 0xf1a1a4d8, 0xdf5eb72c, 0xa0cfe4f9, 0xbe155322, 0x808c327f, 0x1d9e9dcb, 0x673948b1, 0xbbb3429e, 0xa87428b6, 0x15769548, 0x50c39092, 0x8c938266, 0x744a7cbe, 0xf9a52368, 0x05228781, 0x81b1d8c2, 0xce8e2841, 0x0ef6b8b4, 0x16d8217c, 0x5044ed89, 0xd5ea65e6, 0x71708fc2, 0xc08b851a, 0xafc170bd, 0x2f70770b, 0x2146bad6, 0x939c4959, 0xdaa508c1, 0xcd84c7b5, 0x8dfec5aa, 0xd93dc438, 0x3a7d0f4c, 0xe80e0920, 0xb0d1322a, 0xadb0dc41, 0xa6f0d199, 0xb4b099d7, 0x01e1ef5a, 0x759b71d8, 0x0173a16c, 0x00f4f943, 0x9ce79dcd, 0x32000f3b, 0x3e34ae20, 0x8e90cc8f, 0xf498a5ed, 0x8a942c71, 0xc4d43db3, 0x2cb73388, 0x415e4744, 0xf815dd6f, 0x8115097e, 0x3f96c2b9, 0x03020349, 0x1874203c, 0x664e8be8, 0x2d7a7faf, 0xc82c3f11, 0xa02d8eb5, 0x1818bc7c, 0x8cb7860f, 0x44616c09, 0x17c37006, 0xdce1797e, 0x262067c1, 0x163e2a1d, 0xc0bfa651, 0xfb26d74f, 0x4a20f906, 0xfed1cf57, 0x9afd70d8, 0x94d686d8, 0x7279943d, 0x7fb106b1, 0xea979669, 0x65aa06a8, 0xdd1c9b33, 0x75f36f06, 0x9d5bcf53, 0xb1fcbc13, 0x38009684, 0xb3ddd247, 0x841bf86b, 0xa8cf3529, 0xc13d11c7, 0x61712e1c, 0x2f94716f, 0xb3cd94b2, 0xc3bf8100, 0x99e5c378, 0x741b6a57, 0xbbef8a8b, 0x2c71ea5e, 0xab288d55, 0xdfb6436c, 0xa0ddb70c, 0x55a857e0, 0x2afd06f1, 0x31facb85, 0x2bf3012e, 0x92a9604d, 0x99122a61, 0x5e009ff2, 0x3b2bf8d5, 0x01a4163b, 0x7466a4ab, 0xe87d42f8, 0x42924d68, 0x0922918f, 0x51799336, 0x85198eb5, 0x0a1d9921, 0x6eceb77e, 0x102ae474, 0xe91d1483, 0xe2884af6, 0x9ec6662e, 0x90dfe5b0, 0xb792574b, 0xd3df6743, 0x0118c033, 0xfb7d8e35, 0x5b181e93, 0x65ee4f0c, 0x8d5da3bb, 0xe18114fb, 0xe1a22222, 0x39cade88, 0xd028b13c, 0x18e9aa47, 0x40f66061, 0x969ad9c0, 0x92ebe7ab, 0x0851c58f, 0x7d2ddc83, 0x8116edff, 0x2bb301e1, 0xb5e39f0a, 0xfec6c8ae, 0x7c68864a, 0x8e6080e6, 0xa174b691, 0xaf851d36, 0x35d5436a, 0x061dd550, 0xe41def33, 0x18d17f85, 0xdd0cea07, 0xed485b29, 0x4ccf5968, 0xf690bc3a, 0x53548d4b, 0x0afbf6d2, 0x03103a93, 0x1585693c, 0x32ba9756, 0xf6413d88, 0xe66da7d1, 0x2f655a9d, 0xd17639b1, 0x7b91263d, 0x294f10d3, 0x88db2fdd, 0x3c9060af, 0x1fcfd771, 0xf991a176, 0x52d2ff33, 0x69026b72, 0x27f07e2e, 0x8ea40247, 0x16996eff, 0xd032da80, 0xb1a8a6d7, 0x4f596e15, 0xeb33c034, 0x7a41947f, 0x2544764a, 0xc95f1f0e, 0x912d88ee, 0xad806071, 0x9df4ca4a, 0x36ea6054, 0x21665964, 0xef095f1a, 0xe3cbc59d, 0x72efb716, 0x8174007a, 0x1fc8a8a2, 0x29ffba04, 0x9a1669c9, 0x8c3ba8fb, 0x1a283765, 0x62725d2c, 0x1bcfd949, 0x79ab0eae, 0x2952f8f9, 0xa715e3e1, 0x3f64afe3, 0xbce49540, 0x6e4d8a65, 0x47709479, 0x5fa2c802, 0x8abfedc8, 0x44574700, 0x0f760805, 0x8fc6aa53, 0xb0b41aa8, 0x946e51ef, 0xd06a39a3, 0xd7e06200, 0x434130ae, 0xee8fb053, 0xcdb70f63, 0x63aebbd9, 0xda48097c, 0x86aa4007, 0xb77fbdac, 0x1eb28456, 0xb30e7026, 0x200b8e15, 0x97318c31, 0xda50e4b3, 0xa2fe7745, 0xb8414bfd, 0xa4c9ee73, 0xf2ff3e9c, 0x9cc3c9fd, 0x6a0ea090, 0x995436fe, 0x4bde5ebd, 0x30ce4d64, 0x3a35fa03, 0x46d14cae, 0x6a7bdf97, 0x5913707c, 0x7293963c, 0xe50c461d, 0xeb163f17, 0x610cd148, 0x88272ed3, 0x9c486c3d, 0xe24e2dc4, 0x1f787244, 0x63b244fb, 0xbe736317, 0x39774449, 0x2b2141a3, 0x424bd98d, 0x63f9ce48, 0x1c8dad05, 0x0ccbdb3f, 0x1a5123bf, 0x525ccd43, 0x0e033102, 0xae80d483, 0x3e818fb0, 0x8a73393c, 0x709eb2ad, 0x545ffcf2, 0xb6015dad, 0x5138e4c2, 0x3312f727, 0x76f743bd, 0x421c883b, 0x741f432e, 0x8f417c31, 0x7a9107ca, 0x1f520191, 0xa8c5e2e5, 0xbcf10c6b, 0x248b2cce, 0x9dc0520c, 0xf39aaee7, 0xa2342585, 0xd100362b, 0x3784d183, 0x6f882be2, 0xe9e625e5, 0x95baadd0, 0x23abc37e, 0xbc5ffc54, 0x2076cc77, 0x45b7fa2d, 0xb13d8c93, 0x65a9b9fe, 0x01c3c820, 0x28438c9a, 0x463f3fd2, 0x1cc38180, 0x1f1f12cd, 0x68b6b469, 0xd44b8926, 0x2d2ac88e, 0x2c3d072c, 0x3942afbc, 0xcc0402b7, }; RandomAssert.assertEquals(expectedSequence, new KISSRandom(seed)); } @Test void testConstructorWithZeroSeedInSubRangeIsPartiallyFunctional() { // If the first 3 positions are zero then the output is a simple LCG. // The seeding routine in commons-rng-simple should ensure the first 3 seed // positions are not all zero. final int m = 69069; final int c = 1234567; int s = 42; final int[] seed = new int[] {0, 0, 0, s}; final KISSRandom rng = new KISSRandom(seed); for (int i = 0; i < 200; i++) { s = m * s + c; Assertions.assertEquals(s, rng.nextInt()); } } }
3,060
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/ISAACRandomTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class ISAACRandomTest { private static final int[] SEED_1 = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; private static final int[] SEED_2 = { 0x61b12894, 0x4a43da95, 0x03e4d8c5, 0xd92e174f, 0xe8998d71, 0x0ecaaa89, 0xaba8a61d, 0xcfd457fc, 0xbf25f0a7, 0xe05b20a9, 0xdc744513, 0x9a3eb193, 0x1b69542b, 0xedb0890d, 0xca6b233d, 0xfcabf357, 0x297e95f0, 0x1a6c456f, 0x0e3738b0, 0x1c0517f2, 0xcfd105bd, 0x3b7c39eb, 0x141804e9, 0x8a13a0d1, 0x3e57cf5c, 0x35471206, 0x00115ef6, 0x3424ec23, 0x2a6a63a7, 0x4cecb3e8, 0xecb4d341, 0x63d25ac1, 0x8b68eafd, 0x0eca65b4, 0xd8354668, 0xb37b1ff8, 0x82e80ce3, 0x4c212f9c, 0x58d82d5f, 0x47f36348, 0x5bd88987, 0xf77ac66e, 0x75ff93ee, 0xef763453, 0x9705f8b6, 0x64e44649, 0x84f03338, 0x902120e9, 0x5350212e, 0x34f466f8, 0x97f96d0e, 0x7f1db8f0, 0x15879833, 0xefee14b4, 0xda25520a, 0x81c0dd7c, 0xa20bb729, 0x2fd76844, 0x1b522548, 0xf394565d, 0xabff5f1b, 0x38eaf2e7, 0x364a6ccf, 0x8ed5e169, 0xe76aae18, 0x0e4c0b62, 0x68356792, 0x8bc4aa83, 0x31546e69, 0xa6d04441, 0x2abef1df, 0xa40a164e, 0x8a8d00ba, 0x32b38dba, 0x6f66a7c6, 0x493b0c84, 0xd86846f0, 0x50d50178, 0x26a7e67c, 0x97802153, 0xf35f4ad3, 0x4b54b54e, 0x35bcaef1, 0xc3ed09d6, 0xc127bc55, 0xb2e34ea4, 0x8d674133, 0xed82f03d, 0xa8443b28, 0x30bf4bd9, 0x6748419c, 0x155eb313, 0xb17c85da, 0xcd0d8014, 0xf1e95740, 0x5e769ed2, 0xf8fa5819, 0x3bd0d93a, 0xa46eaf3f, 0x90f5ae2c, 0x912aa83d, 0x66995d3d, 0x8359b2a9, 0x64534b93, 0x1fa38094, 0x89fde50c, 0xef925ef5, 0x4edf2287, 0xc6e82b2b, 0x99812c32, 0x53f6fe01, 0xf104263f, 0xbc5d8b8e, 0x6f23f102, 0xb6cc174d, 0xb47b2421, 0xdb083f26, 0xd67a1f25, 0x4f4b0c50, 0x86fca924, 0x442036ba, 0x0d7dba08, 0xf6d89c1e, 0x65c9e28e, 0xd689fafc, 0x47f7ae9b, 0x3cb86674, 0x798dc4ce, 0x62a2d235, 0x73912420, 0x276ef9b4, 0x50deb689, 0x40b6b744, 0x84d79856, 0x86e5f0c6, 0x835a552b, 0xeb73ff95, 0xe428e392, 0x1d60bf15, 0x9ad09e35, 0x783e60ae, 0xb42feb61, 0x028a0343, 0x90febcfc, 0xdffb01c8, 0x1087ab1e, 0x84d8a8f9, 0xe5433763, 0x897548cc, 0xb4075ece, 0xd0a5a727, 0x0a84da19, 0x0f33964b, 0x5634a097, 0xb7cbac77, 0xe50340eb, 0x93f0ce63, 0x0c743ed4, 0xf6f6f015, 0x30f74cca, 0xbb0306a7, 0x3d037943, 0x22f2979e, 0x3591b2c3, 0x9a772e24, 0x5c5812fa, 0x9e733c8a, 0xaae1c943, 0xd0b7d924, 0x56b9c463, 0xfaedb557, 0x139b7aa5, 0xe173f123, 0x2d0686a2, 0x03cd0bef, 0x4f47c01e, 0x4cf9fb83, 0xba9d433e, 0x95a620d5, 0x62b67429, 0xe836067d, 0x606bc5f7, 0x36af81e2, 0xae0b8b30, 0x38ffe5fa, 0xb548228b, 0xc2a25bcb, 0x4ba139ee, 0xab214ad7, 0x66ef4f50, 0x5f8787fa, 0xcb5982b1, 0xdbb48ff8, 0x14eaa914, 0xe0874168, 0x3e578246, 0x488c1c11, 0x982039a2, 0xde096b35, 0xa420fb41, 0x197a1b67, 0x16eabc59, 0x0e689585, 0x24db72b7, 0xf89878c3, 0x04a5e854, 0x3346da02, 0xb4bb6a04, 0x278a0dd4, 0x7bb6e224, 0x92e219c3, 0x595f8a33, 0xedd87ae6, 0xa313cc9f, 0x385c1d3a, 0x0a8b1db5, 0xb192379d, 0x3a0be0eb, 0xb8ba269b, 0x1f0c62a5, 0x56307342, 0x8fc9ac94, 0xec91a232, 0xa7b8d3db, 0xfbf43a60, 0xa773463e, 0x72d5a4d1, 0x8ddf1755, 0x27da39bb, 0xa8d4668a, 0xd2f3bbfc, 0xa188d6af, 0x82ed668e, 0xb45f0032, 0xcdfd4ca0, 0x14e5a80d, 0x456594bc, 0x68efcdd5, 0x7dbf1f9d, 0x74599699, 0xfc8639e8, 0x4139e791, 0x9c06921a, 0xc5121d36, 0xd15b9425, 0x0670dca7, 0xbc60c353, 0xaa49e487, 0xa7cb5854, 0xd06ddbdb, 0xcb4be0d2, 0x8391ab98, 0xc750d7bf, 0x365340b1, 0x264677c7, 0xb76075a4 }; /** * The output sequence generated by reference ISAAC algorithm in C language. * For initial seeding is used SEED_1 data array. * 1024 signed 32-bit integers in hexadecimal representation. */ private static final int[] EXPECTED_SEQUENCE_1 = { 0x182600f3, 0x300b4a8d, 0x301b6622, 0xb08acd21, 0x296fd679, 0x995206e9, 0xb3ffa8b5, 0x0fc99c24, 0x5f071faf, 0x52251def, 0x894f41c2, 0xcc4c9afb, 0x96c33f74, 0x347cb71d, 0xc90f8fbd, 0xa658f57a, 0xc5c29e18, 0x6249fa29, 0xbae16ffa, 0x25c871bd, 0xf4c75e24, 0x5ab3eab9, 0xac450b8f, 0x1629cfa4, 0x0016e86f, 0xf27c4d0d, 0x67648b17, 0x05c04fce, 0x44d3ff79, 0xc6acd20f, 0x472fd994, 0x842131c4, 0xead4a900, 0xc01eda0d, 0x9e604c7b, 0xfb8a0e99, 0x02e17b6f, 0xe8b4f627, 0xc7041eae, 0x42d19cd7, 0xa358eb94, 0x19ca2158, 0x6be6ce81, 0x4b90a4de, 0x26f0774d, 0x4e83930a, 0x2492d476, 0xb97ffabe, 0x675cc8ae, 0x4cfdd254, 0x5d3c00ea, 0x7bba5ead, 0x6f461810, 0xbef63eea, 0x72eb767b, 0xed6e963b, 0xb026016d, 0x17cb7ebf, 0xa7dc6e56, 0xf460bdf1, 0x1ffe0e04, 0x902b347d, 0x02c0d8ab, 0x98cb3f8b, 0x6f359a39, 0x9521825f, 0x9026d97e, 0xde342516, 0x890a740c, 0x0f2969e4, 0x2e7ea9ed, 0x394b8a4f, 0x1bdf1fd0, 0x15d565b4, 0xbaf0406d, 0x4dac20db, 0x03359832, 0xe34802d5, 0xcc5fff02, 0x0935ad6e, 0x7c53c9b2, 0xb10b5d29, 0x4fbb94be, 0xd7e48546, 0xb7cfa23c, 0x7f081c9a, 0xe099baf1, 0x9c7dc323, 0xb831ad14, 0x5b563101, 0xfa55319b, 0x060ded54, 0xc5418124, 0x765f0dba, 0x1ad3d9d5, 0x3f07ec49, 0xdd5e06c6, 0xc230e2ac, 0xc6ba1971, 0x9cc17bcc, 0x10b22a22, 0x7dfc8c7f, 0xb3310333, 0x205530ee, 0xdbf38a8f, 0x003a02f5, 0x007e96a3, 0x36658201, 0x08dfd64f, 0x6275acf3, 0x3d29669b, 0x6b2f4538, 0xb0cc336b, 0x1d3043eb, 0x1ad1d764, 0x4c655b84, 0x7a725bb2, 0xb3fc5c66, 0x80b4b915, 0xb2cbd9e4, 0x2992dfc6, 0xdf8be548, 0xb310d06e, 0x436385c6, 0x44d6e893, 0x44c4d79d, 0xe3bb2064, 0xe41ea465, 0x3ff4cc70, 0x9d21ac42, 0x672c3725, 0xa43a1d02, 0xfd84b19b, 0x5b6fb132, 0x4af40896, 0xe15000a6, 0x7cab12f6, 0x8b8e753c, 0xfb253454, 0x359ac366, 0x67822b45, 0x290a1140, 0xade6e428, 0x6095efcb, 0x99d8d9e6, 0xa5b5981d, 0x332c95d6, 0xaf5cfcab, 0x161f5ca6, 0x1844cee2, 0xffb8ab5c, 0x82fccaeb, 0x49ecf97a, 0x7a60fabd, 0xf9585a3a, 0x4eb6bd32, 0x3b347002, 0xf4930dba, 0x5d21d51e, 0x64e8e3f4, 0x52801fa8, 0x71ce907c, 0x872783a4, 0x0761dc80, 0x5c509848, 0x41ba2adc, 0x7e2f5520, 0x85c5eec2, 0x368d3d00, 0x5fc7c5f3, 0xb849d785, 0xd95f25b3, 0x79801fd5, 0xbf2443d6, 0x360d41cd, 0x651b11c0, 0x801a89ca, 0x8b9e6b94, 0xfde283c4, 0xcc5e6974, 0x2b2f4c09, 0x8b2160a8, 0xdbf57f01, 0x76aa1c4e, 0x11f0831a, 0x54713d17, 0xc99a2639, 0x4c373e7a, 0x09e6e57f, 0x71f63b07, 0x7be3f02e, 0x2c907ade, 0xe5f489f6, 0x0b0cd6da, 0xb566e14c, 0x0f955969, 0xa0e5710b, 0x80d8c2de, 0x9971e496, 0xc7efbc2f, 0x97a48e53, 0x2d845c0d, 0xe1194b0e, 0xad2ba480, 0xd5253552, 0xca890b31, 0x60060afb, 0x89dae927, 0x565e2229, 0x43abc21c, 0x03dd14a5, 0xbbadd184, 0x9e979702, 0x2f659883, 0xf313adec, 0x621bd7ca, 0xb6470834, 0x4c3901c6, 0x32028bb8, 0x9ded8244, 0x66907654, 0x0a06b272, 0x4a8ec630, 0x4207d36f, 0x3e7a8b49, 0x13871be7, 0xbf7af48e, 0x3de0df39, 0x0e864542, 0x8c090a23, 0xaf90e49e, 0x97661c5e, 0x365aa66c, 0x0073e342, 0x9c8ac447, 0x6f57e7ce, 0xd5be7ffa, 0x89651d84, 0x53f78eaa, 0x8173dc04, 0xd70b1e10, 0x43c1a57b, 0x10c8a5ab, 0xed6abd62, 0x2f840e43, 0x4873d91e, 0x49f413fc, 0x5d89a1c1, 0xd3a388fc, 0x96c59cf4, 0x456f1edd, 0x3dd20023, 0xa264e933, 0xd32956e5, 0xd91aa738, 0xe76dd339, 0x7a68710f, 0x6554abda, 0x90c10757, 0x0b5e435f, 0xaf7d1fb8, 0x01913fd3, 0x6a158d10, 0xb8f6fd4a, 0xc2b9aa36, 0x96da2655, 0xfe1e42d5, 0x56e6cd21, 0xd5b2d750, 0x7229ea81, 0x5de87abb, 0xb6b9d766, 0x1e16614c, 0x3b708f99, 0x5cf824cd, 0xa4ca0cf1, 0x62d31911, 0x7cdd662f, 0xcb9e1563, 0x79ae4c10, 0x080c79ec, 0x18080c8e, 0x4a0a283c, 0x3dde9f39, 0x09c36f90, 0xad567643, 0x08294766, 0xb4415f7d, 0x5597ec0f, 0x78ffa568, 0x8bace62e, 0x4188bfcd, 0xc87c8006, 0xafa92a6d, 0x50fc8194, 0xcae8deba, 0x33f6d7b1, 0x53245b79, 0x61119a5a, 0x7e315aeb, 0xe75b41c9, 0xd2a93b51, 0xec46b0b6, 0x1ed3ff4e, 0x5d023e65, 0xadf6bc23, 0xf7f58f7b, 0xe4f3a26a, 0x0c571a7d, 0xed35e5ee, 0xeadebeac, 0x30bcc764, 0x66f1e0ab, 0x826dfa89, 0x0d9c7e7e, 0xe7e26581, 0xd5990dfb, 0x02c9b944, 0x4112d96c, 0x3ff1e524, 0xc35e4580, 0xfdfef62d, 0xb83f957a, 0xbfc7f7cc, 0xb510ce0e, 0xcd7411a7, 0x04db4e13, 0x76904b6d, 0x08607f04, 0x3718d597, 0x46c0a6f5, 0x8406b137, 0x309bfb78, 0xf7d3f39f, 0x8c2f0d55, 0xc613f157, 0x127dd430, 0x72c9137d, 0x68a39358, 0x07c28cd1, 0x848f520a, 0xdd2dc1d5, 0x9388b13b, 0x28e7cb78, 0x03fb88f4, 0xb0b84e7b, 0x14c8009b, 0x884d6825, 0x21c171ec, 0x0809e494, 0x6a107589, 0x12595a19, 0x0bb3263f, 0x4d8fae82, 0x2a98121a, 0xb00960ba, 0x6708a2bc, 0x35a124b5, 0xbccaaeed, 0x294d37e5, 0xd405ded8, 0x9f39e2d9, 0x21835c4d, 0xe89b1a3b, 0x7364944b, 0xbd2e5024, 0x6a123f57, 0x34105a8c, 0x5ad0d3b0, 0xcc033ce3, 0xd51f093d, 0x56a001e3, 0x01a9bd70, 0x8891b3db, 0x13add922, 0x3d77d9a2, 0x0e7e0e67, 0xd73f72d4, 0x917bdec2, 0xa37f63ff, 0x23d74f4e, 0x3a6ce389, 0x0606cf9f, 0xde11ed34, 0x70cc94ae, 0xcb0eee4a, 0x13edc0cb, 0xfe29661c, 0xdb6dbe96, 0xb388d96c, 0x33bc405d, 0xa6d12101, 0x2f36fa86, 0x7ded386f, 0xe6344451, 0xcd57c7f7, 0x1b0dcdc1, 0xcd49ebdb, 0x9e8a51da, 0x12a0594b, 0x60d4d5f8, 0x91c8d925, 0xe43d0fbb, 0x5d2a542f, 0x451e7ec8, 0x2b36505c, 0x37c0ed05, 0x2364a1aa, 0x814bc24c, 0xe3a662d9, 0xf2b5cc05, 0xb8b0ccfc, 0xb058bafb, 0x3aea3dec, 0x0d028684, 0x64af0fef, 0x210f3925, 0xb67ec13a, 0x97166d14, 0xf7e1cdd0, 0x5adb60e7, 0xd5295ebc, 0x28833522, 0x60eda8da, 0x7bc76811, 0xac9fe69d, 0x30ab93ec, 0x03696614, 0x15e3a5b9, 0xecc5dc91, 0x1d3b8e97, 0x7275e277, 0x538e1f4e, 0x6cb167db, 0xa7a2f402, 0x2db35dfe, 0xa8bcc22d, 0xd8c58a6a, 0x6a529b0b, 0x0fd43963, 0xafc17a97, 0x943c3c74, 0x95138769, 0x6f4e0772, 0xb143b688, 0x3b18e752, 0x69d2e4ae, 0x8107c9ff, 0xcdbc62e2, 0x5781414f, 0x8b87437e, 0xa70e1101, 0x91dabc65, 0x4e232cd0, 0x229749b5, 0xd7386806, 0xb3c3f24b, 0x60dc5207, 0x0bdb9c30, 0x1a70e7e9, 0xf37c71d5, 0x44b89b08, 0xb4d2f976, 0xb40e27bc, 0xffdf8a80, 0x9c411a2a, 0xd0f7b37d, 0xef53cec4, 0xeca4d58a, 0x0b923200, 0xcf22e064, 0x8ebfa303, 0xf7cf814c, 0x32ae2a2b, 0xb5e13dae, 0xc998f9ff, 0x349947b0, 0x29cf72ce, 0x17e38f85, 0xf3b26129, 0xd45d6d81, 0x09b3ce98, 0x860536b8, 0xe5792e1b, 0x12ad6419, 0xf5f71c69, 0xcbc8b7c2, 0x8f651659, 0xa0cc74f3, 0xd78cb99e, 0x51c08d83, 0x29f55449, 0x002ed713, 0x38a824f3, 0x57161df6, 0x7452e319, 0x25890e2e, 0xc7442433, 0x4a5f6355, 0x6a83e1e0, 0x823cedb6, 0xf1d444eb, 0x88381097, 0x5de3743e, 0x46ca4f9a, 0xd8370487, 0xedec154a, 0x433f1afb, 0xf5fad54f, 0x98db2fb4, 0xe448e96d, 0xf650e4c8, 0x4bb5af29, 0x9d855e89, 0xc54cd95b, 0x46d95ca5, 0xef73fbf0, 0xf943f672, 0x86ba527f, 0x9d8d1908, 0xf3310c92, 0x05340e15, 0x07cffad9, 0x21e2547e, 0x8c17eff0, 0xd32be060, 0x8aba3ffb, 0x94d40125, 0xc5a87748, 0x824c2009, 0x73c0e762, 0xcdfec2af, 0x0e6c51b3, 0xa86f875e, 0xbc6172c7, 0xf7f395f1, 0x3f7579b3, 0x7aa114ed, 0x165b1015, 0xd531161a, 0xe36ef5bb, 0xdc153e5f, 0x1d0cb81b, 0xceffc147, 0x6079e4ce, 0xc3142d8f, 0xa617a083, 0xb54fed6f, 0xc3c7be2c, 0x02614abf, 0x6fb5ce56, 0xd21e796c, 0x2d0985de, 0xe9f84163, 0xc1a71e3c, 0x2887d96f, 0x57c4c925, 0x05efe294, 0x88157153, 0x9a30c4e8, 0x8854a0a1, 0x02503f7d, 0x0cd6ef81, 0x1da4f25a, 0xe8fa3860, 0x32e39273, 0x4c8652d6, 0x9ab3a42f, 0x9ead7f70, 0x836d8003, 0x6cbe7935, 0x721502dd, 0x5a48755c, 0x07497cae, 0xde462f4d, 0x92f57ea7, 0x1fe26ce0, 0x27c82282, 0xd6ec2f2b, 0x80c6e402, 0xce86fdfc, 0x52649d6c, 0xc798f047, 0x45bae606, 0x891aec49, 0x66c97340, 0x9ca45e1c, 0x4286619c, 0xf5f9cc3b, 0x4e823ad3, 0xc0d5d42a, 0xaee19096, 0x3d469303, 0xfe4cb380, 0xc9cd808c, 0x37a97df6, 0x308f751f, 0x276df0b4, 0xe5fbb9c7, 0x97ca2070, 0x88412761, 0x2ce5d3d5, 0xd7b43abe, 0xa30519ad, 0x26414ff3, 0xc5bde908, 0x275ead3a, 0x26ceb003, 0xbf1bd691, 0x037464c0, 0xe24124c0, 0x81d4cc5f, 0x484525e4, 0x1c3a4524, 0x9e7e4f04, 0xe1279bff, 0x6dd1943a, 0x403dae08, 0x82846526, 0xd5683858, 0x29322d0d, 0xa949bea2, 0x74096ae7, 0x85a13f85, 0x68235b9d, 0x8ef4bce6, 0x142a6e85, 0xdad1b22a, 0xb7546681, 0x959e234e, 0xfd8650d8, 0x3e730fa8, 0x56f55a71, 0xd20adf03, 0x7cdc78a2, 0x19047c79, 0x253b1d7a, 0x4389a84a, 0x0aeb8165, 0x9c15db3b, 0xaafef5a7, 0xbe8b06b2, 0xb5fe87c0, 0xe2a4ef71, 0xd9d711f9, 0xecfcf20b, 0x80fac4c2, 0xbbb8abc4, 0x239e3b0a, 0x858129a6, 0xd97cd348, 0x8a30738a, 0xc5b71937, 0xd649a428, 0x18c1ef9a, 0x75c08a36, 0xc921f94e, 0xdf9afa29, 0x040f7074, 0x72f5972f, 0x84ef01da, 0x2cb7b77f, 0x867027d7, 0x9ce0199d, 0x71865c4c, 0x7a36af93, 0x6c48ddd8, 0x19b48fd0, 0x75f4e9e2, 0x0084cfe5, 0x63bfd4d8, 0x9783cdee, 0x64f2632c, 0xf1b20eaf, 0xcc8bfa2d, 0x39ddf25a, 0x740e0066, 0x9ddb477f, 0x12b2cfb6, 0xd067fce5, 0x1a4b6a53, 0x001cdb95, 0x12c06908, 0x531ac6bf, 0x513c1764, 0xf7476978, 0x1be79937, 0x8a8dfaa3, 0x70a1660e, 0xfb737b1a, 0x3f28ee63, 0xc1a51375, 0x84bd6dd6, 0xe4a51d21, 0xeafed133, 0x22ae0582, 0x4d678f26, 0xf854b522, 0x31907d62, 0x4b55dd99, 0x04d3658a, 0x19c1e69c, 0xc6112fdd, 0xc66699fd, 0xa0eabe6b, 0x3724d4dc, 0x0d28fe52, 0x46819e2e, 0x96f703ac, 0x21182ca3, 0x2e7b022f, 0x31860f13, 0x1956b1e5, 0xe1322228, 0x08063a85, 0x1d964589, 0x94d90481, 0x53c078f3, 0x5afb43ae, 0x1e3437f7, 0x877eb7b4, 0x5d67133c, 0xa385cb2c, 0xb82f2703, 0xef05ee06, 0x931dd7e2, 0x10d210aa, 0xe21339cc, 0x479c3a22, 0x613b67b2, 0x33c5321c, 0xa5f48ac4, 0xba5590c8, 0xeb244925, 0xd0ef3cc9, 0xd8c423fb, 0x15cfcc5c, 0x1feb2e4f, 0x36ec0ea3, 0xdbef7d9f, 0xd5ec6bf4, 0x3d3dff8a, 0x1e04a7f7, 0x8766bb54, 0x9a1d7745, 0xc79a1749, 0xb8d2486d, 0x582e3014, 0xa82427f5, 0x65dfa427, 0xbc5654c1, 0xbd086f26, 0x0451516d, 0xff4cfc35, 0x81f2864d, 0x31860f05, 0xd0638e1a, 0xb059261d, 0x3d1e9150, 0x21e2a51c, 0x82d53d12, 0x1010b275, 0x786b2908, 0x4d3cfbda, 0x94a302f4, 0x95c85eaa, 0xd7e1c7be, 0x82ac484f, 0xd24daa9a, 0x70055204, 0xbf27ef0d, 0x740ebb34, 0x47a1ff2f, 0x037e5e95, 0x3362d8d3, 0xb08c9e58, 0x7036b9a5, 0x3354197a, 0x326c9f12, 0xab99166e, 0x6c5d388b, 0xb222a768, 0x1bf121c5, 0x2ef76080, 0xb0658121, 0x8331bdd3, 0x64b5cd35, 0xc3c7fbe4, 0x576e7d5e, 0x1cbdc1b2, 0x5c54b675, 0xbffd76e3, 0x2ad7b53f, 0xe596de29, 0xc2d972db, 0xf1c92f34, 0x6af3ded6, 0xec317d66, 0x6a17bed5, 0x27750e9e, 0x8d950cb3, 0xb07688cc, 0x17a6ddd8, 0x5bf140e0, 0xed8713e0, 0x05890caa, 0xf66e4d73, 0x5ea0347f, 0xf535f2f5, 0x240c70f3, 0x5e922192, 0x8e59f944, 0x3185f7a7, 0x852dd342, 0x58c8e53b, 0x760071c5, 0xadb76f78, 0x185ab80c, 0x9d84df28, 0x4d2056da, 0x69bebccc, 0xa9fcb5f8, 0xb9d3ac81, 0xcc374aac, 0xc04e1ee9, 0x72634634, 0xb3bbf485, 0x1eb91de5, 0x5e8de602, 0x211cd561, 0xb03404e6, 0x051f3a53, 0xb263569c, 0x96cbc54f, 0x9eaf58e6, 0x32e9f0f2, 0x370f1cd9, 0x15bf840d, 0x1dbd5d06, 0xb04d214d, 0x6d1697ee, 0xc4b6fce1, 0x1b95f82d, 0x46979ca6, 0x0354cfc8, 0xd5950d3d, 0x10db605d, 0x18af3572, 0x990ec7a8, 0x2a0fe87f, 0x7937dbb8, 0xee376c86, 0xcc9f9070, 0xc953caa8, 0xd5c7c2ed, 0xee4f752e, 0x84f302f7, 0x1e08a48e, 0x44a5da35, 0x0fab83e2, 0xbb7cb9df, 0x2590afe1, 0xfef026aa, 0x83dbd643, 0xbe916d11, 0x27f7fba9, 0x82135d43, 0x6c5fa2a4, 0xe1e1370e, 0x51534581, 0x4cd9def3, 0xd94b4990, 0x74772379, 0x59264a1d, 0xc1dcdd8e, 0xed4ef1e9, 0xf29d901a, 0x68ecd0ad, 0x9ac31f92, 0x839a285e, 0x46447122, 0xc0e56c6e, 0xb09a4b83, 0xa9500b90, 0xda83c4e5, 0x4175b2f8, 0xeb4ddb4a, 0x236c6f2e, 0xeeb57a32, 0x2626e708, 0xa9d35265, 0x6ab3e9ab, 0xf12fcc1f, 0x1c317c43, 0x66c34fb3, 0xe17e58a0, 0x0295d4a1, 0x40cd40f9, 0x72700bb0, 0xd591e61e, 0x3e96b29b, 0xb50d5db3, 0xa3715dcc, 0x3405bcb4, 0x0e034d65, 0x210a4a2b, 0x7c302f2c, 0x24e8bef6, 0xa5135147, 0x0607ef80, 0x01f86c8f, 0x2c792c8a, 0x6ab73133, 0x6f436006, 0x09bf22a6, 0x1fde4622, 0x9841bd1c, 0xb23a7ad7, 0xdad579a4, 0x431229e9, 0xfa5dcb2d, 0x7da4f790, 0xa9b2c914, 0xcd191ced, 0x7a05e4aa, 0x73af1889, 0x192667b3, 0x538d4540, 0xacdbca81, 0x6b9d9e90, 0xc0462bba, 0xfcf5a7b9, 0x7b5c2904, 0x41a83c83, 0x7e69828f, 0x328a3bab, 0xdcd0f182, 0x1d113bcd, 0x1fb5c81c, 0x2d52afa0, 0x2d0c6111, 0x2a02ce14, 0x3fcd2c15, 0x54d574f9, 0xde57e605, 0x85dbb53d, 0xfc7cc003, 0x769c74d9, 0x6f834a4f, 0x79b3b87e, 0xe3d7c853, 0xa83e14b2, 0x3b1fc1ad, 0xb7dc2988, 0xb60ed652, 0xda3e3d1a, 0x5f2f680c, 0xb46e08da, 0x589b77da, 0xcef68535, 0x1c05d7a6, 0x24572da1, 0x939b02a5, 0xccd08d13, 0xdfa22970, 0xdff7581b, 0x2d5fade6, 0x5cfd3389, 0xce26cbb1, 0x376d7fd0, 0x02811e2e, 0xcc8030ab, 0xa7a4c9dc, 0x81db0ca7, 0x15a1bcef, 0x2045c6b5, 0x52c2f563, 0x6c829737, 0xb4f3384f, 0xb14d2f2b, 0xe102e00a, 0xba359973, 0x6045dd8b, 0xd0a5e531, 0xd679300f, 0xaabec63e, 0x526ad900, 0x95224f33, 0x723d6d44, 0x83584ad4, 0xa14ed869, 0x727bb03a, 0xdde37025, 0xb29d6587, 0xc3f3971d, 0x725933c2, 0x68f3eda4, 0xf73e9fdc, 0x944afd04, 0xa4c5e05f, 0x477f70ae, 0xffebfc90, 0xc21cff70, 0x84c68e32, 0xe82a3d6b, 0xba243867, 0x511c142f, 0x2062b8ac, 0x274a119f, 0x772afba2, 0x88a3974d, 0x205cf0de, 0xe45d5a2a, 0x1745056b, 0x2c8138f5, 0x982938a7, 0xfb265af9, 0x700c2cdf, 0x93e549b4, 0xb8abd353, 0xd74073e8, 0x289caf2a, 0x63e802e9, 0xc2f9adb7 }; /** * The output sequence generated by reference ISAAC algorithm in C language. * For initial seeding is used SEED_2 data array. * 1024 signed 32-bit integers in hexadecimal representation. */ private static final int[] EXPECTED_SEQUENCE_2 = { 0x67ae8a37, 0xa78322ea, 0xb9394933, 0x61f6e3c5, 0xbea576f1, 0xbb958f18, 0x12ce6479, 0xc593d5de, 0xdef281a0, 0x8435bb62, 0xf20b44db, 0x8a98479a, 0xbf2b8b66, 0x1080265e, 0xf0f8f12f, 0x021fa7f3, 0x81d2ed59, 0xb224a5f8, 0x0c1ff346, 0x92007ea8, 0x8fd1ce43, 0xeced69f5, 0x651fe09a, 0x45cf2c3e, 0x449b2b1e, 0x4f136be5, 0x8240cc97, 0xca979afa, 0x33b6a857, 0x7300f4f3, 0x79755d71, 0xcf11dd62, 0x916b7e04, 0x02076c0e, 0x9b4e3e68, 0x04836ed5, 0xf1b492c6, 0x887ef90c, 0x091b68f6, 0xaf7f0d3b, 0x89d7e5c1, 0x2b28fff7, 0xe6280e4f, 0x6681a805, 0xcb270bbb, 0x8e037463, 0x31a125f7, 0x0ba3c135, 0x7c2e8b3e, 0x6e21e06e, 0xc8b336ba, 0x08d677c3, 0x469fd05c, 0x71528649, 0x2024c602, 0x000e4f99, 0xb03395b1, 0x0a12d960, 0x68b15274, 0x7c415c07, 0x047c739b, 0x46658905, 0x45512a3c, 0x7f4cd2ff, 0x3d4d4ef6, 0xd7016dad, 0x6074bbf0, 0xbeaa55eb, 0xc519d326, 0x3ad349fd, 0x2fec4649, 0x14fa40ae, 0x96b51341, 0x2bf08ef1, 0xd1d374e4, 0x44168b14, 0xb01bee9b, 0x0b3f4332, 0xc799b9da, 0x76fc7dbd, 0x8c368a57, 0xe4cd2cad, 0xeeb0318a, 0xc2152908, 0x2b707a0e, 0x73457464, 0xc08e92a0, 0xfcdfca5b, 0x1320ed43, 0x333b77b9, 0x2e70948a, 0xa77d94f7, 0xbc1fb9fa, 0xa8ad15a1, 0x3c47b0f4, 0x867c4a8f, 0xb85784e0, 0x8a854e80, 0x456c8700, 0xc28f3a01, 0x415da6aa, 0x1315c6d8, 0x70a4ca70, 0xfdea940e, 0x686fbdc9, 0xda649eba, 0x661196f7, 0x795b5d27, 0xe10c78fa, 0x2fd89cf3, 0x61e850da, 0x00c49764, 0xee51d841, 0x00c18025, 0xdea163b3, 0x8b1b2080, 0x6abdd683, 0xe560c731, 0xc661b3e0, 0x23a3ff08, 0xa7579919, 0xfa443cba, 0x480bd220, 0x0a11740b, 0xb4b327c7, 0x831a0840, 0xb7c50ff3, 0x4266dff1, 0x835d0665, 0x52593038, 0x3917fb8e, 0x88c3b400, 0x05fb8823, 0xc9eaa89f, 0x6957fd17, 0x8dbcb8fe, 0xdec10c3c, 0x918d5fd8, 0x6af8619a, 0x8f472255, 0xc2f757b7, 0x9d62e804, 0x45e0e276, 0x482597d3, 0x06190214, 0x172b8501, 0xe91d7b8c, 0x4ee985fc, 0x3ecbf47a, 0xbbae5d98, 0x1f0bbdeb, 0x0d38208e, 0x6d4cb3e3, 0xa70050c4, 0xf0db408e, 0xddb6f1a7, 0x4bc4bc61, 0x90e1c1db, 0x203770dc, 0x39959247, 0xe2e0376a, 0xf70a8521, 0x81c759b2, 0x24d9915b, 0x09cc2ec3, 0x0fd0bff9, 0x58875636, 0xee78837e, 0x025a7eee, 0x4226859f, 0x85e21806, 0x9c1328bd, 0x0522fda0, 0x585441aa, 0x366f9ea0, 0xeb70934f, 0x0e394c41, 0xfa801419, 0x2b6d4c3e, 0xb09775fe, 0x3f0184ae, 0x3ace3518, 0xf80bf893, 0x9754753b, 0x78c46b93, 0x281e1918, 0x0dfcc5ee, 0xc0401601, 0xf8b11ce9, 0x9f799306, 0xb65c4232, 0x12ee4f73, 0xade72a42, 0x0ce54d71, 0xa6780e69, 0xe73bd8f9, 0xc245228f, 0x5fa2ed1a, 0x11627d1d, 0x2617ea2f, 0xd7404db6, 0x228fb877, 0xc5379535, 0xfe00008d, 0xc5f1491e, 0x1a3bdb0e, 0x9a90cc98, 0xa0abe3f5, 0xac7a0d18, 0x87bb3538, 0xa224baf7, 0xf2496ca4, 0x6a5b9bd6, 0x9a7da8d8, 0x72419677, 0xa36aec4d, 0x2a08ac0d, 0xfc4d7b21, 0x25f2aad0, 0x4f7146d4, 0xb4a603bd, 0x194e9792, 0x8f60cf1c, 0xed8ae037, 0xa47f90b1, 0x5eec55a3, 0x326c33d4, 0x6f79168f, 0xbcfc27fa, 0xd9e76d04, 0x79430e33, 0xd0c3b727, 0xd4bb06af, 0x8805066b, 0xaaef1130, 0x04958fef, 0x2e3270f4, 0xf5a8ffe8, 0x2a089c72, 0xff411bfc, 0xd6ed9552, 0x6253f5ef, 0x0c836c2f, 0xb79471b0, 0x127d177c, 0xf901cefa, 0xff75dc46, 0xde79ec4f, 0xe9f1f182, 0x9d28d8cd, 0xfcc98a94, 0x227670c2, 0x46b7c48c, 0x8fd77dcb, 0x60bc6d66, 0xe775322d, 0x0def2251, 0xf3dd14cb, 0x6c3f3468, 0x87696244, 0x10cca0be, 0x1d7fa716, 0x955b963b, 0xe53b6074, 0x77af9ec4, 0xfc856100, 0x91a06dc7, 0x8d55e3f1, 0xf8c805a3, 0xf3a1cb7a, 0xbcd51c6d, 0x301fdcdf, 0xdbcbcc54, 0x8b85fe57, 0x946d707e, 0x388a2ed4, 0xc4b93a5b, 0xd48631d2, 0xae2b4f28, 0x5b731392, 0xdf6e621e, 0xc4590c30, 0xa3a23cd5, 0xbfce9899, 0x4620cff9, 0x966c8c3f, 0x7a302556, 0x3fe549fa, 0x67533e77, 0x80250302, 0xcd899fe7, 0x694e77ea, 0x0879525d, 0xab6675e4, 0x763f8b35, 0x7684e6a1, 0x8fa35070, 0xe9fccaf1, 0x2d7195b7, 0x85b45186, 0xab799317, 0x2c84bd2c, 0xf8354c09, 0x02d96875, 0x8fdcc390, 0xf6af5aec, 0x2a584739, 0x8a1ba7e9, 0xea46f9b2, 0x98acd24f, 0xfc8a3a24, 0xa496eff9, 0x625c30ea, 0xc6ea0535, 0x3ed3b5d6, 0xffcd675d, 0x0b1719f6, 0x1b1c4e7b, 0x3206a672, 0x62fc1851, 0xa6a4c781, 0x78bbdbbe, 0x06c1c8ce, 0x5747c340, 0xfff7ab9c, 0xebaf9370, 0xf7b185a8, 0xf8309f84, 0xfa1601de, 0xf9fc8780, 0x59c2f8bd, 0xe74fcd5b, 0xf115f57f, 0xddda3332, 0x2ee56568, 0xa2243659, 0x9d6d578f, 0xbb507574, 0x95d44e0e, 0xdbdf2bc3, 0x0dc1b750, 0xc6a24241, 0x207d7115, 0xc337d024, 0x3817ef9a, 0xe9f12ccf, 0x4d67fc7d, 0x3da57a2b, 0x000e09a5, 0xe739c5a2, 0x7b7e1613, 0x23d576fe, 0x6941a210, 0x57521190, 0xdc4359c0, 0xd8eed2f8, 0x7862904f, 0xfc179a41, 0xeee2716e, 0x362cf76a, 0x0a087072, 0x3e6e2fa9, 0xaf2a0efb, 0x221d513f, 0xf054d856, 0xc3297367, 0x1c0998c8, 0xa664172f, 0xe2637c8e, 0xc17ac7d4, 0x0e041f43, 0x0d9c0ae4, 0x9346dacf, 0x7fb2a015, 0xe92276c2, 0x21478bfe, 0x119e2d0c, 0x5f76aeaf, 0xbe21aaec, 0x63174d5f, 0x13b796c3, 0x0fa0eba1, 0xe2f7277a, 0x3f555b42, 0x0215c7e4, 0x96266efa, 0x2953a4d1, 0xadfc171a, 0x396234a7, 0x560c0279, 0xefa6d2c6, 0xf48d9b5a, 0x4131c7b1, 0x9e302f70, 0x637c9f23, 0x22637330, 0x09927e76, 0x0898d1d6, 0x1b797274, 0x9ad491a2, 0xa2df3627, 0x012c3ed0, 0xc19c09d7, 0xa2fdaf56, 0x5b91f8fd, 0x3b7c49c9, 0x25694d29, 0xd7b42e9c, 0xa7be0053, 0xa91f1761, 0xd89e8b2a, 0x67846097, 0x76bd4006, 0xb8eb0712, 0x859bf877, 0xca42d70c, 0x24e80a69, 0xd92bc598, 0x55498c1e, 0x86deba8b, 0xf7c340b7, 0xa36caa12, 0x0631ddec, 0xc0146fe8, 0x2f959ef3, 0xf8400f0c, 0x58f676a0, 0x4ae4fe13, 0x9c4af056, 0x9e6f19d6, 0x12a9eb69, 0x1aeed54e, 0x34c91114, 0x97128045, 0x920d1f59, 0xffe7fbaa, 0x2db4a671, 0x6e6ff7aa, 0xd40d46bd, 0x1578f939, 0x15c5cbc6, 0xff356fd0, 0xd5d1680c, 0x5b11d14d, 0xe75541c0, 0x0fe2e2ba, 0x3ad55308, 0x8f036a69, 0xa9bfc3cd, 0x87685338, 0x510092b4, 0x1f66622a, 0x996337b2, 0xc531891f, 0x98371a93, 0xd9630100, 0x513ff133, 0xcf8381da, 0xed12e8e9, 0xe3ce7c7b, 0x8f731ab5, 0x511ba7c2, 0x9d754e87, 0x244603ac, 0xfd9985e1, 0xc1581765, 0x84e50a12, 0xa0ab0034, 0x63ee60c2, 0xdf5ab248, 0x09b42202, 0xca87f896, 0xca6ae5f0, 0xa569d829, 0x977cf29b, 0xd56a2a2f, 0x85ad1532, 0xfa2a131a, 0x00784081, 0x81f0e466, 0xebd340d3, 0xc37ad0e4, 0xd0aa6d7a, 0x36d2551f, 0xd6ff8448, 0xc7b89445, 0xa43421ad, 0x3be75400, 0x557a61ef, 0x0f519b14, 0x56503579, 0x1c8d164d, 0x0dcef35b, 0x3d9f1f2a, 0x56d056f2, 0x5d8fd4ec, 0xa481a350, 0x7cadd9c0, 0x70375ce2, 0x83263d2a, 0x5826ea3b, 0xfa523ce7, 0x50c9438b, 0x74fca95d, 0x62967ef5, 0x11fd6429, 0xcbb8e28c, 0x67fb9e81, 0xdc9e1147, 0xa29672f7, 0x1cf310f7, 0xb1e1d8e6, 0x3f862ff3, 0x6ade0327, 0xa92f3686, 0xed79f165, 0x359e1620, 0x36c68936, 0xe46fe521, 0x0c5e36b0, 0x6d9d9cdb, 0xc095eecd, 0x566dd305, 0x6d96cd36, 0x5d115a80, 0x2a9489a8, 0xdd067488, 0x73acf831, 0x7392c0f0, 0x30707838, 0x92826042, 0x67c54548, 0xf830434d, 0xebe67854, 0xaefc9a41, 0xcabf703f, 0x5242c77f, 0x1f3867a9, 0x48174739, 0x8657c39e, 0xa11247e2, 0xb4e6624d, 0xc7ffe78a, 0x1e11a841, 0x6690244b, 0x8dcc9292, 0x5ce4dcc4, 0xebcba02d, 0x2ef6503d, 0x4fb880bc, 0xb949a759, 0x7bb18a1e, 0x5973d2e8, 0x577ad8a6, 0xa9d4992e, 0x1a248a0c, 0xcc4450ed, 0x7e0178d3, 0xe98a8f3f, 0x209fb330, 0xf7bf40fc, 0x632231b3, 0x7055fdaf, 0x7719e655, 0xf8d49413, 0xc200aa04, 0x8a41183a, 0xdfa217c1, 0xcd0c165d, 0x08fec61c, 0xef608048, 0xe19fae2c, 0xedc6f3ea, 0x859a69f9, 0x5f96c76d, 0x571aec69, 0x9cfe7fa4, 0x692baf70, 0xbb143cb4, 0xe8968678, 0xfcb77411, 0x02d3268d, 0xcdc3daa3, 0x514e78e9, 0xa231a480, 0x8ac10400, 0xe19a2ca1, 0xfa790fe1, 0x808fec9c, 0xe4760960, 0x62e9d051, 0x5c4b006b, 0x22eb9703, 0x426b5907, 0xfa1cd338, 0xa3b4811a, 0xad6185c1, 0x349efbc0, 0xeee28d42, 0x02531fc5, 0xd11b2c4d, 0x5b3bf865, 0xf4823687, 0x4f92b6b7, 0xfb641c60, 0x0c526fa9, 0x42438de8, 0xd5cbf7a0, 0x54ad0d1f, 0xb4e63f09, 0x666285eb, 0xe7ec0275, 0x57e7225a, 0xafe6b0e3, 0x17431cd7, 0x33bc9204, 0x8a9cbdde, 0x94d8fe7d, 0xc943f36c, 0x1348c3c6, 0x43cf9b8c, 0x5a84ae20, 0x6d372dea, 0xdb0b3c92, 0xf0f2a72d, 0x473a1fe7, 0x062416df, 0x0a12c61c, 0x3680c102, 0x8d0189db, 0x0824325f, 0xffb97ead, 0x0d8d353f, 0x4a4e6ec2, 0x76243bb7, 0xdabfbeee, 0xcd8410d7, 0xa30f17c3, 0x2b59ceef, 0xda27f7c0, 0x791d813b, 0xc0516741, 0xb363e4ff, 0x31ddbfb7, 0x49db1590, 0xd843513c, 0x8d317a75, 0xb24387df, 0x63fd4066, 0xa0fce498, 0x7b42de97, 0x30eddc0c, 0x071ad222, 0x3a9054c4, 0x5ce35298, 0x375be64b, 0x10af32c8, 0xa999ade1, 0xfa9f4d31, 0xfbe24a2a, 0x4c92714b, 0xcce3056a, 0xa81d616a, 0x3bb49213, 0x72fd2b0e, 0x1b46d17e, 0x92159bc7, 0x7462e172, 0x4fdc3e05, 0xf309c063, 0x9133532c, 0xe62d9341, 0x681a4871, 0xb1598525, 0x498ca388, 0x96a7ea81, 0x791c8a85, 0x2a33a1e2, 0x1e6abc87, 0xb21a4878, 0x65fac53b, 0x59162ae1, 0x22858a30, 0x40f4e569, 0xe5cb0023, 0x626cd2a0, 0xfe6d8fc8, 0xbb7ed7c3, 0x9a557393, 0xd0ff5e60, 0x2a20ed9b, 0x4eaafb5a, 0xbe9425bd, 0x63620ce1, 0x31ea24ed, 0x082e426a, 0x7ff35a73, 0xa67fbaa9, 0xd2e3c5b9, 0x1a90e96a, 0x71f19184, 0xb836b88b, 0xe51fa187, 0x42576438, 0x58d28776, 0x47bd92a3, 0x09816862, 0x295138ef, 0x23ab2bb1, 0xd7c584e0, 0x1793062f, 0xcc47e852, 0xc2eb9703, 0xe6812d93, 0xa4aa4d2e, 0x7f635b79, 0xa7407b29, 0x9724c087, 0x406e08ce, 0x6bf1d8b7, 0x9ef5b815, 0xf2c6f094, 0x86269ca2, 0x17fdaa4f, 0x9b645b61, 0x701bbbeb, 0x8de7bcb7, 0xd468266a, 0x48df44ae, 0x570b08ca, 0x7a5ba43b, 0xfc927312, 0x3461a3dd, 0x0ffe5943, 0x87060375, 0x8d8afed7, 0x83d20387, 0x77eabb51, 0xf86d045b, 0x71a47537, 0xa4485ea8, 0xfd2b6ac3, 0xb4ba1fcf, 0x31dcee82, 0x8b41cdf6, 0xeacde42b, 0x02de5fbb, 0xb6311aa8, 0x1596ee5d, 0x355cc39d, 0xbe1a87c1, 0x01e1df07, 0xfe413d46, 0x5e5e13ab, 0x30233fd6, 0x99449292, 0x34955dcc, 0x1f37d394, 0xd43639bd, 0x65c98eee, 0x67b85593, 0x1660b2a1, 0xfd86e9a0, 0x33bb6e5a, 0xdd5892fb, 0xa6832091, 0xd077d216, 0x353bfe9a, 0xb4a10726, 0xca1a536e, 0xed8af6c1, 0x41d167d1, 0x5f554941, 0x93f4032a, 0x83d83ae5, 0xc8866a05, 0xc36d1e1f, 0x95a082c5, 0xd85e6cad, 0x302bc384, 0x41fb8717, 0x61221cc8, 0xce9a44cd, 0x2884ec21, 0x9712152d, 0x419e4939, 0x32367b47, 0x238ee432, 0xd27f0b8e, 0xa3eaed24, 0x1eefd0a9, 0xf38a2400, 0x72c0d4b2, 0x8bdbdec1, 0x563a2b59, 0x0d50177b, 0xb01576ef, 0x7dd9f33e, 0x7905c120, 0x461712d6, 0x78265e93, 0x54a91f0b, 0xb88eb9c0, 0x9d8997af, 0xcb1d1296, 0xfa0a3a77, 0x5bb26dd9, 0xf6da78df, 0x53b8e80c, 0xc55cdaf6, 0x871a3971, 0x0bd8d322, 0xfa9e0a9c, 0x95949e0e, 0xe94f0edb, 0x15e06b25, 0x8b3e34cc, 0x980261a9, 0x3a8fe440, 0xc72330aa, 0xbff5c8b6, 0x486a08e6, 0x6b3f0668, 0x53c90761, 0x1dc2374b, 0xba623bb6, 0x720d9fff, 0x8454fada, 0x29f09563, 0x6512330e, 0x84370042, 0xda55c14b, 0xe6397b27, 0xdb03bcc7, 0xd6e27986, 0x483ad4f2, 0xe0e2c39a, 0x459e6792, 0x03c120ec, 0x13df7847, 0xc3ee77e1, 0xbcee7cd4, 0xdb3734ec, 0x0e19ebb2, 0x1501517a, 0x815190f7, 0xea30ba2c, 0xed58523c, 0x9dc64c08, 0x58d8753f, 0x1fa771c5, 0x7721fb09, 0xc64d1f60, 0xf407dc18, 0x6fdb1e33, 0x89abccb8, 0x2fab8715, 0xd8ee352e, 0x41bfa764, 0xda0267ee, 0x65794080, 0xe3095d65, 0x08e2148b, 0x173103b5, 0x55673978, 0x8d76b213, 0x6ed42e4b, 0xbe589395, 0xcf4c4d8a, 0xd331b237, 0x0af2f4cb, 0x202be7fa, 0x2e87bc27, 0x140a95df, 0xa0d1ef7e, 0x1031da30, 0x630f3ea6, 0x0e3b0991, 0xba7c0462, 0xca8a192c, 0x668417e2, 0x2c6e8ec5, 0x3f2e4372, 0x310927e9, 0xa87b5f4e, 0x21e3f285, 0x66aab4be, 0x96804f73, 0x097c363b, 0x76445811, 0xaf92fb77, 0x660010b7, 0x3ff5abbb, 0xdaa505d0, 0x17dc3488, 0x45dac66a, 0xa834d6eb, 0xacf0641f, 0x05576174, 0x28bf1858, 0x08829e92, 0xd3c5d530, 0x6acd00b2, 0xf36c0645, 0x4385cae7, 0x93b11f88, 0x3dfe1da7, 0x2d5df9d8, 0xd51d498f, 0x1d122b84, 0x2ca7619f, 0x670fba3a, 0xa59f3019, 0xd25ade01, 0x43ef2f88, 0x05cf6af4, 0x6fc3b5e0, 0x305a309e, 0xb7bce57b, 0x49c55693, 0xd59bd6d0, 0xdf13274c, 0xa5640917, 0xb8e88520, 0xf81fb865, 0x245967cf, 0x64420112, 0x97720fd0, 0x0ef913f8, 0x9fcf14f4, 0x99a86a60, 0x150ae075, 0x1b4be51e, 0x12fe7368, 0x23781feb, 0x3657b0c3, 0x90f84f92, 0x082f626d, 0xf057cef1, 0xf04fc2c1, 0x8767f311, 0x240ab838, 0x160564c0, 0x96f4460d, 0x2c7e5c83, 0xb44e6da1, 0x43f86ae1, 0xe3afdf03, 0x34173462, 0x197d8030, 0xb02d6ae7, 0x0cec076c, 0x0fb9d05e, 0x1fa74d99, 0x03f9636e, 0x03afa44d, 0x79ceb46c, 0x8b9e3158, 0xad87d846, 0xaf794612, 0xdd00ae31, 0xde8d63de, 0x229c5e66, 0x2df46e14, 0x3cbb35d1, 0x9832a55b, 0xaff3c01c, 0xaf4cc2be, 0x05095c2b, 0xee6be44f, 0x7b9bd378, 0x09a5f11f, 0xfddc340a, 0x010da0fa, 0x60874e63, 0x9f03a38b, 0xddfe1c05, 0x8dadcc16, 0x6df97114, 0xe0779adc, 0x8de82987, 0x83cca69c, 0x38b19e7c, 0xebc30d07, 0xb36f46cc, 0xee4d1453, 0x7522c310, 0x3a43d376, 0xab400f15, 0x4fedfa99, 0x02e7323e, 0x4c57f680, 0xe4190ae5, 0x6a5bba49, 0xd3c223d8, 0x1b87ab96, 0xaef4795f, 0xf457cd2d, 0x2bae8689, 0xa229c48f, 0x41bd5e74, 0x25cb3da8, 0xfd47e4d0, 0x0a241ffc, 0x16c0dba7, 0x6f1469fd, 0x810c16da, 0x66a7b33c, 0xe6c9e001, 0x9ccefde6, 0xd24f7adc, 0x1bcc8980, 0x37084252, 0xb779d5cd, 0x52e456d0, 0x313ba4de, 0xffb09943, 0x0e9d4e1f, 0x5a3c51ac, 0x6f055f04, 0xb2ac9a26, 0xb7fac64f, 0x27cc0c8d, 0x342bbac3 }; @Test void testReference1() { RandomAssert.assertEquals(EXPECTED_SEQUENCE_1, new ISAACRandom(SEED_1)); } @Test void testReference2() { RandomAssert.assertEquals(EXPECTED_SEQUENCE_2, new ISAACRandom(SEED_2)); } }
3,061
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/XoRoShiRo64StarTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo64StarTest { /** The size of the array seed. */ private static final int SEED_SIZE = 2; @Test void testReferenceCode() { /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro64star.c */ final int[] seed = { 0x012de1ba, 0xa5a818b8, }; final int[] expectedSequence = { 0xd72accde, 0x29cbd26c, 0xa00fd44a, 0xa4d612c8, 0xf9c7572b, 0xce94c084, 0x47a3d7ee, 0xb64aa982, 0x67a9b2a4, 0x0c3d61a8, 0x8f70f7fa, 0xd1edbd63, 0xac954b3a, 0xd7fe941e, 0xaa38e658, 0x019ecf61, 0xcded7d7c, 0xd6588891, 0x4414454a, 0xb3c3a124, 0x4a16fcfe, 0x3fb393c2, 0x4d8d14d6, 0x3a02c906, 0x0c82f080, 0x174186c4, 0x1199966b, 0x12b83d6a, 0xe697999e, 0x9df4d2f4, 0x5a5a0879, 0xc44ad6b4, 0x96a9adc3, 0x4603c20f, 0x3171ca57, 0x66e349c9, 0xa77dba19, 0xbe4f279d, 0xf5cd3402, 0x1962933d, }; RandomAssert.assertEquals(expectedSequence, new XoRoShiRo64Star(seed)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64Star(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64Star.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo64Star(new int[] {0x012de1ba}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final int[] seed = { 0x012de1ba, 0xa5a818b8, }; final XoRoShiRo64Star rng1 = new XoRoShiRo64Star(seed); final XoRoShiRo64Star rng2 = new XoRoShiRo64Star(seed[0], seed[1]); RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2); } }
3,062
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source32/Well44497aTest.java
/* * 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.rng.core.source32; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class Well44497aTest { /** The size of the array seed. */ private static final int SEED_SIZE = 1391; @Test void testReferenceCode() { final int[] base = { 0x2c2878c6, 0x47af36c4, 0xf422e677, 0xf08fd8d3, 0xee9a47c7, 0xba983942, 0xa2a9f9a5, 0x1d443748, 0x8fc260b2, 0x5275c681, 0x4a2f5a28, 0x2911683d, 0xa204c27e, 0xb20a6a26, 0x54ba33be, 0x67d63eb0, 0xdc8174cf, 0x3e73a4bc, 0x6fce0775, 0x9e6141fc, 0x5232218a, 0x0fa9e601, 0x0b6fdb4a, 0xf10a0a8c, 0x97829dba, 0xc60b0778, 0x0566db41, 0x620807aa, 0x599b89c9, 0x1a34942b, 0x6baae3da, 0x4ba0b73d }; final int[] seed = new int[1391]; for (int i = 0; i < seed.length; ++i) { seed[i] = base[i % base.length] + i; } final Well44497a rng = new Well44497a(seed); final int[] expectedSequence = { 0xa8ae884a, 0xa5241b7f, 0x3ad211ce, 0xf526210c, 0xcf2feb2e, 0x97ffba0e, 0x52feddda, 0xf51d0147, 0x8734a1d2, 0x4acbbeb2, 0xe788e18f, 0xad07070d, 0xc6425a56, 0x588f6997, 0xd490294e, 0xf9488e69, 0x10661884, 0xbe534578, 0x471d345f, 0x1a6f733f, 0xe18cc123, 0x20c659a8, 0x75afd83c, 0xb76d1580, 0xebfb9078, 0x85df2067, 0x3f98521b, 0xdb8b8dcd, 0x75f40193, 0x29048e7e, 0xe461eb58, 0x0365b046, 0x29ae4ce9, 0x2424d096, 0x06ffaab1, 0x7426e78f, 0xe30e5c49, 0x16ff7a29, 0x66d5655a, 0xc930b87f, 0x0435efd4, 0x7ee00964, 0x668820e3, 0x2c07c519, 0x45431f6c, 0xdb8e2218, 0x57916201, 0x7a49c076, 0x244da2eb, 0x8a78fb16, 0xc61df7bb, 0x2e44ca30, 0x608c0d17, 0xa3c715d2, 0x598e6afa, 0x366dd741, 0x68fad791, 0x04dfe8d4, 0x03b5e214, 0x5a8b4ef4, 0x49d9bf18, 0x1d2f364c, 0xd04a5ac2, 0x82657d22, 0x1d85fd6f, 0x100f6eb4, 0x87ff6147, 0x400d51a9, 0x620e9ee4, 0x22d4c70a, 0xe7d5a2b7, 0x4830703c, 0x85352976, 0xffe0c0b0, 0xe5896bd0, 0x102a7741, 0xc443db14, 0x5a8eeba2, 0xd691ab70, 0xfb49015f, 0xa54d6898, 0x0b783095, 0x451cbca2, 0xaf440bf7, 0xe38d46e9, 0x22c7d7b4, 0xd88ea06e, 0xb56c933b, 0xe9fa124f, 0x87deae52, 0x82b5204e, 0xd98a09e9, 0xc8d97ff7, 0x9c2fff81, 0x514a9e01, 0xce6cc44c, 0x9c853f1b, 0x910fa157, 0x04fe2053, 0xc10d9321, 0xe10fdc4e, 0xa3d404ab, 0x64d4e81e, 0xcb3130ef, 0xb9189319, 0x7ce1869b, 0x6489a897, 0x912ab85f, 0xf06864ae, 0x66a35cb8, 0xe2ba1984, 0xc764bdaf, 0x264c2a9c, 0x887972ff, 0xc41c7d2f, 0x2c4543d8, 0x628d8d24, 0x2efe649b, 0x5b8fe05c, 0x346c521f, 0xa5f3ad10, 0x06de66f6, 0x01b46956, 0x588aca73, 0x4bd835a6, 0x1f2eb048, 0xd24bd232, 0x4f6c004f, 0x1f847910, 0x120ee210, 0x85feeb8d, 0x420cdd07, 0x600f14e8, 0x20d54003, 0xe5d42332, 0x4a31f03b, 0x38185c23, 0xfde144a6, 0x68b01814, 0x122bf3e1, 0x7168a77c, 0xe7a391ba, 0x63bad890, 0x4664710f, 0xa74ce8e8, 0x0979bd77, 0x0935b5ca, 0xad45830b, 0x56a633e1, 0xaffeaffa, 0xda8f22df, 0x0047e018, 0x24f365e5, 0x2affdd04, 0x80b4a2d2, 0x64a77769, 0xcad8fb2c, 0x9e2e7394, 0x534b1ff6, 0xcc6d4325, 0x9e84bc3d, 0x930e2b3e, 0xb1d55eaf, 0xc30c1637, 0x5424a696, 0xa1d58d75, 0xd9f9958e, 0x7e1a4ea1, 0xbb191149, 0xc1ccf4cf, 0xe9b0d595, 0x932b3948, 0xf269e6dd, 0x64a2d999, 0xe0bb937b, 0xc565373a, 0x8b6d51ba, 0x2558063d, 0x88357b5e, 0x9168063d, 0xd7a6f8df, 0x2cffe18e, 0xeea49aa9, 0x814728d7, 0xa7f2202b, 0xabff189a, 0xb49f1dbe, 0xe5a7b4f9, 0xfef34366, 0xa203ca0a, 0x7f6aa08e, 0x4d6d838d, 0xd28d08c4, 0xbf2f9a83, 0x30d59494, 0xff21a95a, 0xd52463a7, 0x9df83f32, 0x58f95b89, 0xff1a897f, 0x3a19db1f, 0x40cc3f2b, 0x6ab19c9f, 0xa7008e8a, 0x73692f8b, 0xe5a21873, 0x61bb5504, 0x4465f5d4, 0x0a6d9a94, 0xa458c70d, 0x0b343bde, 0x1068caab, 0x54a7b59d, 0xadff25ca, 0x6fa45ed1, 0x02465711, 0x26f2e2aa, 0x28fe5b21, 0x359fd47f, 0x66a6fdc8, 0x7ff38f2d, 0x2303076d, 0xfe6a6170, 0x794639c7, 0x538dcc42, 0x3e2f5b79, 0xb3d4d08a, 0x7e21615c, 0x562522bd, 0x08f8f8b5, 0x1bf81147, 0xc81bc3e9, 0x0a3264db, 0xf3cd7889, 0x4bb15859, 0xd20045d0, 0xff449d73, 0xb989aff5, 0x6596edfc, 0xf04e4dbb, 0xcd6cdc55, 0x93598784, 0xda34f1af, 0x43698540, 0xa5a77c5a, 0xc9d493d8, 0x58a516fd, 0x3b469b17, 0x32d9540c, 0x65fe9e05, 0x5a9e93f1, 0x3fa63cf2, 0x64f2c682, 0xdc0243db, 0x116b2dc6, 0xd046f7e7, 0xa88c88e0, 0x0d2e198f, 0x9ad411f8, 0x4d202878, 0x6725e697, 0x3ff9b649, 0x1ef8d9fc, 0x291b02c0, 0x4332ab6a, 0xa6cdb170, 0xb79ae42e, 0x31010c05, 0xa2445835, 0xe08965f1, 0xc496269a, 0x854e8069, 0xbc6c1479, 0x22594262, 0x5e1f4b59, 0xee694b25, 0x598ac7f8, 0xd4d455ec, 0xc9a5d13d, 0x436b2871, 0x13d995b8, 0x49d529ab, 0x3f9e511f, 0xd38d8e54, 0xc5f20ef4, 0x5d028718, 0xd86be75f, 0xdb47b956, 0x418c4e01, 0x282edd4c, 0x16ddaa61, 0x5420e9a6, 0x6f0859d7, 0x8ef9724d, 0x4ed36572, 0xd522b8e2, 0xa433e8c7, 0x3ae60c16, 0x2e9a2f41, 0x7801c1bb, 0x85451081, 0xf7882289, 0x039762a0, 0x7a4fcdf5, 0xd847ac37, 0x1672f98a, 0x631f8a99, 0x9a42faaf, 0x908a0409, 0x3bd5127d, 0x35886510, 0x2e6becca, 0xd8d8df83, 0x98d5eb7d, 0x9797e265, 0x7a8d4a20, 0x0dd3b2a4, 0x6d293d54, 0xe440588e, 0xa64774d9, 0x3da7ff07, 0xf8056595, 0x6fdd632d, 0x700b5d06, 0xfa0896ad, 0xc6d2c81b, 0xe3d3a6ec, 0x0c227260, 0xfd3323c3, 0xa7e6ca86, 0xc59b6f27, 0x30387d6e, 0x4c45d20b, 0x7a88ebe0, 0x5297aee3, 0x974f05e6, 0x95476d79, 0x83723dcf, 0xa01ec9bd, 0x1b4231eb, 0x4b8b4741, 0xfed5d0a8, 0xd088a0ef, 0xc16aac08, 0x01d81ede, 0xdfd4ad09, 0xbe9721a9, 0xd18c0232, 0xb0d3700e, 0xd429f592, 0xc94099d4, 0x5a6ac0f1, 0x48a73b45, 0xf105a843, 0x6cdc24ad, 0x150b9be4, 0x3509de74, 0x17d20178, 0xa8d2e5d7, 0xdb2331de, 0x553a995a, 0x7ce78682, 0x589ba1ab, 0xc138b266, 0x046e6f52, 0xeaa95f91, 0x6abc12df, 0xf746babd, 0x5e462da5, 0xd0737d43, 0xc91e075f, 0x24437131, 0x268b8b3f, 0x6ef86f2e, 0xc389ec69, 0x1c6a674a, 0xa1f5a4bc, 0x16d467bb, 0x4996645b, 0x148cc6eb, 0xe3d234ed, 0x0b28b841, 0x4a41d702, 0x736a0a8f, 0x03a67e91, 0x8a04e7b7, 0x35dced85, 0x120ad3f1, 0x9c091621, 0xb8d34628, 0x31d22cbb, 0x2a23f2a2, 0x043a5406, 0x51e74eb8, 0xa8b61e4d, 0xf239f982, 0x516ea2b8, 0x17a9983f, 0x9fbcd13d, 0xc6467e9b, 0x1b46eb3f, 0x0173b989, 0xbd33b382, 0x7d43ba50, 0x62a034d8, 0xb7f8a33a, 0x22892957, 0x4041d682, 0x0cf564a2, 0xa2f9ddef, 0x3096a2b4, 0x48ad7202, 0x1ad2f2c7, 0x2e2874a5, 0x034116de, 0xd86b4a73, 0x1aa6b2f3, 0x030427c5, 0x19f757ed, 0xb70a1d79, 0xb022a03b, 0x69d38960, 0xaddb91a1, 0xae08441c, 0xb33b1ea6, 0x01c6f5fd, 0xb1b6d17e, 0x93393689, 0x1a6fe4ec, 0x00a8d399, 0xa8bd9931, 0x7547378d, 0x076b552b, 0x495e0450, 0x98337f3d, 0x217a00e6, 0x37a0f47f, 0xc8f9e96e, 0x0ea29d97, 0x114116f2, 0x0bf42f58, 0xeff91c13, 0xa0bd13c0, 0x49adbe86, 0x12f9442b, 0x2e21c306, 0x0b6ca998, 0x096b8f39, 0xd28b0cd6, 0xe7299a48, 0xd4f79eac, 0x6333ac21, 0xdd2262ee, 0x59f23650, 0x84db5305, 0x67088ae4, 0xbe3bd233, 0xb4c633a2, 0x86b793cd, 0xe712866a, 0xdf6f213a, 0x49a812a5, 0x61bd5a21, 0x7c47f17f, 0xd66b9edf, 0x885ec850, 0xf7323f71, 0x0c7ac236, 0x90a1b3b3, 0x49f9269c, 0x5ba25365, 0xc2405803, 0x12f4e899, 0x08f853c2, 0x6dbdde7d, 0xe2acfc1b, 0xb3f98eef, 0x97210592, 0xd66c6958, 0xa1403f70, 0xd78bce3b, 0xf4bb1107, 0x5bc27d25, 0xacc95c19, 0x102ef254, 0x0ac1f78f, 0x1cd4e509, 0x29f41282, 0x7e7ce7b0, 0xdc3b3eeb, 0x1f1d3ab4, 0x098ff47d, 0xd3a0eaaa, 0x5bdd33eb, 0xb7d1001a, 0xba672ab4, 0x5c79923a, 0xcf59a9f8, 0xaacf3e8b, 0x50851f39, 0x734856a1, 0xc6ae7cf3, 0xf332d5c2, 0x81252c6d, 0xa2f15f58, 0x590b8436, 0x520e551f, 0x05a91142, 0x0d391a86, 0xf634324d, 0xfcf112cc, 0x1da1e251, 0x498849be, 0x0fdec559, 0xd1e5b9c5, 0x38dc1111, 0x191521be, 0xad1c5109, 0x6d5698e6, 0x0047c8bf, 0xcad8f3f0, 0xd3a5deb0, 0xbf7671de, 0x55551958, 0x8e085910, 0xf679ce6b, 0xd5fb60a0, 0x749bcf0b, 0x2648698e, 0xe9aee016, 0x5d243c3a, 0xefbae64c, 0x3fddc441, 0x27c72b48, 0xa3d3bade, 0x1459317a, 0x511a11d7, 0x1683304c, 0xe5f39a54, 0x9c8b92a4, 0x4a116a76, 0xd2e7df8e, 0x26807ad5, 0x721ed8eb, 0x7f400e7d, 0xf13bd4f6, 0x08903141, 0xfef2653a, 0x6b891f33, 0x18b0e984, 0x3d656029, 0xa0eaf31d, 0x34e6ab8a, 0x20f7d85b, 0x45f73a17, 0xede588bf, 0xef4928a1, 0xbbd085f6, 0x8ed6e84d, 0x022c686a, 0x46e7705c, 0xa39d5f22, 0x412b1100, 0xe50a6435, 0xd46d7967, 0x423b2afb, 0x64047615, 0xce24a91c, 0xcdc18198, 0x0caeaca1, 0xcd98d5e1, 0x39984c71, 0xcef00848, 0x29bf6acb, 0x24e65645, 0xedd5596b, 0x93be7934, 0x95540c13, 0xf60d157a, 0x292aae6d, 0xd8172ae6, 0x417badbc, 0xc525c308, 0x1afddd87, 0xbf0a94f6, 0x48c96e37, 0x488fac6d, 0x1e88cc2c, 0xbe063e22, 0x63440e2a, 0xa8d29366, 0xf3a1d2a5, 0xd0f4ac3e, 0xd4e0ba73, 0x1e855845, 0x562f798a, 0x64e11921, 0xc494eaeb, 0x5621a9ca, 0x3b314eda, 0x9caffaee, 0xca82587f, 0x3c222142, 0x49ac5116, 0x595c1641, 0x659f6dee, 0x93c51a0f, 0x28e9a1f4, 0xe3a969dc, 0x5eb4932a, 0x34bde2c4, 0xe3df2a3a, 0xe2f29cc9, 0x4eea09a5, 0xb50a5c72, 0xe40d02fe, 0x90bd1ce0, 0xab7ca062, 0x65c8a3e3, 0x3b6f27f1, 0xaf02720f, 0x7300a67c, 0xdbe6ee06, 0xa16126ea, 0x1bf6a7b7, 0x42d8f997, 0x2c9cf66c, 0x47a4d156, 0x8e5b3b99, 0x048dbf89, 0x0f90354c, 0xd08d9cbb, 0x8ae89e1b, 0x67b4bd96, 0xd2e36042, 0x5e41c86d, 0x587e5ae3, 0x1cdb5613, 0xd5eb8a6e, 0xaf0f9b3e, 0x20b1c1e7, 0x2c39e9cc, 0x3cac683e, 0x841c8da0, 0x7d0af9ef, 0x21edd1e9, 0xfa85a975, 0x6f1b32ca, 0xcbc7524e, 0x14112e63, 0x8bbef2ce, 0xcc0181c0, 0xd88751fd, 0x106f3807, 0x29ef8e64, 0x4f8704c6, 0xf3084f62, 0x3e3b8f81, 0x2c50f298, 0xa57cd65a, 0x013a8c24, 0xd2305e5b, 0x3a348c72, 0xfe808c16, 0xc55edb0c, 0x04466663, 0xf8e498d7, 0x15fc1d57, 0xd090ee0c, 0x297d3eec, 0x523d8c36, 0x0e031767, 0x27b6a18a, 0xb1b2b0fe, 0xd99f50c8, 0x1fdc2b4b, 0xf112fad7, 0xb622257c, 0x097e2f50, 0xbd45bb62, 0x45a0bb53, 0xae8c935e, 0xb0638c4b, 0x0ec995ec, 0x52dc0b63, 0x7a48f924, 0xc2fde5b3, 0xa7cc4f23, 0x958f11fa, 0x5ff36ee6, 0xc9374765, 0x6584c544, 0xcd2f1110, 0x5dccd99e, 0xe996845b, 0x122efade, 0xb6949925, 0xa46504da, 0xf88ac8bf, 0xca8d7d78, 0x32d26e2f, 0xe4b11e69, 0x2c923e48, 0x45eb2d55, 0xa277e53c, 0x12a076ed, 0x9bd52416, 0x06fa983f, 0x2f5c720d, 0xb6a05efc, 0xc17a3591, 0x7e5208cf, 0x618e8114, 0xe0dd237c, 0x61007c22, 0x207d38de, 0x9bab6a98, 0x589a232f, 0x95200120, 0x71bf656d, 0xb6ebed39, 0x5366d788, 0xdcf0df5e, 0xb3258773, 0x1fde1119, 0x7ebbecc3, 0xf1cade95, 0xb5d1527e, 0xa24fbe33, 0x85a6627d, 0x59185873, 0xe07d28df, 0xc589c685, 0xf6fb9b61, 0xa8781bdc, 0x6fd6d3f5, 0xee051de2, 0x577f4d00, 0xd3ca3cf0, 0xdb833383, 0x05818f4e, 0x3ae26361, 0x3dd9e2ea, 0x2f5005b1, 0xad49d7db, 0x14d4cbdb, 0x21a9eb84, 0xbac8d9ee, 0x24efeb0d, 0x7afa7069, 0x0b60b8aa, 0x04e0dee7, 0xa7a35cee, 0x10e1d009, 0x06d0ed24, 0x8ffa873d, 0xb3815054, 0xadcf9ab6, 0xd47aebcd, 0x5d9f906e, 0xef3af4f7, 0x7f0fd1dc, 0x2f6cb93e, 0xe25acd48, 0x1a156fb1, 0x72f4610e, 0x556ba647, 0x94a276a9, 0x85425b3c, 0xf2c988f2, 0x09c0e9c1, 0xe77bc354, 0x0e908925, 0x2ee4e69a, 0x1d282fc4, 0x1bb1a5d2, 0x4b09858c, 0xf2594e12, 0xc4b674f9, 0x7322c071, 0x6a5872bd, 0xfa363075, 0x670db884, 0xb0c707f7, 0xa043ffb6, 0x4ff10759, 0x641d27c9, 0xd085aefa, 0xc322db13, 0x551ae33c, 0xf32f916b, 0x0ef2c668, 0x3d612668, 0x0567632d, 0x23e9dee2, 0x7b43ccaf, 0x587104a3, 0x002395c1, 0xe4bb9b77, 0x281400fe, 0x5bf4ca29, 0x6a2bc460, 0x8abf6ef7, 0x85ad7cab, 0xb59a6361, 0x9c11d077, 0x116de09c, 0x0c7effa5, 0x6859663c, 0x740af36a, 0xce5bc8fe, 0x69bfd15f, 0xe52add86, 0xe641b3ad, 0x248eaf44, 0x75632e93, 0x4a924265, 0xb144891f, 0xfcfb008c, 0x10485f93, 0xee56f65c, 0x47497d05, 0x4f4358da, 0x2d43d0ea, 0x3bce9c36, 0xefa2954d, 0xdf48a8e1, 0x857d1e64, 0xd24d6990, 0xe4573866, 0x9292460f, 0xf5e70f0b, 0x30ebac60, 0x6a037c37, 0xa1b3779c, 0x98fad5a5, 0x844c11c6, 0xa03d76c8, 0x9a8be78d, 0xff100ff0, 0x02b7f4fb, 0x8aba2ce6, 0xd7f9e8e1, 0x9242e3e7, 0xc98f2448, 0x33d643b4, 0xd06de850, 0xa2c5c661, 0xc93ab33b, 0xcfb75c3e, 0x198f6c38, 0xabbfe29b, 0xcdade806, 0x501a8128, 0xd6ea5702, 0x6cf12fa6, 0xd055c019, 0x651e0329, 0xd4b7b168, 0xadad356a, 0x624d1eb0, 0x6ac21db1, 0x905e895e, 0xe30d08b9, 0x5b7d770d, 0x9fec542d, 0xdd52cd61, 0x0e77eddd, 0xe9351ed6, 0x27d74538, 0x1724f508, 0x2ca211c6, 0x2a8bc91d, 0x95adc424, 0x9351198a, 0xc3da6170, 0x7b780411, 0x44cfe602, 0x4e121fea, 0x989e02e0, 0x483f1bb8, 0x3ae814d2, 0xa921696d, 0x654fd279, 0x946e039d, 0x8d6406d2, 0x3b9bf306, 0xf27f07b3, 0x07d5639d, 0xc020f194, 0x1551c4f9, 0x5dd43c63, 0x3b67b3b6, 0x398f7aed, 0xfd28e448, 0x0e8a1b4a, 0x3037aced, 0xd91e54cf, 0x207b8a8d, 0xb880ed37, 0x29e3ce73, 0x3b7779f4, 0x5f01668c, 0x7e53092f, 0x5682a00e, 0xfd7f3d2f, 0x79399211, 0xc293e126, 0x6924a8bf, 0x1b4472e3, 0x88b84303, 0xe9bf022d, 0x8bc04e7c, 0xb3029d94, 0x265266c3, 0x932da2ea, 0xa34617c1, 0x6335d441, 0xfe86c45e, 0x7138ac8c, 0x04ae1c16, 0x6318969d, 0x67fe2e8c, 0x80461d7a, 0xa0c2b07c, 0x07090143, 0x5f2be9d4, 0x1819318b, 0xaf7b337c, 0x26aa684f, 0x46a8a99f, 0x6b2d68b1, 0x30d8d41d, 0xa89305fe, 0xd33869d9, 0xcb8f0d2e, 0x8ce6823d, 0x575b5469, 0xac69ece8, 0x2dce3ee7, 0xac43a267, 0x8dd0f670, 0x91fa69f9, 0x12aa7dc7, 0x49e2c52e, 0x768a2d47, 0xdee90b6e, 0x8d38abb8, 0xf520cee9, 0xad01cd68, 0x03ddb46b, 0xe17d6825, 0x700dfb2b, 0x36c560bc, 0xcde2d010, 0x1b65184d, 0xa542beaa, 0x936fb278, 0x3a400751, 0xfc6310ef, 0x91ed2b38, 0x4809aa29, 0xe1483d3d, 0x4335edfe, 0xe8744921, 0x1d5155a4, 0x7a6a6924, 0x21dfe6d6, 0x5f30b46e, 0x50cd038e, 0x84a6331c, 0x71344b4b, 0x32597fe7, 0xdabd8b7f, 0x44501723, 0xc223965d, 0x47c27ea1, 0x6fccb2ad, 0x3b40e379, 0x57bb0174, 0x78ce89a2, 0x004b6686, 0xc074ff7f, 0xc55d56c2, 0x7970b5c1, 0xce23b368, 0xda3f40ad, 0x48d50005, 0x7f6091c8, 0xe9f3e608, 0x16340b6c, 0xf9638cc7, 0x04cc9a50, 0xd8c6d90e, 0x2b62c5b4, 0xaf50ecc6, 0xe3a91016, 0xeea2c371, 0x3f24d19e, 0x23462e3f, 0x3cb0ff1a, 0xeb3e0a0f, 0x6f17bfe2, 0x0eaa285c, 0xc2d67480, 0xaf273c38, 0xb700b2ca, 0x0fa48fe5, 0x4af81a71, 0x985eff8c, 0x0498eea6, 0xb14e698f, 0xffdc40b9, 0x93afeafa, 0x8fb47bf9, 0x517e2631, 0x90eb5bbe, 0xbc7dccd7, 0x08e1effb, 0xb6ca0248, 0xdd8bf4c1, 0x154613f3, 0x7dbf57ac, 0xcb877aeb, 0xcc4b7d82, 0x82a1f27c, 0xe432cb3d, 0x1a607024, 0x158c332d, 0xb9e1cfa8, 0xc595b709, 0x72194d6a, 0x711c6119, 0x524d70a3, 0xf48885a2, 0x51d3ed03, 0x89a1665e, 0x40b224f0, 0x5d44daac, 0x68d9de4a, 0x670de923, 0x24e06832, 0xecc5bde3, 0x36379112, 0xc51eef5c, 0x06dfac0c, 0xd447393e, 0xe56a3644, 0x487bc4b0, 0x044af5da, 0xc54d5ce4, 0x704bebd8, 0x17225487, 0xe8f3f3c3, 0x82c1768b, 0x3cc297c9, 0xd1c0a480, 0x3a145cca, 0x9103e219, 0x5685932e, 0x58b274f1, 0x9b2705ca, 0x0941fbcd, 0x6997da01, 0x2cab7638, 0xef0caf78, 0x4e417b2e, 0xc7045996, 0x498ff349, 0xedf1e56e, 0xe4d63c48, 0xed0eb104, 0xd9ff9b0a, 0xc15bc1ef, 0xf940e692, 0xbe486622, 0xeddfd360, 0xfb0ed29c, 0x30b8a2e6, 0xf0ccb7de, 0xfea83275, 0xef52620e, 0xd288421f, 0x582b4030, 0xc3dcfbe9, 0x98649927, 0x2f203cf2, 0xb7681f4e, 0xbd6114e8, 0x20853550, 0x77bf82d5, 0x0f4e45a6, 0xe2b16fc8, 0x531148bb, 0x386840d5, 0x92875447, 0x84f08144, 0x32ebb7f1, 0xc5a57165, 0x1607d665, 0xeb23ef5d, 0x84472ea7, 0xa371e168, 0x8e150474, 0x72330ebd, 0x0401ed33, 0xf05b1f1d, 0xd67a9c88, 0x01e3960b, 0xb641c4da, 0x963a0f4a, 0x96bf7161, 0x36d23641, 0x9e4f5f08, 0xe54f040d, 0x10ea0e79, 0x8c33b68b, 0xdc025a36, 0xb20579a4, 0xac761e10, 0x80161ada, 0xc91e4ec8, 0x4bf1e6e3, 0xd0499356, 0xc1be9f13, 0x39c95fb6, 0x0d8d4944, 0xdc950250, 0xded34089, 0x735428c3, 0xc599a441, 0x79724d47, 0xc76f240a, 0x6de0ea66, 0x78d66aea, 0x271f22c2, 0xa23e96bf, 0x373b8263, 0xb3921694, 0x3317c684, 0xd43080ab, 0x1f8ab5d9, 0xd202d203, 0xb2014636, 0x1f029c45, 0x60718e3c, 0x48a01894, 0x256a5a37, 0xfdba5c3b, 0x7fa84f1e, 0x67c6e088, 0xaa5729be, 0x50b784fe, 0x3eabc2e2, 0x6d93ff6c, 0xe0f23b24, 0x00b4cc14, 0x4d184b90, 0xb2650e21, 0xab3b8b07, 0x8b8063b6, 0x7a5e04ff, 0xacb7f5ea, 0x425cd211, 0xbddd944d, 0x852731f0, 0x3ec085a4, 0x30043902, 0x15a6b247, 0x940a7051, 0xa5d2decf, 0xba82b019, 0x29ce9075, 0xc676e501, 0x2226f896, 0x91e21758, 0x7dc0913c, 0x83e255c5, 0x07a9a95e, 0x25f4ed18, 0x129adb8c, 0x059b7d88, 0x1d77ad9b, 0xa3adbde4, 0x7c547198, 0xc0a2d5cb, 0x30bec354, 0x9724800a, 0x88e62084, 0x21a7a7f5, 0x52508084, 0x42470680, 0xb9018291, 0x52f59407, 0x80cc2f08, 0x1620835a, 0x8ce9ec7c, 0xbccea26e, 0x015c4ce5, 0xbdfe5e13, 0x933be568, 0x78861e4b, 0x30982fe1, 0xb65224b5, 0x021130af, 0xbdbb45de, 0x9efc7d0a, 0xb1ca8bd8, 0xd8be76b7, 0xf9727e27, 0xd3579a0c, 0x0530fe42, 0x55d3bfca, 0x896edc93, 0x4a975959, 0xcb993653, 0x0194d907, 0x8c46df02, 0xa09c5d09, 0x4765bede, 0xeaa12521, 0x2e7d3d35, 0xfbf4273f, 0x46dd082e, 0xfb2a03ea, 0x51c129e0, 0xcdc5df27, 0x5d81e110, 0xf81c674d, 0x62f5a2a5, 0x74a49b11, 0x4755013d, 0x125ac0a7, 0x01a840dc, 0xa4b860e8, 0x17abafb9, 0xd5ad5784, 0xca87a224, 0x38187b39, 0x6d75337b, 0xe3dde7ab, 0x4877ba59, 0x30b52c46, 0xba6db09f, 0xb4f03274, 0xe4caf103, 0xbfc8ae95, 0x452fe298, 0xf8fdb65e, 0x65a06cac, 0x8eb558c2, 0xa75684e1, 0xbfe5980b, 0x28201adb, 0xf10bb48a, 0x677839fd, 0x90b6e316, 0xecc28ce5, 0xfe43ac8a, 0x1db4062f, 0x38e6eeab, 0x8d61b89c, 0xcb85bd41, 0x6a23832c, 0x69181af8, 0x9c4365a8, 0x9a5c5dc3, 0x6cf0274e, 0x77246a1c, 0x2ddcfb4a, 0x9f10eb22, 0x3838bea1, 0x8b47f98f, 0x82f332e1, 0xee9e2db4, 0x2161a678, 0x52b39e4c, 0x7ac6fd53, 0xc93488dd, 0xc4f962d5, 0xa7b6e029, 0x5e0316b9, 0x44f0858e, 0x6a1dd16f, 0x7e61395f, 0x290ac8bd, 0xf2c16a25, 0x15473849, 0x1e3c2d92, 0xfeb0187c, 0xc62eafea, 0x3fd92f7e, 0xb1cbabac, 0x1c2544e7, 0xed0c7427, 0xc524202f, 0xbe7915b8, 0xf7f73a0b, 0x2713050f, 0x5263d1a5, 0x3d0a1eec, 0x2f928776, 0xf60dde52, 0xa72464d4, 0xa176ce19, 0x10025ffb, 0x51fe0c1e, 0x9c8af51a, 0xed7c121c, 0xef188d94, 0xcf2797c0, 0x0aa1809b, 0xda0e093e, 0xf2a4dc8c, 0x8b82e782, 0xb69aed05, 0x432b143d, 0x5e70775f, 0x2689ca25, 0x743ac1ec, 0x9fb28344, 0xb9a82828, 0x3d2290a7, 0x7004a58e, 0x6eff1e06, 0x9dd60e19, 0xdf5c1979, 0xea48db1c, 0x236100f5, 0x3f88a96e, 0x8ad662fd, 0x37a9229e, 0x739e1097, 0x5ceb7555, 0xb1689b3b, 0x2f2f5ff6, 0x0a03209e, 0x68447867, 0xf36bb7df, 0x145d1f75, 0x5a4b620a, 0x832eeda7, 0x6b46cb82, 0xd739a473, 0x23e326af, 0x8c38afe6, 0x7aeda29e, 0x8a5a2e67, 0x08e55d38, 0x8263f8eb, 0xaadc66a5, 0x23005bf5, 0xc5f94054, 0x4b39c9b7, 0xf93e9015, 0x3e5b22c0, 0x5c36fde0, 0xc9c4ecf7, 0xbaef5e1c, 0x75d10b67, 0x940af4c2, 0x3e6ad7ae, 0x3e5a96df, 0x41a9ac5f, 0x6d985748, 0x6d5b73a3, 0x69e91de3, 0x5e6b94c3, 0xd20d2466, 0xc433135d, 0xe220df92, 0xdf8c28e4, 0x33ade44e, 0xfbdaedb6, 0x6aa92017, 0x2c6ccba6, 0x15293bab, 0xd046c380, 0x4ce6261f, 0x97acda3b, 0x24c559bb, 0x6ec4eff1, 0xe3e70b84, 0xf74dc43c, 0x80e6a406, 0xd403da98, 0x5fc5bcd6, 0x554dc3f5, 0x50abf290, 0x094046c2, 0xa3224839, 0x3972e544, 0xc72ed9a2, 0x3618cd8a, 0x7230c35e, 0x2a5d2646, 0xfcfa7998, 0xc363d099, 0xed967e6e, 0x9ef413d9, 0x5a4b3963, 0x75f721d4, 0xb980c699, 0x87f96fdf, 0x28c28acc, 0x2d010871, 0xc9f3d703, 0x3ff88a93, 0x265cc19c, 0xe7165060, 0xc697166b, 0xaf0c8b2b, 0xe55d263f, 0x32992462, 0x022c84a3, 0xd0577ceb, 0x2b389b0c, 0xe487f030, 0x50d3971a, 0x4f6c04b2, 0xa5c90efe, 0x38264797, 0x0220c47e, 0x8b2a0174, 0x035e080c, 0x64297359, 0x758386ed, 0xefbeefbc, 0x6ea2709f, 0xaa51953e, 0xb26548dd, 0xf6f09f33, 0x62626d3d, 0xee1f8d07, 0x0104464f, 0x0f5dfa3e, 0x0f70ffc9, 0xf538529a, 0x217475d0, 0xeb1a3596, 0xe4efe3ef, 0x7f92f15c, 0x521882e1, 0x49face82, 0x3094bd30, 0x96f4514b, 0x5af07be8, 0xf86dd3c5, 0x9d9834d5, 0x64179b42, 0x1af06375, 0x3002a633, 0xe3afc154, 0x58379d4b, 0x266f1b6f, 0x916cb00b, 0xe634b195, 0x81ba7683, 0x9ed46d11, 0xc9592e65, 0xdff76d1e, 0x943af59a, 0xa26f07e3, 0x75fd10a7, 0xa353765b, 0x3182e74b, 0x6cff0335, 0x3e7860f1, 0x2675f731, 0x56db561d, 0xd16802d5, 0xfbf4fde3, 0x8182d51f, 0x0b8e002c, 0xf27f2628, 0x5662636e, 0x2e987bc8, 0xc2b45a65, 0x3a4ce87b, 0x044ee889, 0xb170f9f1, 0xe6b8aeec, 0xefbf3f47, 0xd1f51035, 0xab5bf009, 0xacdd3333, 0xf293bfdf, 0x5771c6c5, 0x9c7bf757, 0x9edef19e, 0x9d8c3a54, 0x83469c0d, 0xa1b06bd5, 0x885ea3ee, 0x12f83cab, 0xc958781c, 0x53a5c68c, 0x95499661, 0x320500fa, 0xdbaced82, 0x3bbfeecd, 0xe1917797, 0x6e3abec3, 0x82b1bf84, 0xadb6d27c, 0x78ac4c90, 0x66e14b28, 0x233326e0, 0xa30da16a, 0x4abd2f3c, 0xf8260a05, 0x7baae9ec, 0xf6800e6c, 0xe746f3a3, 0x40137893, 0x35136e6e, 0x61552456, 0x95a1a72f, 0x82f5fb4b, 0x903b52c0, 0x1f924b15, 0x88c18881, 0x562364d2, 0xe3a4a07b, 0x2edf98e9, 0x70ea54bd, 0x25ce5fe2, 0xa5f665cf, 0x41918902, 0x610fb925, 0x403642b1, 0x79b70112, 0x6470165c, 0x7ba7b8bc, 0x79b3fbf4, 0xd5c34822, 0x59e07c3f, 0x45e5397f, 0xf682000b, 0xfca29583, 0x98f021a4, 0x79f1ad95, 0xe7dbb076, 0xb40707cf, 0x78e602ae, 0x17574b2f, 0x10018d88, 0xd3ea809e, 0xae88f4fb, 0x5c22f645, 0xeed5917e, 0xc8ca5f1e, 0x0c598032, 0xd20a533b, 0xc989aae6, 0x8a004c70, 0x3b900cd7, 0xaa6c9bb8, 0x68f22e55, 0xd38c0621, 0xcdf66aee, 0x8f12c269, 0xc01005a9, 0xdaf0e9d3, 0x27973358, 0xe10f522e, 0xd07f4615, 0xe1655d86, 0x1e241b04, 0x0dde2614, 0xb4e218d2, 0x68922dbc, 0xb2605162, 0x63c2c60f, 0xe3909f1e, 0x020fb0c9, 0x505de07d, 0xf06ef431, 0x9e71c430, 0x856e882d, 0x6446c898, 0x339f1762, 0xbe64d708, 0x45e1d24d, 0x8cd881ed, 0xd783de5c, 0xdbda853f, 0x13658127, 0x6851e80a, 0xc1f8eb30, 0x4841916d, 0x9b4c6301, 0x8e8e0982, 0xb4ec1723, 0x223e236c, 0x27dcccfe, 0xc6bfe849, 0x85750e90, 0x37814a21, 0x39b583dd, 0x6a262a49, 0x9ac89453, 0xba1c7ea7, 0x37192497, 0x1d109f87, 0x0114aa48, 0x9ff6d8bc, 0x674de54e, 0x78c8e9db, 0x7f80219a, 0xe82a7675, 0x486a1fcc, 0x6beaaa5a, 0x7e7d4424, 0xee59e390, 0xe90c90c8, 0x02c25538, 0xdf58bbd5, 0x23d02560, 0xecb80473, 0xcaa853c4, 0xab85b0b7, 0x3be6a274, 0xc9e3c889, 0x95abee53, 0xb3dca1da, 0x092275d3, 0x1b86ad2e, 0xa73d6f72, 0xe02f5e94, 0x49a7d365, 0xaa017fc5, 0x84f6b836, 0xf2745d34, 0xd6bec3e7, 0x656441bb, 0xc5a1c8a7, 0x4e418115, 0xf7ac4a82, 0x630ffdf4, 0x2c1c632c, 0x52912ba6, 0x5701c33e, 0x4226c5b7, 0x485cce2a, 0xc3b6cacf, 0x726597b0, 0x90f2b4b4, 0x63a3220f, 0x2c2d94aa, 0x67376f4b, 0x5f317092, 0x12ac9029, 0xaa84344c, 0xc69954a6, 0x759c1800, 0x61c0a734, 0x08c6ebdb, 0x32a5257c, 0xb37e8829, 0xaee7ecc0, 0x433363cb, 0x4574b191, 0x4d4b5344, 0x0b1bdc1a, 0xc633b647, 0x4f539674, 0x51de3002, 0x4af3263d, 0xe78ba0e3, 0xadfc2466, 0x434af73a, 0xdce29725, 0x8a043f09, 0x844c06af, 0xd71f7b5d, 0x38de0dc3, 0xa1127eeb, 0xeb0686a9, 0x16b93639, 0xbfc0b67f, 0x40ba8ec2, 0xb937883b, 0xd88d5d8e, 0x3215ce13, 0x3f274efe, 0x5b78c8a6, 0x5e7b07bc, 0x811e4ce2, 0x195c182e, 0xbf8f40fe, 0x4b46284b, 0xbcb8a3d4, 0xdaf42b76, 0xafa34368, 0xee54d024, 0x8b8449b9, 0xba7c436a, 0x8d5d8d73, 0x8c6debea, 0x4da0669a, 0x0d349421, 0x8f86be6e, 0xa1745821, 0xe79da9a9, 0xc5fee423, 0xfbcff299, 0xdefc6ae5, 0xc4f3ce5f, 0xffb9c159, 0xca90d0b3, 0xbc9c2452, 0x04c3f96d, 0xf7510ca9, 0x4caae118, 0xbc6d9d54, 0x7eda5298, 0xa0902598, 0xd3ba04ea, 0x027bc3fd, 0x46a53cc1, 0x0b8a7f22, 0x44d4bf77, 0x3e7778c1, 0x0f6468ff, 0xfe779657, 0x9682d1cc, 0x0e49f346, 0xb16f73f5, 0x472a4344, 0x906d5e2a, 0xd744ee06, 0xbe7462ed, 0x3e592100, 0x9b1a05cd, 0xc33a5c15, 0xb20e1e6f, 0x97322ed4, 0xd3a543f2, 0xd6bf6615, 0x31d1834b, 0x04c244a2, 0xd7bed090, 0x4f4bba3a, 0x3123d3c4, 0x37c7e26b, 0x534e26d5, 0xc4c6f75b, 0xe1d2cda1, 0x31de8347, 0xfc483589, 0xd6d30b8c, 0x98f17c3d, 0x3fcf10dd, 0xc3416a6d, 0xbeddbe73, 0x93e01443, 0xeab55d59, 0xc3b84125, 0x4add0993, 0x90c488cf, 0x703feb47, 0x036d2ae1, 0x764a9f53, 0xc4d5dfb2, 0x0d736feb, 0x154f8046, 0xfd682036, 0xb8f660d4, 0x09fb8e86, 0xd7d394dc, 0xe132664d, 0x0fd2e79a, 0x8127ea10, 0x83e79663, 0x242b3d64, 0x072663cf, 0x8cf6b7a7, 0x1484e94a, 0xb314e7a0, 0x778ae206, 0x14fb6cca, 0x7de1b498, 0xf6edcdf1, 0xc873afe4, 0x94515b6a, 0x3bd937bd, 0xd8f07e15, 0x575581bd, 0xb6c4a59a, 0x364a5b4f, 0x3451953f, 0x86529bdc, 0x07103d2e, 0x64e3655a, 0x8ed1c32b, 0xd6071f3c, 0xac545e8f, 0x73f309db, 0x7349ba19, 0x91fc9a20, 0x499bc952, 0xa722eb99, 0x069f24e3, 0xbdff8536, 0xf93acb47, 0x08c7ed85, 0xa4d72be2, 0xa7ca5a20, 0x010fd774, 0x1778aace, 0xed3d20e6, 0x5411457a, 0x27a7d958, 0x83497c72, 0x6c55d72a, 0x97dd5136, 0x8b7df133, 0xa16f1032, 0x860be9af, 0xbbcb0116, 0xc1959f79, 0x26842197, 0xb7533b56, 0x6d796542, 0xc557cfaa, 0xb49c7bf2, 0x6b536f7a, 0xc3c897c1, 0x0d2253c8, 0x8878f90b, 0xbc2583d2, 0xb6ee8761, 0x62af092a, 0xe871d8a1, 0x98f42f8b, 0x5d59302e, 0x8b07359b, 0x365f879f, 0xa2eaf2b2, 0xdecdccce, 0xd3b389a6, 0x7b80ad34, 0xa3163bab, 0x6e2a64ee, 0xe464df1c, 0x9e6a52c7, 0xcd242ec0, 0x455e7ceb, 0xc7670eb7, 0x0a34313c, 0x1f319b49, 0x75ccccc3, 0x773d283f, 0x1930ffdf, 0xea5e2e9a, 0x96533652, 0x9cfa6233, 0x33a9d201, 0xd0d65060, 0x593377bd, 0x8e7b2345, 0x8c5a773f, 0xaa6622b9, 0x86f6f3c7, 0xedb97d46, 0xe5c304cf, 0x6fa094fb, 0x2a188053, 0x9697f74b, 0xd65b295d, 0xa19b5c8d, 0xf79ca4ed, 0xf99b83f9, 0xd45337cb, 0xa2b4ca7f, 0x6941cf49, 0x9af67e9e, 0xdad6ad9c, 0x72210114, 0xd4e6c5a1, 0x00e9948a, 0x129c27c7, 0x2f9ac636, 0x49ee4fe7, 0x45371418, 0x226d96e6, 0xc297f0b3, 0xf585c95a, 0x7e0ab8c0, 0x614f5bbc, 0xbe800c20, 0x533c75c6, 0x17da788b, 0x1f75ec7c, 0xc696a43f, 0x0bf4d180, 0x71d28381, 0x46b788c6, 0x6accc6b6, 0x6fc91d04, 0xa7215efc, 0xb09d35ae, 0x68364c9a, 0x17151edb, 0xce019542, 0x4ab47b88, 0x5adc0202, 0x536dfe02, 0x509c46ff, 0x78cee68d, 0x7e6acd4c, 0x3f05b68d, 0x6f243905, 0x39c52e86, 0x90977869, 0x0c97f592, 0xbb7de9ef, 0xa941be47, 0xdc29d4fb, 0x131b6e32, 0x0bf5643d, 0xaebe0e3e, 0x23b59595, 0x89140b51, 0x44cfe231, 0x313534e6, 0x7958fbc5, 0x9f9b5b2e, 0x35c49d6a, 0x6b57f136, 0x26c7e5f8, 0xf3793ba9, 0xf57d7b6b, 0x29c79609, 0xc242439a, 0xa02e1402, 0x1e3c9eed, 0xbe68af0e, 0x11ef978b, 0xab8158a3, 0x460816c9, 0x76d1a9ed, 0x9e1b8523, 0x893fb020, 0x45f4c4bd, 0xb504928a, 0x481778d8, 0x4d7bd14a, 0x941a9890, 0xd69e0a83, 0xfd9e2eff, 0x884c2aa7, 0x947d9e27, 0xd22f6fb3, 0x63afd584, 0x15996632, 0xab00fc01, 0xb17e8185, 0x28d97512, 0x84a086ab, 0xb03e1d01, 0x8709bd74, 0x67653bba, 0xb8601eaa, 0x97526eda, 0xb24aba3d, 0x51186258, 0x879e23a4, 0xf35faecc, 0x89f2b9d2, 0x4754bd39, 0xcc8c3c1b, 0x499c1aa8, 0x7fd251f3, 0xc35283c6, 0x070dbdc9, 0xb66951ed, 0x9f1b8520, 0xa59d3eff, 0x521e88ad, 0x27c8a481, 0x807ecd32, 0x8cb53092, 0x1a980eb0, 0xa232db99, 0x8253b203, 0xf67fc4f4, 0x09a382b1, 0x30214edc, 0x3cbf250a, 0xf2941413, 0x1fea456d, 0xc34fae34, 0x3380c640, 0x5523c74d, 0x68c9b8fe, 0x3e6fb040, 0x94bf7d88, 0x3f6e1ed6, 0xd13cf25e, 0xb392839b, 0xbadd0f7b, 0xd5b487c2, 0x6506ea26, 0x6b9aa968, 0xd1af0abf, 0xbee956a0, 0x637e8050, 0x6a0f2072, 0x0413ac33, 0xd9ea3783, 0xdae783a8, 0xa2e79fb6, 0xbff59440, 0x3dadb638, 0x447ec592, 0x1551841a, 0x5ba5d9e8, 0x65b65fa7, 0x5c611529, 0x5ae2d0c4, 0x0d53c132, 0xc024f673, 0xa57c3133, 0xa5055dca, 0x3369c277, 0x2221eeaf, 0x2e603b5f, 0xf4f4a86c, 0xb0fc71fa, 0xc717aba5, 0xd91bff24, 0xf5b9dc36, 0xc449ff70, 0x0cdce183, 0xbd8d6822, 0x4551bf85, 0xa1e4e6e9, 0x0839c147, 0x8a5fc6d8, 0x1bcf92fd, 0xc754c33f, 0x2d58145b, 0x3d4680b8, 0x91763b8d, 0x3e2b4a33, 0xc61155dc, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new Well44497a(new int[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertIntArrayConstructorWithSingleBitInPoolIsFunctional(Well44497a.class, 44497); } }
3,063
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L128X128MixTest.java
/* * 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.rng.core.source64; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L128X128Mix}. */ class L128X128MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 4; } @Override public int xbgSeedSize() { return 2; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG128(seed[0], seed[1], seed[2], seed[3]), new XBGXoRoShiRo128(seed[4], seed[5], false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L128X128Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L128X128MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0xf7a78c13fc329c64L, 0xef8c948e0494a150L, 0xac4b477c6908b1bdL, 0x3b98735f99c554c8L, 0x702659bd934b4909L, 0xfd71d0bb15bc255dL, }, new long[] { 0xb8a3befcc9d12da1L, 0x0aa25f8df2b6d30fL, 0x377d33c3a36a02ebL, 0xd7c7fe74dbc32741L, 0x758ee8a262f1a31fL, 0x22d8616b5ffba248L, 0xc63691898f00d2b3L, 0xe730156a52a30750L, 0x2bc09f92096f83a5L, 0x788e002353be1ddcL, 0x77b06a59be8cce16L, 0xf999cd4ce13b3604L, 0xec9a14c9327ff9e9L, 0x01b27bc5bad453c5L, 0x168617c059484a23L, 0xa14908b9013b0bf4L, 0xf86d3652e36ae2fbL, 0x6a6dd1eb42453991L, 0xd1c2a605c8657763L, 0x5eb946fd92b2149eL, 0xe2c7bd97792c9a54L, 0x7a1072a987220290L, 0x95509bfdfbe5dfd6L, 0xd773ef0b1a11ea4bL, 0x23b820058b149fbcL, 0xfa0c47fb90be1869L, 0xe0deb44ae92cfa4fL, 0x74c3e743907845c3L, 0x24d2b874702ce165L, 0x07aaeb42bfa6cdabL, 0xd2c4f8831c34953cL, 0x5864cac991d89733L, 0x2d18205aadd00e16L, 0xba6358375a1c78c1L, 0x544e9dc9a710bbf6L, 0x6f43b974aeff36c9L, 0x0bc71cf292068347L, 0x2ae0f87e3276731cL, 0x5c8b0866ded3fb5aL, 0x4ef10c7dcae51e25L, }), Arguments.of( new long[] { 0x4545d46b1b673815L, 0x0806c96b523a302cL, 0x82f2829259399f1eL, 0x1aaed35434ddf7c3L, 0xcf18a9ed54609638L, 0x7340b6851a41457eL, }, new long[] { 0x813e05de773584dbL, 0x75fbaf767fd4e4d7L, 0x1160b629ad35e420L, 0xf8850cae917f8dffL, 0xe0457f00e2ace49dL, 0xec8d69e7e8ee29bfL, 0x51d9fbf6e213ca8aL, 0x899dba9a21163ce8L, 0x2e7b28807b274195L, 0x6aa548f1b93007ceL, 0x1211481ba5634f60L, 0x8d84948c5d7721f9L, 0x3934e7b0be28c84cL, 0x22a8842d6e92540cL, 0x4e5543d059af300bL, 0xa3be91fbe4a31b65L, 0x8555bcb95a292ae8L, 0xd7c0761ca7f6e51fL, 0x9bc2e7f8bc4a5c35L, 0x6ecba9e5da68d11bL, 0xb6dd609ff75da3e6L, 0xb419b5841e8242e3L, 0x10affb57349d69d6L, 0xa2468a067cd111c8L, 0xca06bc18b07a23f6L, 0x6c35acec00214ff7L, 0x948af68673ceb096L, 0x3ffaef071fa7d66dL, 0xd50d3eb49a99557aL, 0x59a5d62382db6922L, 0x5b2711614a95aa11L, 0x57721649868077bcL, 0xc0ab75a00b5c9a54L, 0xd5a855baab2f9e73L, 0xcd0b779a4b00bbfcL, 0x20017121200dc1ddL, 0x5054ac4a75515202L, 0x7eff78db2ba3327dL, 0x3d2ddad31a4c073bL, 0xd766756cde66e520L, }), Arguments.of( new long[] { 0xca3ad0c9fdb80553L, 0x9a1d5d6b9970d990L, 0x325734fae5d66ea8L, 0x20e5991e069f70dfL, 0x97e5e144d0cae5beL, 0xf778cc0ff3bbe5d0L, }, new long[] { 0xf4662b85cbe7d457L, 0xe960eff7cce91220L, 0x501e265cfdd3d9ddL, 0x112ecbd65f94d068L, 0xaef96e7cb7cef806L, 0xfc6d69cb2f17c7ffL, 0xabbe0e5a0a756ee6L, 0x574210c4bfd26204L, 0xd56d82c7d5af5844L, 0x6cba6352115d13b3L, 0x3fb3cc3c4cb673d2L, 0x5b14ce737608823dL, 0xc3d042947b4d9d09L, 0x15be86cd65bf7a76L, 0x72896127eb90f17bL, 0xeacd7326935ea13dL, 0xfbc85eb3a5cfb667L, 0x298b9763221c4d8fL, 0x284d146047d37be0L, 0xf99aae51c8fe4377L, 0xc4dbc790201f47ffL, 0x39e0363f4e66f8b0L, 0x21e0903dbe177124L, 0x4e3ce16e5a9b4a34L, 0x502969d272b05192L, 0x55f395dd777066b5L, 0xcaf3bbac8c86090dL, 0x3450b10eff592cf7L, 0x8edbbaa7fb43dd23L, 0x8cc4624b087758a2L, 0x4de23a236e734d75L, 0x96f82ab35df96018L, 0x744fca3a57366d80L, 0x3c30b7f058c689d6L, 0x54d8c98deccd6bafL, 0xaa31a308717c5a79L, 0x00a5cbf1228ff424L, 0x7faf65d94b81faf6L, 0x4d5624366334513eL, 0x1613c5c23ee26a7eL, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(long[] seed, long[] expected) { final L128X128Mix rng1 = new L128X128Mix(seed); final L128X128Mix rng2 = new L128X128Mix(seed[0], seed[1], seed[2], seed[3], seed[4], seed[5]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L128X128Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L128X128Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,064
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/DotyHumphreySmallFastCounting64Test.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class DotyHumphreySmallFastCounting64Test { @Test void testReferenceCode() { /* * Tested with respect to PractRand::RNGs::Raw::sfc64 of the C++ implementation (v0.94). * See : http://pracrand.sourceforge.net/ */ final long[] expectedSequence = { 0x383be11f844db7f4L, 0x563e7e24056ad886L, 0x959e56afde1c3f72L, 0x7924b83a8ac40b01L, 0xe3096acc85876ae6L, 0x9932c32968faf17eL, 0x5df8e164496c717bL, 0x443e63b0f0636d11L, 0xa0c1255bd56fb4ceL, 0xe9b12d67fbae4394L, 0x87a6b8f68968124bL, 0xe7a29a2c9eb466b6L, 0xcfbcb67cac7ffb22L, 0x9a77f8d8860be8e5L, 0x51c287e3d450bf11L, 0x9518d0a2cd3f16a3L, 0x36fdfd2044cbbb67L, 0x94d6e5b7e50ed797L, 0x01c80459dcc9ba5eL, 0x913aa13874b1da2aL, 0x136f9eb31f816b8dL, 0xbb68f2aba658e9f5L, 0x455f38462bb2e598L, 0x216693ead3d4036dL, 0x2e697d6093522eeeL, 0x8aa3e5e922c68cecL, 0x55f38b99e6e9fadcL, 0xc3b18937baf48d2fL, 0xd3a84a0f0781ef03L, 0x0374b8766ea7b9a7L, 0x354736eb92044fc2L, 0x7e78cca53d9bb584L, 0x6b44e298f16ca140L, 0xf1c7b84c51d8b1d8L, 0x0bee55dd0ea4439dL, 0xd9a26515c0a88471L, 0xda4c3174cafc57f8L, 0x6193f4b96362eb4bL, 0x207e9a94b58041afL, 0x5451bd65c481d8fcL, }; RandomAssert.assertEquals(expectedSequence, new DotyHumphreySmallFastCounting64(new long[] { 0x012de1babb3c4104L, 0xc8161b4202294965L, 0xb5ad4eceda1ce2a9L })); } }
3,065
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusPlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; class XoRoShiRo128PlusPlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 2; /* * Data from running the executable compiled from the author's C code: * http://xorshift.di.unimi.it/xorshift1024star.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L }; private static final long[] EXPECTED_SEQUENCE = { 0xf61550e8874b8eafL, 0x125015fce911e8f6L, 0xff0e6030e39af1a4L, 0xd5738fc2a502673bL, 0xef48cdcbefd84325L, 0xb60462c014133da1L, 0xa62c6d8b9f87cd81L, 0x52fd609a347198ebL, 0x3c717475e803bf09L, 0x1b6e66b21504a677L, 0x528f64243db486f4L, 0x3676015c33fbf0faL, 0x3e05f2ea0216a127L, 0x373343bb4159fa59L, 0xc375c54ebe2f9097L, 0x52d85b22744e0574L, 0x055dd7e34e687524L, 0xb749afc4bc4ed98aL, 0x31b972f93d117746L, 0xc0e13329779abc15L, 0xee52ec4b4ddc0091L, 0xc756c7dd1d6796d6L, 0x3ce47f42e211c63eL, 0xa635aa7ce5d06101L, 0xe8054178cbb492c1L, 0x3cc3ad122e7da816L, 0x0cbad73cdacab8fdL, 0x20aa1cbc64638b31L, 0x3bce572cfe3bc776L, 0xcc81e41637090cd8L, 0x69cc93e599f51181L, 0x2d5c9a4e509f984dL, 0xf4f3bf08ff627f92L, 0x3430e0a0e8670235L, 0x75a856b68968f466L, 0xdee1dbbb374913d7L, 0x9736e33202fbe05bL, 0x4bea0cc1151902a4L, 0x9fe7fd9d8de47d13L, 0xf011332584a1c7abL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xd0247ffd625d34bbL, 0x5247d11117b07db9L, 0xb9aec11eefe737e1L, 0xa88d6ac4c2d7f480L, 0x876e38fc5bcd7f89L, 0x14aece3dfd7a6ce9L, 0x6a7d54fb077757c9L, 0x4b295874fd6e600aL, 0x7efc54a81a770ffbL, 0xba3e5563f7fe8921L, 0x2239e13c7e891f63L, 0xb55fe548136c7487L, 0x9c25f74da0d8ee56L, 0xf0394d67496e5365L, 0x66825f2ecdf8dd31L, 0x4556f285a8ddf1e5L, 0xe36263a4651b8713L, 0xe71d97594e1fcbf4L, 0x85e29bc51ade5500L, 0x29072f022acf4424L, 0x5a6ad132ebe0e3acL, 0x27636b07bd1d0589L, 0x4bea854864d0b254L, 0x9067b3ebd340445cL, 0x8efb0e541cf4748dL, 0x77ac4cf8b2c52130L, 0x3e6d153c3fd9d216L, 0x133f5b6cb6113ae4L, 0xb412087ed3fc5f2bL, 0xaf452866aed4f35fL, 0x18f5416acb6e5e7cL, 0x98efa7dd2825dd3eL, 0x4741ee086b0c21b8L, 0x83bee0e266ace631L, 0x1365249d567dd250L, 0xe249e3e3531ce4e5L, 0xc731814bfb74fb0dL, 0x7e787fed6a4867a6L, 0xd5dcb4243d5e99b5L, 0x814f448f602da27aL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x6df051506844df28L, 0x82b35c7bdbfaf5d1L, 0x4777df68763da907L, 0xcce5e8b9296d60a4L, 0xbc6c9ad115aeca29L, 0xc07d256e39f80780L, 0xf8ff8370f70e7a3cL, 0x7febc84171245a56L, 0x81d2666d50e5025aL, 0x6bb05ddc3eba6542L, 0x9ee3a1141dec8267L, 0x699d68881fa2a2b3L, 0xd4a22412b7a931f1L, 0x806e870d51a5de43L, 0xf3c9392aca6b5165L, 0x0336ef0f76d4bbe3L, 0xa64aee05112a7bafL, 0x00d4e91ecda4e9efL, 0x72d6f199a63bae80L, 0x3e3fe49a3e9d3c16L, 0x8d01430dcfbbaf88L, 0x7c251ea9025494beL, 0x6f9d95802f2b73d4L, 0x77940cc33cb6d44bL, 0xa2923dd106c5a575L, 0x7a40e2608ecc4156L, 0xabab85f8f1f0ac30L, 0x7d46384e68e42393L, 0xe51135892364edabL, 0x30a6d989c2e1ce8cL, 0xe3a5ca50ef05e784L, 0xb166f894ee83ee6cL, 0x8034b69518bda95aL, 0xbf47c48eb7a25d81L, 0x6661fd8cc91ba9cfL, 0x0bbc9b1b8dd65082L, 0xd15af2be53778c09L, 0x5167f22075802d3cL, 0x76c2c76784ba1941L, 0x9e79d12a7bb75038L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo128PlusPlus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo128PlusPlus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo128PlusPlus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo128PlusPlus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoRoShiRo128PlusPlus rng1 = new XoRoShiRo128PlusPlus(SEED); final XoRoShiRo128PlusPlus rng2 = new XoRoShiRo128PlusPlus(SEED[0], SEED[1]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo128PlusPlus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo128PlusPlus(SEED)); } /** * This PlusPlus algorithm uses a different state update step. It overrides next() directly * and the abstract nextOutput() method should not be used. This test checks the method * throws an exception if used. */ @Test void testNextOutputThrows() { final XoRoShiRo128PlusPlus rng = new XoRoShiRo128PlusPlus(SEED); Assertions.assertThrows(UnsupportedOperationException.class, () -> rng.nextOutput()); } }
3,066
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusPlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo512PlusPlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 8; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro512plusplus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, }; private static final long[] EXPECTED_SEQUENCE = { 0x48f140e2854eae38L, 0x88d80a53206851ecL, 0xf1d255641f3ae3b3L, 0x0f4f285abdd594d8L, 0x143218246037a643L, 0xee38ae788815d8e2L, 0xb034afe60cf3bf1eL, 0x6ebe40961af2ac91L, 0x5dafb9e849412600L, 0xbf27348ef757e243L, 0x469345ef2d21ee91L, 0x4f2f7b8e0ab1c23cL, 0xfb6f8d5eeaba7c82L, 0x0d95bd0852a4ae70L, 0xec95e0448516b491L, 0xa6c62460124a0036L, 0xc206ef6cfb672cd8L, 0xa8339b4fdf8111a5L, 0xa267bc4646c60968L, 0x149f8c339958964fL, 0xea140efc2121e5faL, 0xbb274991873c8148L, 0xee6c77038d610dbaL, 0xebaab9379277d787L, 0x3966eb686a44bb24L, 0x54bb61aa4003b069L, 0xcde854e91cc2cb30L, 0x1f8f8f30b896c2e5L, 0x50b15f5e42bc2517L, 0x2f6695e6dc48d57aL, 0x35a4c31375a7b365L, 0x9927e1adf43ee3c2L, 0xe99ffb0a8dac464cL, 0x41816340c01c96d6L, 0x8f5ebe34bcadcc8cL, 0x3099b1ac7bba1f73L, 0x74ad51e9c17ca276L, 0xc8a871271ab601baL, 0x659aa958c902a843L, 0x741516f44b750698L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x3490d35943b71b6eL, 0x1381f9435a364888L, 0x583be5b08408e06cL, 0x96d9ebeb8441d875L, 0x33fb5086bca9477dL, 0x54a1318e630e3206L, 0x28d51480f6c0fc26L, 0xfc55a9d294c5d9d3L, 0x5d2e6bf2d7a8b758L, 0x2cd5f8233e87db70L, 0x1c2b1fe36201510fL, 0xd6178211474796f9L, 0x5897035554f9057bL, 0x6088bb64660e1d51L, 0x98e07f6c6a499cb9L, 0x98f9bd3445a33f43L, 0x0c28666f552423bdL, 0xb8f19aa7fdb89c1aL, 0x548713e79008b073L, 0x61d2bbb14a0f3bc1L, 0x917ec60de3ffb14eL, 0xd1fe8d6f37b9bd70L, 0x162531e606e234edL, 0x4f83a041b08cf330L, 0xc38ddec5d5179651L, 0x27e89ca3ddf9428bL, 0x14800d12f9ca493aL, 0x0dab376fd3d4d190L, 0xe1e9f6789280e16dL, 0x39025d8c68e4bf32L, 0x8112964847bb7f20L, 0x9c27cab74ab44154L, 0xdf2ecc32a56b4f74L, 0x1b520df69792caacL, 0x1bf881006d0bf761L, 0x25b263716b101941L, 0xd8d54d4602dd3719L, 0x7e6afa6cf20adcdcL, 0x4468886c474bc7bdL, 0x95aabb6847f69f47L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x218b3c94697bfd62L, 0xe2b22013826bfbcbL, 0xca92e100129f73cdL, 0x401313c6b39a0f61L, 0x595926c839e29f64L, 0x1f95ce075d9009bdL, 0x54f0f8f0f8389f19L, 0x788a0c369131e765L, 0x8d4a94df5837ac36L, 0xfe01e5ede70ca81eL, 0xc5e3709b662ae8ecL, 0x541f3cd991d5602bL, 0xcc313c38ccb11f6fL, 0xa97ccc8f19546e0dL, 0x93c3808953be0ea7L, 0x89fdac3da0e315a6L, 0xdd979f2ae97e8b4aL, 0x74fb917f5bdec895L, 0x2ac64ab3d6672713L, 0x72bc49a21ede60e0L, 0x9ec32cacadd5e908L, 0x90d7b30c1abe35a2L, 0x332bc85a538edc63L, 0xf91e8b31e02fdf2bL, 0xed40aee6f7cd0201L, 0x1bbf20497f99c4eaL, 0x390994ea6c7430bfL, 0xcb5c702a5f827df3L, 0xe21c537cda1abddfL, 0xefdbd8500965fd8bL, 0x9f8c363e8824e1eeL, 0x70bb6b5670253cdcL, 0x271cdbf6954126dbL, 0xf1942493c16240a7L, 0xfc7ad4185188e39dL, 0x0726ee3787d4b389L, 0x637d2aac6c82ff5fL, 0xdd00930da7790bf0L, 0xd3b37aa49732fe48L, 0x60a0f58457ed68a0L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo512PlusPlus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo512PlusPlus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo512PlusPlus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo512PlusPlus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo512PlusPlus rng1 = new XoShiRo512PlusPlus(SEED); final XoShiRo512PlusPlus rng2 = new XoShiRo512PlusPlus(SEED[0], SEED[1], SEED[2], SEED[3], SEED[4], SEED[5], SEED[6], SEED[7]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo512PlusPlus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo512PlusPlus(SEED)); } }
3,067
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L64X128StarStarTest.java
/* * 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.rng.core.source64; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L64X128StarStar}. */ class L64X128StarStarTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 2; } @Override public int xbgSeedSize() { return 2; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG64(seed[0], seed[1]), new XBGXoRoShiRo128(seed[2], seed[3], false)); } @Override public Mix getMix() { return AbstractLXMTest::mixStarStar; } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L64X128StarStar(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L64X128StarStarRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0xaee231d184911ad8L, 0x4520532a5549a510L, 0x71a6eb6f79366919L, 0x2083c44d4810bd6fL, }, new long[] { 0x830084a6413e9d08L, 0x9d3ad3573722e868L, 0xd64138313ff25b4fL, 0x345eda927a84a375L, 0x7c20a3612b0c8fbeL, 0xab227b85595fb299L, 0x0c8a0567f166ea51L, 0xf5983403c41db8c3L, 0x4eb1b4dd8d7663b0L, 0xb488ddd3113af675L, 0xd0a7849e601e7a14L, 0x881dd54f67e038a3L, 0xf0f052f1c25c5140L, 0x50a63f651e394656L, 0x0e5c4b325f8e2e5fL, 0xf7768e8d250a834aL, 0x6dd2857fab5822c8L, 0x261a1503b1799ed1L, 0xdc635cdfcb6b928eL, 0xfa41d9e29c8c35c9L, 0xca56e6ec0e924af1L, 0xc9de942825743c7aL, 0xe975cbf2a1d60758L, 0x671044bedafa32deL, 0x3b35dc7e2e583e6cL, 0x7cb03e41feecfc85L, 0x67972bdd63a30d60L, 0xf75b8f37d0b4717bL, 0x0d833c09bba62056L, 0xfca3a08b9be878c3L, 0x4da8c219bd7fc0f9L, 0xeebabf6885853096L, 0x65555aba641a0653L, 0x6a9f5c913a9ed878L, 0x99fbda7113e1354dL, 0x198cbfc3c00a0ca2L, 0x7387aedc8b8b69d4L, 0x26c47d85244685c1L, 0x28065c272a6b0f55L, 0x6de39d4fca78f723L, }), Arguments.of( new long[] { 0x2c157477eb19cba9L, 0x26048e417b3694adL, 0x659d21b0cf0f005bL, 0x02bb00bb4faa6188L, }, new long[] { 0xb5f6cb871d993745L, 0x8261e85c8528b091L, 0x801adfbdd328d6a9L, 0x8e9089dc51c29de2L, 0x2b0be99918898d84L, 0x97c205687d17d03cL, 0xf92e93c9b2f96d37L, 0xdc8f743d166b8bffL, 0x99db57d3bd595c68L, 0xc93392d2fb587e46L, 0xc62445f3b2ed9294L, 0xe4252d2ec389dbe6L, 0x369edad9d9b5651bL, 0xb2e0f52955f8a1bfL, 0xc9a1b6980b2c8ea8L, 0x40e4aa609c3bb19cL, 0xfaede9c7002b035cL, 0xc00b77d1b7989058L, 0x243164c89302811dL, 0xcf47804a33ca4f9dL, 0x15a1a6fc769a1a06L, 0x8be1729b907194ceL, 0xb943ef109365dec3L, 0x35070a31e43a522bL, 0x7a29a9150d6a72b9L, 0x9c67a5bbb72d971bL, 0xf2b927381613bb56L, 0xa317e83d3332ed5aL, 0x62f06a382579291cL, 0x065063eaf4967efdL, 0x4e0dbd771d7e9957L, 0xa1179b58c6d3a335L, 0x30419a0f7114b910L, 0x144eb4c373807ac2L, 0xd0904239bc2793deL, 0xe426ba924a2099f2L, 0xd013d7a457218234L, 0xe7ed5726a3141b1cL, 0xe5be2e06a8e623afL, 0xa9963ae2497e0c14L, }), Arguments.of( new long[] { 0xb84e2fb2f137f0d3L, 0x51a02981dca47a14L, 0xe176a5f236f46230L, 0xb841ae1033bb4ba1L, }, new long[] { 0x813bb3b8ef5bfe77L, 0x93d1da33cc598a2cL, 0x641bdf12d424ed18L, 0xe9002256c8326374L, 0xbfa7b1f649eebe70L, 0xa2a50c9b58d5b8a8L, 0x102d51f8895a250cL, 0x7f645e87d4c4aeb9L, 0xc63a82d7347d97c1L, 0x93b0e513a347fdc5L, 0x9fbe539e9b811777L, 0xf0197ca328174abfL, 0x9e0417be835df367L, 0x7a87a1e461c874ffL, 0x913ec6fbe78ff63dL, 0x62d24838cff9bd28L, 0x19009f9566b627cbL, 0xf30e7ecc3cd4f372L, 0x8513d9f72faaeea6L, 0x02190ed9019ae371L, 0xe1760616b1fd4efeL, 0x58a0960d45583134L, 0x377c9c9e0872d8dbL, 0x4cf6ec0f1ecfb91bL, 0x54910aebb60facc2L, 0x7637e8286fc51437L, 0x15ed477ae483d7caL, 0x7acbbd2a4d1f2901L, 0x9fc5ddd926500fb6L, 0x16c98971886a86aeL, 0xc61dcc20296a9228L, 0x90b4583569802b04L, 0x8ffeb5b929c2d785L, 0xf35e8bfdccc70e97L, 0xc427ca7da8efc0d9L, 0x3d8fe39fcaf4b913L, 0xd350ca35762a802cL, 0x340852a8b058cf12L, 0x073fa1debb4f835fL, 0xfacc317ecd1f047bL, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(long[] seed, long[] expected) { final L64X128StarStar rng1 = new L64X128StarStar(seed); final L64X128StarStar rng2 = new L64X128StarStar(seed[0], seed[1], seed[2], seed[3]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L64X128StarStar(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L64X128StarStar(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,068
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo1024StarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo1024StarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 16; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro1024star.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; private static final long[] EXPECTED_SEQUENCE = { 0x25420431d285b339L, 0x9d50004eab73a9e9L, 0xc62fb621034dc6a6L, 0x8e4f7ec7784c094fL, 0xfb233a178dddbef9L, 0x538a598a1a751e1dL, 0xb2262ac30427231cL, 0xe782bbf13a51326dL, 0x503706f497e83711L, 0x8b44a684d34f4676L, 0x10228730591a6a11L, 0x4d21c526cd1ca7c0L, 0x4497a82cf06b9274L, 0x20324535a7779084L, 0x20f683e744439960L, 0xeca1ed99f04a8802L, 0xb0710447ff1eab59L, 0x725be21e77ae9cedL, 0x0c53e906e3c75f2dL, 0xee3f9b042ee640cbL, 0xddeee0b06f807837L, 0xe7a56a2dab4ff707L, 0xc08a9562b73b0e9eL, 0x424e42a62f21f0e0L, 0x93d9ef07036ace29L, 0x3389f1d557fe67c5L, 0xb6bcecf856c408d0L, 0xed4e342feea1b9c1L, 0x8444c6a9d792ab55L, 0x0b75319a836a81b5L, 0x846082b4ea38f5bcL, 0xa9e2b47baddca313L, 0x3f1a966bb7668740L, 0xf556b1a28a741e1dL, 0xfc99602a418d56b3L, 0x50e51136f1ff265bL, 0x0b1534b756ba6913L, 0x902b3601421c7827L, 0x88a33e48fbabe7f0L, 0xc3fdf390206a509eL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x3c9956e04f3540b4L, 0x8afdc8db489de8deL, 0xc1b843dfaf80f74fL, 0x8af7f996ca0b43acL, 0xe9678ee162f4a196L, 0x64fe21412ecd8f67L, 0xa37fc756b9a58e30L, 0x54e4943ea2256f1dL, 0x462b64934d76c9a9L, 0xb05141b0175a2e75L, 0x6ce6faee43a1f5a1L, 0xaaba2a0d982ddf6fL, 0x2616ff8ac72434bcL, 0xd17792b5478719a6L, 0x5c5e252ff46288a4L, 0x37d60a9cea333634L, 0x1d54cf05358aa8fbL, 0x1b789cbbdc91b2daL, 0xc146695af4e7b3f4L, 0x9e18b93eea2800fcL, 0x8f24b7b08a478060L, 0xf9911ea6c3aee2cdL, 0x0b7c0e4de51be5aeL, 0x5545c9e12996bd73L, 0x06792d509e39c6bfL, 0x35c552ab8ff34bb0L, 0xb82ba8f4110f1004L, 0xee50f84093a0dc87L, 0x34428d22a60e61d3L, 0x2e873e2ee49252feL, 0x60b4c7c15833c7c1L, 0xe509268f45253ee3L, 0xff9324851f9edc6fL, 0xb00abd7d8b3bfaebL, 0x5c33988f7599de27L, 0xd131c3d1d58e79f0L, 0xbdd56afa4e7d3fb1L, 0x46825a94f62679b6L, 0x14e69a0ae63aa3d1L, 0x7c3bbbc3ab5dcf6bL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x844905489c673003L, 0x346119a9080539bbL, 0xd6daab0b3d76dbfcL, 0x779de94809667d8fL, 0xef431e757eb6dcdcL, 0x4afca5425c9ce0d6L, 0x2d3346e5d4e5b904L, 0xdf8c0c75d449c9b7L, 0xce8901ef4341c557L, 0x4d9c8ce74919d511L, 0xa0c9b66bf8151b0eL, 0xf27e867e82ba9960L, 0x7fbe6396e871808bL, 0x05b986f7cd526c2aL, 0x69486c6bd10e5528L, 0xec5b81a2983edceeL, 0x386c3e25b2c97169L, 0xb93097d01f6f2dc3L, 0x6d1de2eca99e0075L, 0x4fa5bb372448ef65L, 0x59a7f43fb5667effL, 0x473aef023223d925L, 0x7960ff90dd9faf6aL, 0xd6a52f1010fc621dL, 0xae0475c0252706feL, 0x04b18a9676b43b1fL, 0x23614d123c1d7981L, 0x90a3a61d347af01dL, 0xa8bb5a73ed60fa71L, 0x9bf4aebbcef5d4dcL, 0xcb055aa09030c14eL, 0xadb90edea65b9f10L, 0x18c39d4ad7d7b42bL, 0x77d582b2b6cd5b63L, 0xb1981ca7f4940a72L, 0x3831442ccb482f24L, 0x290fa8a2be437eb2L, 0xcc6e9e6945d4d357L, 0xb056b5998149a15bL, 0x14d11b85ca684993L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo1024Star(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo1024Star(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo1024Star.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo1024Star(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo1024Star(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo1024Star(SEED)); } }
3,069
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L64X256MixTest.java
/* * 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.rng.core.source64; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L64X256Mix}. */ class L64X256MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 2; } @Override public int xbgSeedSize() { return 4; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG64(seed[0], seed[1]), new XBGXoShiRo256(seed[2], seed[3], seed[4], seed[5], false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L64X256Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L64X256MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0x38ad02a4977cfc85L, 0xac591a702af353a9L, 0xc18ee497ebfa3a67L, 0xc43728a4135dbf81L, 0x501873d92a0ffc1eL, 0x21da5d9219a01f1dL, }, new long[] { 0x4a8fc9b294a2903bL, 0xe124abe9a71662ecL, 0xfc9ca7d4e1c2b278L, 0x612395c98dfbf451L, 0x131004a7cf4033f8L, 0x7d182d5855d1e58eL, 0xba7ccd83d224206dL, 0xc5e270c4ce67cbd3L, 0x9123f5d85e0e8746L, 0xc94f94c5880b6537L, 0x827cfda9abc85255L, 0xce74ef6578c18d8cL, 0xcf9a3c36d733a4a8L, 0xcf56df43d5ffff86L, 0x9cfb645b362f65a5L, 0xabf301bec6e25ac4L, 0xff95d6c7fe60dd77L, 0x69f593186b882b82L, 0x86b35b9cfad7970cL, 0xdbfca7884f3f7f13L, 0x2b9eb4307e26e1bbL, 0x31efdc45759d5be5L, 0x28b33884e8407e1dL, 0xec98fe1c2c05f0bcL, 0x071969a499ad9c2aL, 0xc85430fc3a9c2649L, 0x978cb0eb9d3a6d16L, 0xc5bf174c7b9aea3bL, 0x3da95559360b1c16L, 0x2ef8870aff685d0cL, 0xac7324e58579b175L, 0x6bf70e58e0f2e963L, 0xe1114a1cdde29c1eL, 0x1a8b54c562541b08L, 0xa9529fb0c9bb9c3aL, 0x373a68e6f14e6b2dL, 0x4e96f36a49fac53cL, 0xeb1fe0f59197fb33L, 0x1e8b4d4daa3f3311L, 0x97ff0d70df1ce349L, }), Arguments.of( new long[] { 0x13ca7c4d76bad8f0L, 0x602bf3584414eee4L, 0x3e769cf3ba98660dL, 0x61a95d3d58ed469dL, 0x6fc2fc2f5b0921cbL, 0x774d52db376df887L, }, new long[] { 0x84c5ab78c40ac14cL, 0x1d616aaae3399f43L, 0xab1a55821b3f4dc1L, 0x8bf9a5c970dbf38bL, 0x6fd9452a2dc3da53L, 0x896200532a13b11aL, 0x0c139f2ab4a12593L, 0xb9410309e3750ba9L, 0xaa18856fa85517d9L, 0xb36310525d77e950L, 0x0ffa217a0a56025bL, 0x0b948fe6b5d95704L, 0x3856404d4cf0ddc8L, 0x7c40f0f8a37b7467L, 0xdee45d9ab52003d0L, 0xfc2d9bd0ea1df302L, 0xb9c36154e4c2c927L, 0xc38a3cd9cbd64f86L, 0x80d06dd216397eb2L, 0x94568be37b47a23fL, 0xfedaf933d0ff15c2L, 0x207af70fcf265696L, 0x68f117e04406c116L, 0x798da2b4d1d1bc07L, 0x664bb84977d2914bL, 0xe95c00e85f03bee5L, 0x2468f9cb7e0c2eefL, 0xa3234bafd0d327cfL, 0xa64292c604374ef1L, 0xfde0c87c9e66f469L, 0x040437b68311143dL, 0x793d144161c5a82cL, 0xfc8d06776d37b1d1L, 0x643d0d646a26b7a3L, 0xc0cff6fc7f434038L, 0xf8658db199d75217L, 0xc34ec05c7964404aL, 0x63e8e02d10e0ca82L, 0x7c18e1e8281ff5acL, 0x82e7a4ef6040d9e1L, }), Arguments.of( new long[] { 0x95cef6dd0f721028L, 0x62729b1796f20c67L, 0x3326bb36f96c995fL, 0x7cca88f294ea8828L, 0xcd1baff0c8a55c31L, 0xb8b30298127cafc0L, }, new long[] { 0x6a6d6b182a973476L, 0xb1c8fe6c748abeb1L, 0x8feda649365ec1a8L, 0xd6fc825213db401eL, 0x5abfd540f73ebbe6L, 0x9b5ba437f32b8bd4L, 0xba19cac6c2e0b57dL, 0x81cd25461c2c6869L, 0x0a2ca0730e59d25eL, 0xc4a7794c6f1dd5ffL, 0x4acb6cbb120e5980L, 0x27db228fb1e7e793L, 0x07857cffa09d2ae4L, 0xb1f1291e2853c4c6L, 0xb3201971b3cb2cc8L, 0xbfb40ab19721f445L, 0xb247334ccee1f857L, 0xa13ac1de01fb83b3L, 0x686b00fa750819d5L, 0x2fcd849ed7921c82L, 0xda27960205f0704fL, 0xd11a3299f8af4c44L, 0xb1a23ef67d2f567cL, 0xd5ca5a9e0e4977bcL, 0x9786640a1bcd0104L, 0x43da7cef69ea3cb3L, 0x3e11acd367e77bdeL, 0xc61349825af6400fL, 0xbb6813fb3ce2ef4aL, 0x623c0fbf7b03d348L, 0xf14656bcf51235f1L, 0xd924702149fcd0c1L, 0x8d0674793d656071L, 0x9f0520e73fa64387L, 0x9c5abce00783f3faL, 0xe9f65611774feab8L, 0x4758118eef111e09L, 0xbfbe22a0b12f1be2L, 0x9ca2ce5f7a74bb75L, 0x80500fe93a8a0938L, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(long[] seed, long[] expected) { final L64X256Mix rng1 = new L64X256Mix(seed); final L64X256Mix rng2 = new L64X256Mix(seed[0], seed[1], seed[2], seed[3], seed[4], seed[5]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L64X256Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L64X256Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,070
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LXMSupportTest.java
/* * 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.rng.core.source64; import java.math.BigInteger; import java.util.SplittableRandom; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; /** * Tests for {@link LXMSupport}. * * <p>Note: The LXM generators require the LCG component to be advanced in a single * jump operation by half the period 2<sup>k/2</sup>, where {@code k} is the LCG * state size. This is performed in a single step using coefficients {@code m'} and * {@code c'}. * * <p>This class contains a generic algorithm to compute an arbitrary * LCG update for any power of 2. This can be bootstrap tested using small powers of * 2 by manual LCG iteration. Small powers can then be used to verify increasingly * large powers. The main {@link LXMSupport} class contains precomputed coefficients * {@code m'} and a precursor to {@code c'} for a jump of 2<sup>k/2</sup> * assuming the multiplier {@code m} used in the LXM generators. The coefficient * {@code c'} is computed by multiplying by the generator additive constant. The output * coefficients have the following properties: * <ul> * <li>The multiplier {@code m'} is constant. * <li>The lower half of the multiplier {@code m'} is 1. * <li>The lower half of the additive parameter {@code c'} is 0. * <li>The upper half of the additive parameter {@code c'} is odd when {@code c} is odd. * </ul> */ class LXMSupportTest { /** 2^63. */ private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63); /** 2^128. The modulus of a 128-bit LCG. */ private static final BigInteger MOD = BigInteger.ONE.shiftLeft(128); /** A mask to clear the lower 6 bits of an integer. */ private static final int CLEAR_LOWER_6 = -1 << 6; /** A mask to clear the lower 7 bits of an integer. */ private static final int CLEAR_LOWER_7 = -1 << 7; @Test void testLea64() { // Code generated using the reference java code provided by Steele and Vigna: // https://doi.org/10.1145/3485525 final long[] expected = { 0x45b8512f9ff46f10L, 0xd6ce3db0dd63efc3L, 0x47bf6058710f2a88L, 0x85b8c74e40981596L, 0xd77442e45944235eL, 0x3ea4255636bfb1c3L, 0x296ec3c9d3e0addcL, 0x6c285eb9694f6eb2L, 0x8121aeca2ba15b66L, 0x2b6d5c2848c4fdc4L, 0xcc99bc57f5e3e024L, 0xc00f59a3ad3666cbL, 0x74e5285467c20ae7L, 0xf4d51701e3ea9555L, 0x3aeb92e31a9b1a0eL, 0x5a1a0ce875c7dcaL, 0xb9a561fb7d82d0f3L, 0x97095f0ab633bf2fL, 0xfe74b5290c07c1d1L, 0x9dfd354727d45838L, 0xf6279a8801201eddL, 0x2db471b1d42860eeL, 0x4ee66ceb27bd34ecL, 0x2005875ad25bd11aL, 0x92eac4d1446a0204L, 0xa46087d5dd5fa38eL, 0x7967530c43faabe1L, 0xc53e1dd74fd9bd15L, 0x259001ab97cca8bcL, 0x5edf024ee6cb1d8bL, 0x3fc021bba7d0d7e6L, 0xf82cae56e00245dbL, 0xf1dc30974b524d02L, 0xe1f2f1db0af7ace9L, 0x853d5892ebccb9f6L, 0xe266f36a3121da55L, 0x3b034a81bad01622L, 0x852b53c14569ada2L, 0xee902ddc658c86c9L, 0xd9e926b766013254L, }; long state = 0x012de1babb3c4104L; final long increment = 0xc8161b4202294965L; for (final long e : expected) { Assertions.assertEquals(e, LXMSupport.lea64(state += increment)); } } @Test void testUnsignedMultiplyHighEdgeCases() { final long[] values = { -1, 0, 1, Long.MAX_VALUE, Long.MIN_VALUE, LXMSupport.M128L, 0xffL, 0xff00L, 0xff0000L, 0xff000000L, 0xff00000000L, 0xff0000000000L, 0xff000000000000L, 0xff000000000000L, 0xffffL, 0xffff0000L, 0xffff00000000L, 0xffff000000000000L, 0xffffffffL, 0xffffffff00000000L }; for (final long v1 : values) { for (final long v2 : values) { assertMultiplyHigh(v1, v2, LXMSupport.unsignedMultiplyHigh(v1, v2)); } } } @Test void testUnsignedMultiplyHigh() { final long[] values = new SplittableRandom().longs(100).toArray(); for (final long v1 : values) { for (final long v2 : values) { assertMultiplyHigh(v1, v2, LXMSupport.unsignedMultiplyHigh(v1, v2)); } } } private static void assertMultiplyHigh(long v1, long v2, long hi) { final BigInteger bi1 = toUnsignedBigInteger(v1); final BigInteger bi2 = toUnsignedBigInteger(v2); final BigInteger expected = bi1.multiply(bi2); Assertions.assertTrue(expected.bitLength() <= 128); Assertions.assertEquals(expected.shiftRight(64).longValue(), hi, () -> String.format("%s * %s", bi1, bi2)); } /** * Create a big integer treating the value as unsigned. * * @param v Value * @return the big integer */ private static BigInteger toUnsignedBigInteger(long v) { return v < 0 ? TWO_POW_63.add(BigInteger.valueOf(v & Long.MAX_VALUE)) : BigInteger.valueOf(v); } /** * Create a 128-bit big integer treating the value as unsigned. * * @param hi High part of value * @param lo High part of value * @return the big integer */ private static BigInteger toUnsignedBigInteger(long hi, long lo) { return toUnsignedBigInteger(hi).shiftLeft(64).add(toUnsignedBigInteger(lo)); } @Test void testUnsignedAddHigh() { // This will trigger a carry as the sum is 2^64. // Note: b must be odd. long a = 1; long b = -1; // The values are adjusted randomly to make 'b' smaller // and 'a' larger but always maintaining the sum to 2^64. final SplittableRandom sr = new SplittableRandom(); // Number of steps = 2^5 final int pow = 5; // Divide 2^64 by the number of steps. This ensures 'a' // will not wrap if all steps are the maximum step. final long range = 1L << (64 - pow); for (int i = 1 << pow; i-- != 0;) { // Check invariants Assertions.assertEquals(0L, a + b); Assertions.assertEquals(1L, b & 0x1); // Should carry assertAddHigh(a, b); // Should not carry assertAddHigh(a - 1, b); // Update. Must be even to maintain an odd b final long step = sr.nextLong(range) & ~0x1; a += step; b -= step; } // Random. This should carry 50% of the time. for (int i = 0; i < 1000; i++) { assertAddHigh(sr.nextLong(), sr.nextLong() | 1); } } private static void assertAddHigh(long a, long b) { // Reference using comparedUnsigned. If the sum is smaller // than the argument 'a' then adding 'b' has triggered a carry final long sum = a + b; final long carry = Long.compareUnsigned(sum, a) < 0 ? 1 : 0; Assertions.assertEquals(carry, LXMSupport.unsignedAddHigh(a, b), () -> String.format("%d + %d", a, b)); } @ParameterizedTest @CsvSource({ "6364136223846793005, 1442695040888963407, 2738942865345", // LXM 64-bit multiplier: // -3372029247567499371 == 0xd1342543de82ef95L "-3372029247567499371, 9832718632891239, 236823998", "-3372029247567499371, -6152834681292394, -6378917984523", "-3372029247567499371, 12638123, 21313", "-3372029247567499371, -67123, 42", }) void testLcgAdvancePow2(long m, long c, long state) { // Bootstrap the first powers long s = state; for (int i = 0; i < 1; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 0), "2^0 cycles"); for (int i = 0; i < 1; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 1), "2^1 cycles"); for (int i = 0; i < 2; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 2), "2^2 cycles"); for (int i = 0; i < 4; i++) { s = m * s + c; } Assertions.assertEquals(s, lcgAdvancePow2(state, m, c, 3), "2^3 cycles"); // Larger powers should align for (int n = 3; n < 63; n++) { final int n1 = n + 1; Assertions.assertEquals( lcgAdvancePow2(lcgAdvancePow2(state, m, c, n), m, c, n), lcgAdvancePow2(state, m, c, n1), () -> "2^" + n1 + " cycles"); } // Larger/negative powers are ignored for (final int i : new int[] {64, 67868, Integer.MAX_VALUE, Integer.MIN_VALUE, -26762, -2, -1}) { final int n = i; Assertions.assertEquals(state, lcgAdvancePow2(state, m, c, n), () -> "2^" + n + " cycles"); } } @ParameterizedTest @CsvSource({ "126868183112323, 6364136223846793005, 1442695040888963407, 2738942865345, 3467819237274724, 12367842684328", // Force carry when computing (m + 1) with ml = -1 "-126836182831123, -1, 12678162381123, -12673162838122, 12313212312354235, 127384628323784", "92349876232, -1, 92374923739482, 2394782347892, 1239748923479, 627348278239", // LXM 128-bit multiplier: // -3024805186288043011 == 0xd605bbb58c8abbfdL "1, -3024805186288043011, 9832718632891239, 236823998, -23564628723714323, -12361783268182", "1, -3024805186288043011, -6152834681292394, -6378917984523, 127317381313, -12637618368172", "1, -3024805186288043011, 1, 2, 3, 4", "1, -3024805186288043011, -1, -78, -56775, 121", }) void testLcg128AdvancePow2(long mh, long ml, long ch, long cl, long stateh, long statel) { // Bootstrap the first powers BigInteger s = toUnsignedBigInteger(stateh, statel); final BigInteger m = toUnsignedBigInteger(mh, ml); final BigInteger c = toUnsignedBigInteger(ch, cl); for (int i = 0; i < 1; i++) { s = m.multiply(s).add(c).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, 0), "2^0 cycles"); for (int i = 0; i < 1; i++) { s = m.multiply(s).add(c).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, 1), "2^1 cycles"); for (int i = 0; i < 2; i++) { s = m.multiply(s).add(c).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, 2), "2^2 cycles"); for (int i = 0; i < 4; i++) { s = m.multiply(s).add(c).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, 3), "2^3 cycles"); // Larger powers should align for (int n = 3; n < 127; n++) { final int n1 = n + 1; // The method under test does not return the lower half (by design) so // we compute it using the same algorithm final long lo = lcgAdvancePow2(statel, ml, cl, n); final long hi = lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, n); Assertions.assertEquals( lcgAdvancePow2High(hi, lo, mh, ml, ch, cl, n), lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, n1), () -> "2^" + n1 + " cycles"); } // Larger/negative powers are ignored for (final int i : new int[] {128, 67868, Integer.MAX_VALUE, Integer.MIN_VALUE, -26762, -2, -1}) { final int n = i; Assertions.assertEquals(stateh, lcgAdvancePow2High(stateh, statel, mh, ml, ch, cl, n), () -> "2^" + n + " cycles"); } } @Test void testLcg64Advance2Pow32Constants() { // Computing with a addition of 1 will compute: // m^(2^32) // product {m^(2^i) + 1} for i in [0, 31] final long[] out = new long[2]; lcgAdvancePow2(LXMSupport.M64, 1, 32, out); Assertions.assertEquals(LXMSupport.M64P, out[0], "m'"); Assertions.assertEquals(LXMSupport.C64P, out[1], "c'"); // Check the special values of the low half Assertions.assertEquals(1, (int) out[0], "low m'"); Assertions.assertEquals(0, (int) out[1], "low c'"); } @Test void testLcg128Advance2Pow64Constants() { // Computing with an addition of 1 will compute: // m^(2^64) // product {m^(2^i) + 1} for i in [0, 63] final long[] out = new long[4]; lcgAdvancePow2(1, LXMSupport.M128L, 0, 1, 64, out); Assertions.assertEquals(LXMSupport.M128PH, out[0], "high m'"); Assertions.assertEquals(LXMSupport.C128PH, out[2], "high c'"); // These values are not stored. // Their special values simplify the 128-bit multiplication. Assertions.assertEquals(1, out[1], "low m'"); Assertions.assertEquals(0, out[3], "low c'"); } /** * Test the precomputed 64-bit LCG advance constant in {@link LXMSupport} can be used * to compute the same jump as the bootstrap tested generic version in this class. */ @Test void testLcgAdvance2Pow32() { final SplittableRandom r = new SplittableRandom(); final long[] out = new long[2]; for (int i = 0; i < 2000; i++) { // Must be odd final long c = r.nextLong() | 1; lcgAdvancePow2(LXMSupport.M64, c, 32, out); final long a = out[1]; // Test assumptions Assertions.assertEquals(1, (a >>> 32) & 0x1, "High half c' should be odd"); Assertions.assertEquals(0, (int) a, "Low half c' should be 0"); // This value can be computed from the constant Assertions.assertEquals(a, LXMSupport.C64P * c); } } /** * Test the precomputed 128-bit LCG advance constant in {@link LXMSupport} can be used * to compute the same jump as the bootstrap tested generic version in this class. */ @Test void testLcgAdvance2Pow64() { final SplittableRandom r = new SplittableRandom(); final long[] out = new long[4]; for (int i = 0; i < 2000; i++) { // Must be odd for the assumptions final long ch = r.nextLong(); final long cl = r.nextLong() | 1; lcgAdvancePow2(1, LXMSupport.M128L, ch, cl, 64, out); final long ah = out[2]; // Test assumptions Assertions.assertEquals(1, ah & 0x1, "High half c' should be odd"); Assertions.assertEquals(0, out[3], "Low half c' should be 0"); // This value can be computed from the constant Assertions.assertEquals(ah, LXMSupport.C128PH * cl); } } /** * Compute the multiplier {@code m'} and addition {@code c'} to advance the state of a * 64-bit Linear Congruential Generator (LCG) a number of consecutive steps: * * <pre> * s = m' * s + c' * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the accumulated multiplier and addition for the * given number of steps expressed as a power of 2. Provides support to advance for * 2<sup>k</sup> for {@code k in [0, 63)}. Any power {@code >= 64} is ignored as this * would wrap the generator to the same point. Negative powers are ignored but do not * throw an exception. * * <p>Based on the algorithm from: * * <blockquote>Brown, F.B. (1994) Random number generation with arbitrary strides, * Transactions of the American Nuclear Society 71.</blockquote> * * @param m Multiplier * @param c Constant * @param k Number of advance steps as a power of 2 (range [0, 63]) * @param out Output result [m', c'] * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ private static void lcgAdvancePow2(long m, long c, int k, long[] out) { // If any bits above the first 6 are set then this would wrap the generator to // the same point as multiples of period (2^64). // It also identifies negative powers to ignore. if ((k & CLEAR_LOWER_6) != 0) { // m'=1, c'=0 out[0] = 1; out[1] = 0; return; } long mp = m; long a = c; for (int i = k; i != 0; i--) { // Update the multiplier and constant for the next power of 2 a = (mp + 1) * a; mp *= mp; } out[0] = mp; out[1] = a; } /** * Compute the advanced state of a 64-bit Linear Congruential Generator (LCG). The * base generator advance step is: * * <pre> * s = m * s + c * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the update coefficients and applies them to the * given state. * * <p>This method is used for testing only. For arbitrary jumps an efficient implementation * would inline the computation of the update coefficients; or for repeat jumps of the same * size pre-compute the coefficients once. * * <p>This is package-private for use in {@link AbstractLXMTest} to provide jump functionality * to a composite LXM generator. * * @param s State * @param m Multiplier * @param c Constant * @param k Number of advance steps as a power of 2 (range [0, 63]) * @return the new state * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ static long lcgAdvancePow2(long s, long m, long c, int k) { final long[] out = new long[2]; lcgAdvancePow2(m, c, k, out); final long mp = out[0]; final long ap = out[1]; return mp * s + ap; } /** * Compute the multiplier {@code m'} and addition {@code c'} to advance the state of a * 128-bit Linear Congruential Generator (LCG) a number of consecutive steps: * * <pre> * s = m' * s + c' * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the accumulated multiplier and addition for the * given number of steps expressed as a power of 2. Provides support to advance for * 2<sup>k</sup> for {@code k in [0, 127)}. Any power {@code >= 128} is ignored as * this would wrap the generator to the same point. Negative powers are ignored but do * not throw an exception. * * <p>Based on the algorithm from: * * <blockquote>Brown, F.B. (1994) Random number generation with arbitrary strides, * Transactions of the American Nuclear Society 71.</blockquote> * * @param mh High half of multiplier * @param ml Low half of multiplier * @param ch High half of constant * @param cl Low half of constant * @param k Number of advance steps as a power of 2 (range [0, 127]) * @param out Output result [m', c'] packed as high-half, low-half of each constant * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ private static void lcgAdvancePow2(final long mh, final long ml, final long ch, final long cl, int k, long[] out) { // If any bits above the first 7 are set then this would wrap the generator to // the same point as multiples of period (2^128). // It also identifies negative powers to ignore. if ((k & CLEAR_LOWER_7) != 0) { // m'=1, c'=0 out[0] = out[2] = out[3] = 0; out[1] = 1; return; } long mph = mh; long mpl = ml; long ah = ch; long al = cl; for (int i = k; i != 0; i--) { // Update the multiplier and constant for the next power of 2 // c = (m + 1) * c // Create (m + 1), carrying any overflow final long mp1l = mpl + 1; final long mp1h = mp1l == 0 ? mph + 1 : mph; ah = LXMSupport.unsignedMultiplyHigh(mp1l, al) + mp1h * al + mp1l * ah; al = mp1l * al; // m = m * m // Note: A dedicated unsignedSquareHigh is benchmarked in the JMH module mph = LXMSupport.unsignedMultiplyHigh(mpl, mpl) + 2 * mph * mpl; mpl = mpl * mpl; } out[0] = mph; out[1] = mpl; out[2] = ah; out[3] = al; } /** * Compute the advanced state of a 128-bit Linear Congruential Generator (LCG). The * base generator advance step is: * * <pre> * s = m * s + c * </pre> * * <p>A number of consecutive steps can be computed in a single multiply and add * operation. This method computes the update coefficients and applies them to the * given state. * * <p>This method is used for testing only. For arbitrary jumps an efficient implementation * would inline the computation of the update coefficients; or for repeat jumps of the same * size pre-compute the coefficients once. * * <p>Note: Returns only the high-half of the state. For powers of 2 less than 64 the low * half can be computed using {@link #lcgAdvancePow2(long, long, long, int)}. * * <p>This is package-private for use in {@link AbstractLXMTest} to provide jump functionality * to a composite LXM generator. * * @param sh High half of state * @param sl Low half of state * @param mh High half of multiplier * @param ml Low half of multiplier * @param ch High half of constant * @param cl Low half of constant * @param k Number of advance steps as a power of 2 (range [0, 127]) * @return high half of the new state * @see <A * href="https://www.osti.gov/biblio/89100-random-number-generation-arbitrary-strides"> * Brown, F.B. (1994) Random number generation with arbitrary strides, Transactions of * the American Nuclear Society 71</a> */ static long lcgAdvancePow2High(long sh, long sl, long mh, long ml, long ch, long cl, int k) { final long[] out = new long[4]; lcgAdvancePow2(mh, ml, ch, cl, k, out); final long mph = out[0]; final long mpl = out[1]; final long ah = out[2]; final long al = out[3]; // Perform the single update to the state // m * s + c long hi = LXMSupport.unsignedMultiplyHigh(mpl, sl) + mpl * sh + mph * sl + ah; // Carry propagation of // lo = sl * mpl + al final long lo = sl * mpl; if (Long.compareUnsigned(lo + al, lo) < 0) { ++hi; } return hi; } }
3,071
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L64X1024MixTest.java
/* * 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.rng.core.source64; import java.util.Arrays; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.Arguments; /** * Test for {@link L64X1024Mix}. */ class L64X1024MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 2; } @Override public int xbgSeedSize() { return 16; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG64(seed[0], seed[1]), new XBGXoRoShiRo1024(Arrays.copyOfRange(seed, 2, 18), false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L64X1024Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L64X1024MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0x6d03aad14e58f8beL, 0xd4c213de342c1162L, 0xd597eafd698d6175L, 0xe97dcf3d06517a81L, 0x8fea00579bc8811fL, 0x6309bcb2aea9a4deL, 0x7ac44e80035abac9L, 0xa675b5664574acfaL, 0x6f0b919d4411bd4bL, 0xc4385eca083e11f4L, 0xae4da78f6ee015d7L, 0x5ed8e1cfcf602e2cL, 0x9adebb2f8ee338e8L, 0x7c42477aca7bf3e5L, 0xd5c934704ecce9c8L, 0xa7fcd1cc6f3cc3d9L, 0x4fcda8f3c0f0e9b9L, 0xf5a0bef115769b05L, }, new long[] { 0xdf77d6d9db757098L, 0x0769611d12b166c7L, 0x708c4e0c0b172d54L, 0x85a2116d903b3110L, 0x3907a7d0cd664028L, 0xab1cf1a27126232aL, 0xf5a0ffc53b0e881cL, 0x0dc4aacd8d4ab80fL, 0x6378f240f93e410eL, 0xf4b3f7453749afbbL, 0xda50ef44cffd1ac0L, 0xe4b8986625162c7bL, 0x9cf06807071e3298L, 0x5601d712f9216a74L, 0xe5f63284ff4cdb0eL, 0xa4f750918672c1f1L, 0x0c282fc046e88796L, 0x02a3c158d433bb9cL, 0x3a4a103208803a12L, 0x3c7c9892ecbe1435L, 0x53fe51d586d0a9c9L, 0x0b9d8f499ac3b7adL, 0xa60744cd33c8237eL, 0xfcd11a8401f66410L, 0x27a1a8ef0083950eL, 0x83a43fca0b04f357L, 0xea1a6ad13d53f526L, 0xd46548f3c38cbfa8L, 0x66510fffc2efd464L, 0x0fa0ea2a25a26e12L, 0x95b947b27fc1e38dL, 0xae48ee91936b4a0cL, 0x3b6a6fe3aa245f36L, 0x4d172e1c972b6029L, 0xf56228f6cfaa19f8L, 0x132c2d231740e656L, 0x7b414119880f0602L, 0x7b04530dee6f280dL, 0x570efef48afa46c1L, 0x1526790b50b1c5f0L, }), Arguments.of( new long[] { 0x4e5d2758950ce2f6L, 0x26eb64e6f6ce2f4cL, 0x67dbaeb866eb5146L, 0x2e53f5d363ccc0caL, 0xb082d1f77b99b907L, 0x3d739ee76d3218a5L, 0x2e94a38e16245b39L, 0x13412e76abeca10cL, 0x5e5454bc36fbec04L, 0x1bb9d711b459e3c6L, 0x21e532cad599a4b6L, 0xee674b9fa2375928L, 0xd4142c6ed43d6ad9L, 0x90fb3eb966686cbdL, 0x668de128668949acL, 0xf90dd2df394ec382L, 0x912c0e71f4ed3031L, 0xf0c5b9f6cce6f885L, }, new long[] { 0xe6f95d595a4af8f8L, 0x0c17ba67cad911c8L, 0x404d3881e064f874L, 0x8d327b13d44d02ddL, 0xbb2d324c29f355ddL, 0x50330bbc22e58c05L, 0x273b48550711ac35L, 0x52c7c59b6e5eb50dL, 0xc569a2c2b5b750b4L, 0x48faa3df570c128bL, 0xa23d12062036f287L, 0xd9503f17c8551f7cL, 0x0981a8e88e96ed3cL, 0x02334ba7bd2c7a50L, 0xc2130c5adb86f369L, 0x752eb27167b77d0cL, 0xbdf62fff413892b4L, 0x4fc6ce43dbb707baL, 0x2be070d3f3ac6a21L, 0x92ba5a0c83bf80f1L, 0xfcfd5662258e8ff9L, 0xe24f65da07bbd2b5L, 0x76c40dd4fd3c0f46L, 0x6a89d17f8ce1591bL, 0x8e4aab3bcf6e44d4L, 0x75af39a30c384e90L, 0x6cbe6c1c78cf01c9L, 0x5843c4c0cdd76fbeL, 0x23e4b707d2e89314L, 0xbb5d3050cdc05ddbL, 0x7856e971b410dabcL, 0xc525d7da4da96e56L, 0x6cac7c7d4baca7e5L, 0x03e6c6839e74d3aaL, 0x0ac2f86811ef3634L, 0xc958a175552a7795L, 0xbe2887523e1c4024L, 0x57fc2e99d06003c9L, 0xd6ad5ef42ecbb49bL, 0xa3b621ec83b1871fL, }), Arguments.of( new long[] { 0x2adf28f2d3abfe32L, 0xc30b0861ed8bd4c4L, 0x282546a94af8b08fL, 0xbfba98ae6a0461c3L, 0xba8d7237f5194544L, 0x030e02b1530cf49eL, 0x7da2ef20c7469ecdL, 0x449858a06567c5feL, 0x5efd6bd4c4f2b24aL, 0x6ad10da447eb6b22L, 0x411402a4cf9eeeffL, 0x16d11360e0712af1L, 0xb1fa3007f9f4588aL, 0x5054dcb198cd5032L, 0x73b7545983150dcdL, 0x8f61e32fb7b252ffL, 0x51ca7eec0f4430efL, 0x24d1f99aabf6a75cL, }, new long[] { 0x6e7ff191c0830aacL, 0x4b3752f3792d8ec8L, 0xcda3452b26f49744L, 0x72790a252653f225L, 0x9f8ea6be4ff231efL, 0x2a95cbe1c40b0fe9L, 0xd8e7239fe90daa8dL, 0x8828dc21a1ec9635L, 0x3b0f343e895c642bL, 0x8b935a728e21d134L, 0xdec84687f9fedec0L, 0xfbf6be15ef1f2b97L, 0x9a46a2ddbbf6d697L, 0x0bb2e54396b32d7cL, 0x80dbac0072f34633L, 0x343fee693c38db85L, 0x14f6fa0287518153L, 0xc09a84f9a1b26197L, 0x09b257e0d0be4784L, 0xd13870d9410703c6L, 0x4a5428ab4f0a16edL, 0xe089e6e6cf4ff84aL, 0x41473f06b79df0f3L, 0xc8620e2f443fd831L, 0x170e9539f618fd88L, 0x8eb8e70ce5b18844L, 0xbd429d12f6886994L, 0xa50726552d16cd8dL, 0xe6534e392bef0bf2L, 0x77e41ca7e1e3bdc1L, 0x08d24ebe01774c6dL, 0x42aae0536ce8daaeL, 0x4d2790f2f934b5e0L, 0x9f3102e25874a2c4L, 0x4d7a5c745eba29bdL, 0xdd951065bc1b84b7L, 0x9ba99966e4e55ae5L, 0x24c22788322d0a63L, 0x7e4553bacad3a606L, 0x717d7d11b6610246L, })); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L64X1024Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L64X1024Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,072
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128StarStarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo128StarStarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 2; /* * Data from running the executable compiled from the author's C code: * http://xorshift.di.unimi.it/xorshift1024star.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L }; private static final long[] EXPECTED_SEQUENCE = { 0x8856e974cbb6da12L, 0xd1704f3601beb952L, 0x368a027941bc61e7L, 0x882b24dcfa3ada58L, 0xfa8dafb3363143fbL, 0x2eb9417a5dcf7654L, 0xed8722e0a73e975bL, 0x435fff57a631d485L, 0x954f1ad2377632b8L, 0x9aa2f4dcba28ab71L, 0xaca10369f96ac911L, 0x968088e7277d0369L, 0x662e442ae32c42b4L, 0xe1cd476f71dd058eL, 0xb462a3c2bbb650f8L, 0x74749215e8c07d08L, 0x1629f3cb1a671dbbL, 0x3636dcc702eadf55L, 0x97ae682e61cb3f57L, 0xfdf8fc5ea9541f3bL, 0x2dfdb23d99c34accL, 0x68bef4f41a8f4113L, 0x5cd03dc43f7af892L, 0xdc2184abe0565da1L, 0x1dfaece40d9f96d0L, 0x7d7b19285818ab71L, 0xedea7fd3a0e47018L, 0x23542ee7ed294823L, 0x1719f2b97bfc26c4L, 0x2c7b7e288b399818L, 0x49fa00786a1f5ad9L, 0xd97cdfbe81700be2L, 0x557480baa4d9e5b2L, 0x840a0403c0e85d92L, 0xb4d5c6b2dc19dab2L, 0xdf1b570e3bf1cf1bL, 0x26d1ac9455ccc75fL, 0xdcc0e5fe06d1e231L, 0x5164b7650568120eL, 0x5fa82f6598483607L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xbfc83ba4fd4868deL, 0x3f3eed826e632fddL, 0x9dff0e4eeebb7d32L, 0x686d1cc4eabb9535L, 0xc091c73d7fbf9d0fL, 0x1042f833a9e605c8L, 0x525ef7b2826c2f23L, 0xe550c19eaaf09c9cL, 0x091b0069cde91826L, 0x0cd7b89fce47625dL, 0x51f7596cfccd79d2L, 0xf36137464d58d44aL, 0xc41979c626c4523cL, 0x6fe485d7bc4c268bL, 0x70d606b170542c89L, 0xb2a1ebcaa0b1411eL, 0x4fec797dbbdeac3eL, 0x55c95cc08792d434L, 0x10e8fa257142e24aL, 0x6a7203a3c55725dfL, 0x4c5606f2b8a65adbL, 0x5fc275ff7c58e74fL, 0x52272729918e8ac7L, 0xd495b534244c4ad2L, 0x74be6afb953ce5aeL, 0xe61156ee003e41c9L, 0x5098c77788c9bf25L, 0xde679d7898225baaL, 0x3568366f6aefe488L, 0x2afe2420f9f9dbccL, 0x1ca0bcb15f1d1accL, 0xf1677c09f3df59e1L, 0x45dadcac4957d8d0L, 0xc46265d2810fe48eL, 0xa9234384080ec825L, 0x4a4e054ced918e13L, 0xa9b19565638b0e2bL, 0x755bf81250abd6c1L, 0x2b8ba00b2714bf8fL, 0x5e165740374e0fa6L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x77781c11728ccab7L, 0x53ebc2b248baf6bfL, 0x891c834850b9a60aL, 0x3d75b3f460049bc4L, 0x0ce2e3bec7efd177L, 0xcb3eac404f71e15dL, 0x4d1dd0a773be7287L, 0x2ef0fbefdd874642L, 0x063a585e07c58b9dL, 0xd5b5174e803858efL, 0x40dfddff6a4743efL, 0xfa4e62e3fb9b2de2L, 0x66fa3013cb6c4ca0L, 0x5b53086fea56344fL, 0x2ed92fdc75a14297L, 0x0cf92e9456f1e047L, 0x5455e58538979accL, 0xb4226b69b40f5c89L, 0xe6de8c807f5fdf89L, 0xf9c898bc7c8815cbL, 0x1c0363d696d06dccL, 0x4d7765ac48833302L, 0x84cf12882a284c7eL, 0x1d19d839c41f08b7L, 0xec30f743f1fdf8beL, 0x61a7536651dadd56L, 0xff456aeab1bc73cfL, 0x671390bb0e2dcad4L, 0xb51b533c7dc9cbccL, 0xa78c13a798610f10L, 0xb1cc573c29364864L, 0x23fe45909c9fcd47L, 0xd17b8d91ad355118L, 0x1dd7b85f071efd1bL, 0x518bf93d445908ccL, 0x31a212d5f4cc8458L, 0x62b7ffe46b91633eL, 0x079af812760a6633L, 0x596c95fed05028e7L, 0x6277ec5c12ea9677L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo128StarStar(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo128StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo128StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo128StarStar(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoRoShiRo128StarStar rng1 = new XoRoShiRo128StarStar(SEED); final XoRoShiRo128StarStar rng2 = new XoRoShiRo128StarStar(SEED[0], SEED[1]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo128StarStar(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo128StarStar(SEED)); } }
3,073
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512StarStarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo512StarStarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 8; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro512starstar.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, }; private static final long[] EXPECTED_SEQUENCE = { 0x462c422df780c48eL, 0xa82f1f6031c183e6L, 0x60559add0e1e369aL, 0xf956a2b900083a8dL, 0x0e5c039df1576573L, 0x2f35cef71b14aa24L, 0x5809ea8aa1d5a045L, 0x3e695e3189ccf9bdL, 0x1eb940ee4bcb1a08L, 0x78b72a0927bd9257L, 0xe1a8e8d6dc64600bL, 0x3993bff6e1378a4bL, 0x439161ee3b5d5cc8L, 0xac6ca2359fe7f321L, 0xc4238c5785d320e2L, 0x75cf64526530aed5L, 0x679241ffc120e2b1L, 0xded30a8f20b24c73L, 0xff8ac62cff0deb9bL, 0xe63a25973df23c45L, 0x74742f9096c56401L, 0xc573afa2368288acL, 0x9b1048cf2daf9f9dL, 0xe7d9720c2f51ca5fL, 0x38a21e1f7a441cedL, 0x78835d75a9bd17a6L, 0xeb64167a723de35fL, 0x9455dd663e40620cL, 0x88693a769f203ed1L, 0xea5f0997a281cffcL, 0x2662b83f835f3273L, 0x5e90efde2150ed04L, 0xd481b14551c8f8d9L, 0xf2e4d714a0ab22d7L, 0xdfb1a8f0637a2013L, 0x8cd8d8c353640028L, 0xb4ce3b66785e0cc6L, 0xa51386e09b6ab734L, 0xfeac4151ac4a3f8dL, 0x0e5679853ab5180bL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x49738e6a1dc6cfe4L, 0x20d43df324825b53L, 0x901311368abdf6ffL, 0x5b6a89ae30e497cfL, 0xf39563a09132cd18L, 0x31617c165823312dL, 0xf661249f2e13379aL, 0x83182e7f15ea1d74L, 0x2138679c77623fe3L, 0x659ea7f79918b8edL, 0x95d1cd36efefe94aL, 0xdca9a32638633311L, 0x29e61a381e2d6cd7L, 0xe77e58a6f50d890eL, 0x2bd956f2c3efb142L, 0xf7ac562caac34825L, 0x211db4255861df4bL, 0x0631b9f14d2c23f5L, 0x3fd2f6b40055cee4L, 0x07303eb9aba24a7bL, 0x3edb03a893e24a16L, 0x82d9065956477f0aL, 0x4ff3574b1ae46dd4L, 0xaaf0f48c44668055L, 0x2e778b8218a3fe10L, 0xe29fe50ff75a3fd4L, 0x5ab758438572ba1cL, 0x94bb2aca7d056186L, 0x7699c4588a722dc4L, 0x131fd3c8d477a06aL, 0xc87f6eb21f8d91b8L, 0x5a273a9c0a89e24fL, 0xa4466c28e147be23L, 0x877e97af45d42bbcL, 0x07f0c9e5e1fb133fL, 0x3a2f2422d2a9af03L, 0x2f1f49ee5e1d5207L, 0xafca316bd4672318L, 0x91e9df4fe8cb3cddL, 0x8857a767c3a1d387L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x1d1aaa6cb41463c7L, 0xf37f8d9edc99e22fL, 0xa9f678cc5b0519acL, 0xcfe2d9024aede22fL, 0x1ba8d18326d7c87eL, 0xcdd7d8c3fcc9d409L, 0x270738c721177babL, 0xaebe345f36cbfef9L, 0xece8a978e6d76e43L, 0x6e4aaa9e351488c7L, 0x4ed730591155ad3cL, 0x374d8de2429482feL, 0x1d8ff3451265eee2L, 0x3e74d38a2e3d2994L, 0xfb9aec450dbf9ba1L, 0x70120a3d50950b7eL, 0xe48a3a80393a2203L, 0xb50eb0af3bb7e9f5L, 0xb778f992ecea024fL, 0xce7b77bca13db0c3L, 0x7b579d90909ddcdcL, 0xce2d49db02b06f24L, 0x294fb69e2b1b3431L, 0xc6f9fc24c0d9d2dfL, 0xa41564ad936d9312L, 0xa6b50f2f160adfeeL, 0x32eb7357197f57daL, 0x17bfbb289d8f2c1eL, 0x4350c6747c718f54L, 0x21a283a2364151b2L, 0x059f807782775667L, 0xcc46db651af55502L, 0xdeaf921ddd2f7737L, 0xcacf5eb1ed4ba4f7L, 0x7ab712770f8f2401L, 0xcae044ede9c64460L, 0x0760c193ee64c8d3L, 0x234a3a0cb3870369L, 0x845cee292225f32dL, 0xd3bd2343d30d3057L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo512StarStar(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo512StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo512StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo512StarStar(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo512StarStar rng1 = new XoShiRo512StarStar(SEED); final XoShiRo512StarStar rng2 = new XoShiRo512StarStar(SEED[0], SEED[1], SEED[2], SEED[3], SEED[4], SEED[5], SEED[6], SEED[7]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo512StarStar(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo512StarStar(SEED)); } }
3,074
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo1024StarStarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo1024StarStarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 16; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro1024starstar.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; private static final long[] EXPECTED_SEQUENCE = { 0x462c422df780c48eL, 0xbe94d15abff76d8aL, 0xb4dbef0e8d88ef2eL, 0xdfa2836db33bfc43L, 0x2a4965b718a2e4f1L, 0xce2042d1aa74d50dL, 0xa6febfa96d04f58dL, 0xb16b1ce69018ab5dL, 0xd9a067d3c7a7d9ffL, 0xe6c40f3b3e470500L, 0x54c0b9c458ae5b94L, 0xfba57390e5542333L, 0x9e1670e4da0647b0L, 0x118cacc5ae1d413cL, 0x855f38d9f9975117L, 0xb1d8ae900456c302L, 0x30d6603089b0b5a5L, 0x9b0a5cdd71d36a73L, 0x22598c5adb8a49e3L, 0xbe240590cf3abae3L, 0x9c474364766386e4L, 0x675f099732c21ff2L, 0x432308deff79f4ccL, 0x18106f6cbcbb93d5L, 0x5f87dd27193d4bf5L, 0xd540713e20f70062L, 0xa8e03c5477d99848L, 0xc01f257b1ad88046L, 0x67522ec1327b3994L, 0x4c05d92051d406faL, 0xa03daf3fcd37a5ccL, 0x821445c6408c9722L, 0xf7bbbffc2db460bdL, 0x5b42694c4af4d5caL, 0x408899b212aec78eL, 0x8cf109d6952df65eL, 0xb7e8d62389e997cdL, 0xf4d82497338d8c89L, 0x7f53cea4f43609b9L, 0xa0ecb8e0fa98f352L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x5b636f163fc729e7L, 0x707beb36c524d1bdL, 0xa04aa6338804faa5L, 0xa05a8b17c27aadbaL, 0xaa753a7fb6657575L, 0xe22cf9d201f9ee0cL, 0x922b4f8e56fae8bdL, 0xb460d44f4dac553aL, 0x6b6a2771b70f8dc9L, 0x9ba789987085a9eeL, 0x5d3a89d1d38e935eL, 0x4d8cf26eba3ceb11L, 0x17773c0482382356L, 0x7348d4e4e6ff6ea7L, 0x21f79b80d74d2e00L, 0xea221a71545c67b9L, 0x49aeedb5ca88a5e0L, 0xdf91aaef5bca531bL, 0x91ad24c1bc05880aL, 0x1b4c2a769c2a0602L, 0xfb45e555e943d087L, 0x2fa45261b0447ae8L, 0xc7df5ba87f846bebL, 0x0ee7351cbc43681eL, 0x475aefe9ee1743ceL, 0x277a4780d6ac2a37L, 0xbc88b9e78bad80beL, 0x3cc1034ce7df5ca4L, 0x1f10fcc26ef5376aL, 0x09b9a2233e5bc52fL, 0xe074e46d4d117fecL, 0x5303bc71643cb162L, 0xbd148f7c799e6bb3L, 0xb562ea36a5c9ad62L, 0xbb0ed30e291a901fL, 0x0be93a50e3ad0c4aL, 0x08a3a364587f88a9L, 0xc799a663b02566efL, 0x69d258c77cf97b43L, 0x8e14b0ffaf0f6bb2L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x29d4cc1fe4502216L, 0x81fe29054d484511L, 0xcf4e2f1f65dc852aL, 0x05993959c8d9d985L, 0xb57512c9299013c2L, 0xd2b8a57fc03dd67aL, 0x76ab1e45f4b8ff83L, 0xd276813593cac49bL, 0xc2f796572548f66fL, 0x7bad52ec5018d885L, 0x7d679c0324cdb96cL, 0xae500f725e175105L, 0x869fde40ae42de70L, 0x2b8d995a2dfbac20L, 0x32391cf98dcc6fe7L, 0xd5ef6a4b2900cbb5L, 0x0f6dc088c163aba4L, 0x5f072870f2394255L, 0x0915668ce75ca5bfL, 0xee97505819af2f8aL, 0x0e0aaeb44813209bL, 0x1aa3e857d2a250f2L, 0x8fc5f60afad20c9eL, 0x3be2b0549452d2e8L, 0x7ec70fe0db81c3a3L, 0x97941a6b3d611110L, 0x0ad410b1b1a0a1edL, 0x24574ac5004bd459L, 0x46cd9e7cb86a283dL, 0xae3164713f141638L, 0x36a3e58636929c8dL, 0x5db41ba175a678a2L, 0x54052dfa35a50c8aL, 0xf24bd17fad5e7174L, 0xc5c51f1cbc2f8a21L, 0x34b1ca5c1769f076L, 0xbb5924f5674d6712L, 0x9309c6aa3701f4c8L, 0x1885c9eedd107655L, 0x55f4a50aeda95998L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo1024StarStar(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo1024StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo1024StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo1024StarStar(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo1024StarStar(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo1024StarStar(SEED)); } }
3,075
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XorShift1024StarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 16; /* * Data from running the executable compiled from the author's C code: * http://xorshift.di.unimi.it/xorshift1024star.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; private static final long[] EXPECTED_SEQUENCE = { 0xd85e9fc0855614cdL, 0xaf4965c9c1ac6a3dL, 0x067da398791111d8L, 0x2771c41db58d7644L, 0xf71a471e1ac2b03eL, 0x953449ae275f7409L, 0x8aa570c72de0af5eL, 0xae59db2acdae32beL, 0x3d46f316b8f97301L, 0x72dc8399b7a70957L, 0xf5624d788b3b6f4eL, 0xb7a79275f6c0e7b1L, 0xf79354208377d498L, 0x0e5d2f2ac2b4f28fL, 0x0f8f57edc8aa802fL, 0x5e918ea72ece0c36L, 0xeeb8dbdb00ac7a5aL, 0xf16f88dfef0d6047L, 0x1244c29e0e0d8d2dL, 0xaa94f1cc42691eb7L, 0xd06425dd329e5de5L, 0x968b1c2e016f159cL, 0x6aadff7055065295L, 0x3bce2efcb0d00876L, 0xb28d5b69ad8fb719L, 0x1e4040c451376920L, 0x6b0801a8a00de7d7L, 0x891ba2cbe2a4675bL, 0x6355008481852527L, 0x7a47bcd9960126f3L, 0x07f72fcd4ebe3580L, 0x4658b29c126840ccL, 0xdc7b36d3037c7539L, 0x9e30aab0410122e8L, 0x7215126e0fce932aL, 0xda63f12a489fc8deL, 0x769997671b2a0158L, 0xfa9cd84e0ffc174dL, 0x34df1cd959dca211L, 0xccea41a33ec1f763L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x65d54201f5acfddeL, 0x6fde36eb1f6a0de8L, 0x4bb549d952f83a9eL, 0x8a450adce576715aL, 0xb031eca9597292adL, 0xf37e0e06b835d614L, 0x26bfeba6b6a6066aL, 0xd3e5bc247e11b3e9L, 0x17efb0eaa881e128L, 0x17b53d5adf9c0c6cL, 0xcf40e97397cf3b90L, 0xaa9c9cc35ba0f5c0L, 0x531285c99a04b28aL, 0x868bac37b6520ea0L, 0x8532f89936f0c04dL, 0x46d686a34dafce8cL, 0xca15a5750af797b7L, 0x83cc94eb061c67a7L, 0xea90f3d41c3da1f3L, 0x18b74852ce1150daL, 0x193e6b8e68eea0f9L, 0x6ac72b7aa768f620L, 0xf302fae8b5f1705cL, 0x7cfbedcfa8626240L, 0x46a5a5fb00c3ee85L, 0xde6d0648b997303bL, 0x56ae91e7655900bbL, 0xab5031aa9f399904L, 0x8a97889ade2a276fL, 0x1b9faa106ffce5d3L, 0xab648350c8f58ecbL, 0x713bfb795f4397b7L, 0x2935361aa09ab73cL, 0xb5b425a0382d3f4cL, 0x8253cb35cdc787b1L, 0x9bad73436171212dL, 0x1d9c3fd0fcf525b7L, 0x5c0b302408af77ecL, 0x8e92e2d9a37b8481L, 0x8f3c06c39295f749L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XorShift1024Star(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XorShift1024Star(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XorShift1024Star.class, SEED_SIZE); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XorShift1024Star(SEED)); } }
3,076
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo256PlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro256plus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, }; private static final long[] EXPECTED_SEQUENCE = { 0x1a0e1903ef150886L, 0x08b605f47abc5d75L, 0xd82176096ac9be31L, 0x8fbf2af9b4fa5405L, 0x9ab074b448171964L, 0xfd68cc83ab4360aaL, 0xf431f7c0c8dc6f2bL, 0xc04430be08212638L, 0xc1ad670648f1da03L, 0x3eb70d38796ba24aL, 0x0e474d0598251ed2L, 0xf9b6b3b56482566bL, 0x3d11e529ae07a7c8L, 0x3b195f84f4db17e7L, 0x09d62e817b8223e2L, 0x89dc4db9cd625509L, 0x52e04793fe977846L, 0xc052428d6d7d17cdL, 0x6fd6f8da306b10efL, 0x64a7996ba5cc80aaL, 0x03abf59b95a1ef20L, 0xc5a82fc3cfb50234L, 0x0d401229eabb2d39L, 0xb537b249f70bd18aL, 0x1af1b703753fcf4dL, 0xb84648c1945d9ccbL, 0x1d321bea673e1f66L, 0x93d4445b268f305fL, 0xc046cfa36d89a312L, 0x8cc2d55bbf778790L, 0x1d668b0a3d329cc7L, 0x81b6d533dfcf82deL, 0x9ca1c49a18537b16L, 0x68e55c4054e0cb72L, 0x06ed1956cb69afc6L, 0x4871e696449da910L, 0xcfbd7a145066d46eL, 0x10131cb15004b62dL, 0x489c91a322bca3b6L, 0x8ec95fa9bef73e66L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x894cd8014fa285abL, 0x9737ec9aba91e4b6L, 0xf53b956d74db413aL, 0x6fb0350e20edef6bL, 0x1babe425f938088fL, 0x04f33708a2103773L, 0x03a3f59f511629dfL, 0x9d7323fd9cc8f542L, 0x8df0f8083323117bL, 0x9097a2cc69730c34L, 0x54e01393f7e1c5f6L, 0x14971cb42dce9e33L, 0x6ee4f7da32d287feL, 0x36124f300901b735L, 0x71726514f0341ccaL, 0xbdd6ff5845590a93L, 0x75982d4223903b23L, 0x75e88dbec205937cL, 0x82fa1ef5ed2d3ff5L, 0x49983b880a0758b8L, 0x8d3d74acd90595ccL, 0x1176ea450c32b01fL, 0xfddac8dca767aee0L, 0xbd8226c3f021dcfdL, 0xf95c1aead608a5f9L, 0xc7bd37c9a128d4f2L, 0x8abc94eb440371ceL, 0x4f86410df47f6928L, 0xd2d3479afe5730feL, 0xa7e02f6550aa6668L, 0x5f8b8630e9f5814eL, 0xe8c605350467cdccL, 0xecc91d6be68b5d11L, 0xbe9382f9d9e9e205L, 0xb512c7b80ca731f1L, 0x5125f56b47a89007L, 0x6d98bfd64342222bL, 0x7244f91ff7efc522L, 0xbcf26439fef5555fL, 0xce657c86d81455ceL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x1cf7608ee3f54c71L, 0xd9ec5faa2133c458L, 0xee0c3c44ac9a4571L, 0x8e6dd93270d03b83L, 0x44268688ca3c44b2L, 0x07aefb00d1cd925fL, 0x3733c3fe9831692dL, 0x2d2c5df21bfd31b2L, 0x1253a79c879276c2L, 0xf862b65e4bcb6bcfL, 0xf6dbb01651a1b8b5L, 0x68476e3a6b9bf559L, 0xb135e7151bafae3eL, 0x6e905be176bb02bfL, 0xa5ee1972ed7e090cL, 0x2cad1fadf7be52d6L, 0x1a34866325f9ae69L, 0xec4f680ff02209c1L, 0x4f01371b2db5a1c3L, 0x566fb6f85561d4d0L, 0x75fec1eb21866b41L, 0xfaff7b899909d846L, 0xe9e1bc7229c28446L, 0x63389a9af17857c5L, 0x07233b15e9f42541L, 0x46e262e3ba5c337cL, 0xfac8e73ae594bbd9L, 0x990562a1cbaf25d8L, 0xd99269c3ac01c7a0L, 0x20c249266bba8447L, 0x947bf8bc4e0e8deaL, 0xbd69e3c43fa8582aL, 0x7fb6e0e0d918bba6L, 0x95633e090da85696L, 0x1529c0b55ede291bL, 0x9034b247848dc3beL, 0x6422cc4a32efeb31L, 0x6334a19977a2fca6L, 0x016a2c7af8cf0eefL, 0x49dab6a0dc871a1cL, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo256Plus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo256Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo256Plus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo256Plus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo256Plus rng1 = new XoShiRo256Plus(SEED); final XoShiRo256Plus rng2 = new XoShiRo256Plus(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo256Plus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo256Plus(SEED)); } }
3,077
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XorShift1024StarPhiTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XorShift1024StarPhiTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 16; /* * Data from running the executable compiled from the author's C code: * http://xorshift.di.unimi.it/xorshift1024star.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; private static final long[] EXPECTED_SEQUENCE = { 0xc9351be6ae9af4bbL, 0x2696a1a51e3040cbL, 0xdcbbc38b838b4be8L, 0xc989eee03351a25cL, 0xc4ad829b653ada72L, 0x1cff4000cc0118dfL, 0x988f3aaf7bfb2852L, 0x3a621d4d5fb27bf2L, 0x0153d81cf33ff4a7L, 0x8a1b5adb974750c1L, 0x182799e238df6de2L, 0x92d9bda951cd6377L, 0x601f077d2a659728L, 0x90536cc64ad5bc49L, 0x5d99d9e84e3d7fa9L, 0xfc66f4610240613aL, 0x0ff67da640cdd6b6L, 0x973c7a6afbb41751L, 0x5089cb5236ac1b5bL, 0x7ed6edc1e4d7e261L, 0x3e37630df0c00b63L, 0x49ec234a0d03bcc4L, 0x2bcbe2fa4b80fa33L, 0xbaafc47b960baefaL, 0x1855fa47be98c84fL, 0x8d59cb57e34a73e0L, 0x256b15bb001bf641L, 0x28ad378895f5615dL, 0x865547335ba2a571L, 0xfefe4c356e154585L, 0xeb87f7a74e076680L, 0x990d2f5d1e60b914L, 0x3bf0f6864688af2fL, 0x8c6304df9b945d58L, 0x63bc09c335b63666L, 0x1038139f53734ad2L, 0xf41b58faf5680868L, 0xa50ba830813c163bL, 0x7dc1ca503ae39817L, 0xea3d0f2f37f5ce95L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x07afdab6d38bddd2L, 0x7bb8f1495d6aaa58L, 0x00f583e9eb57bf12L, 0x694e972ba626f7b6L, 0x0f7017b991b531dbL, 0x702fc8e6791b530cL, 0xf62322eab2db2526L, 0xe6bdc2cffeeae3ffL, 0xa0cb7cb78c2e3918L, 0x53fa4d1ee744ee74L, 0x5f0bcdebaf4b4af0L, 0xec23017af16e9040L, 0x2d1119530d4f4e06L, 0x75b721c9942eea60L, 0x6aab166dbc9d553bL, 0xcfa59b433d647154L, 0x687a4f5aa4bfd161L, 0xcc954692756486f1L, 0xcfa57a42ae5e8285L, 0xe290ecfae5d74436L, 0x0adb47de60db796fL, 0xee4e161668406ee0L, 0xe7f5beb82ec63004L, 0x5ee1ed818be7d7c0L, 0xb1aa1517d646c3c3L, 0x31ab29451adf8b7dL, 0x2612a880abf60efdL, 0xa7679f88450d8d9cL, 0xee3b07f323a85a69L, 0xac7e0039bb81e9a5L, 0x5b454710e237ab6dL, 0xdd6c4ac09653d161L, 0xc03e09a39f5e8c24L, 0x4a8352f76f7c3e94L, 0xebeb6e56c67fc377L, 0x3726d52cb3cda75bL, 0x3f6c00e368b97361L, 0xc49b806ba04c8ef4L, 0xf396608b186fdf27L, 0xe0c7f13ba319779fL, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XorShift1024StarPhi(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XorShift1024StarPhi(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XorShift1024StarPhi.class, SEED_SIZE); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XorShift1024StarPhi(SEED)); } }
3,078
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo128PlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo128PlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 2; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro128plus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, }; private static final long[] EXPECTED_SEQUENCE = { 0xa6d5fa73b796e607L, 0xd419031a381fea2eL, 0x28938b88b4972f52L, 0x032793a0d51c1a27L, 0x50001cd69cc5b006L, 0x44bbf571167cb7f0L, 0x172f6a2f093b2befL, 0xe642c831f1e4f7bfL, 0xcec4e4b5d448032aL, 0xc0164992807cbd59L, 0xb96ff06c68515410L, 0x5288e0312a0aae72L, 0x79a891c387d3be2eL, 0x6c52f6f710db553bL, 0x2ce6f6b1946862b3L, 0x87eb1e1b24b47f11L, 0x9f7c3511c5f23bcfL, 0x3254897533dcd1abL, 0x89d56ad217fbd1adL, 0x70f6b269f815f6e6L, 0xe8ee60efadfdb8c4L, 0x09286db69fdd232bL, 0xf440882651fc19e8L, 0x6356fea018cc26cdL, 0xf692282b43fcb0c2L, 0xef3f084929119babL, 0x355efbf5bedeb114L, 0x6cf5089c2acc96ddL, 0x819c19e480f0bfd1L, 0x414d12ff4082e261L, 0xc9a33a52545dd374L, 0x4675247e6fe89b3cL, 0x069f2e55cea155baL, 0x1e8d1dcf349746b8L, 0xdf32e487bdd74523L, 0xa544710cae2ad7cdL, 0xf5ac505e74fe049dL, 0xf039e289da4cdf7eL, 0x0a6fbebe9122529cL, 0x880c51e0915031a3L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xb715ad9cb572030eL, 0x10634f817a8f69b1L, 0xe871b367a8f9c567L, 0x3096f4ceb23198cdL, 0xf5b09c8734d26da9L, 0x58ba83f779a2549cL, 0xb6c54c8ea9fc672bL, 0x87bb9766ff20834dL, 0x2686d4a93b9bb8a7L, 0xababdb798931fbfbL, 0xc9ce83b35259eb39L, 0xe2314c9488d44131L, 0x5841497fb6fe3a62L, 0x00ecd78f2eba1e81L, 0x4aed6d184b3ada35L, 0xb33681cb7e39c9d0L, 0x911498e07cb3d3ceL, 0xea92a7dfb98aa971L, 0x5db49f2f3f22321cL, 0x4982bae495d875f4L, 0xa436167ccfb3a982L, 0x646b0fe680176bb2L, 0x8312610f7e93a41fL, 0xa67a4d13d12cb264L, 0xf22330689f003fa8L, 0xfa0d7f3712db37beL, 0x409b34496c4a847cL, 0x9ac1b246a47e2a17L, 0x6006078c0c743c74L, 0x457ef921811029ecL, 0xd6d9f58851583575L, 0xabbdd4ac3c49239bL, 0xc07444095ef29743L, 0x75a2eb7c74f95d6eL, 0xd0ead6726a91ada9L, 0x9749e96908129418L, 0x83ae5b12eb343545L, 0x10828fde12decab5L, 0x6952d51f130505b7L, 0x9b6bd81fbe064f54L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0xf569a89f3ee6fa3dL, 0x3c61867cbcc208a8L, 0x95a83b710aa1a57fL, 0xed5c658383559407L, 0xbb70b6959a82f3b0L, 0x31eab244213fe7beL, 0xe14bb9d50b6b026fL, 0x80716d04b81d5aaaL, 0x5451ede574559e11L, 0x1aa1d87fdfb78d52L, 0x38e4a2c33f97870fL, 0xf88a98d8376a95b5L, 0x5b88ec173f131549L, 0x642af091aab9643aL, 0x92edb9171a703919L, 0xf65bd2ac6b1efd62L, 0xe10b1da6e5b2efafL, 0xf2abec22c6c4170dL, 0xa5b853b7dee402c1L, 0xe45d2ec5e488cac3L, 0x64f126a2065224d6L, 0x5e4f64edde60e852L, 0xc8dc8819696ef333L, 0x1a9acfaa4a10f22bL, 0xd75be7f8487d1c52L, 0x7759ec0ea4b8e698L, 0xc36b934a78c463e2L, 0x2c7257c4228f7948L, 0x0651b8c869027343L, 0xe66b122fec5ca8a1L, 0xe3f8ad0bcf74642dL, 0x08f46e481eec937aL, 0xccd1c4419af68187L, 0x93396af913995faeL, 0x95ee2988b39a3534L, 0x7da239ae0dd79948L, 0xa0fdcf90f2e3ab22L, 0xb2e20b2ce7a83f7aL, 0xd1346542947f8b0dL, 0xa95894552bbb2460L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo128Plus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo128Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo128Plus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo128Plus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoRoShiRo128Plus rng1 = new XoRoShiRo128Plus(SEED); final XoRoShiRo128Plus rng2 = new XoRoShiRo128Plus(SEED[0], SEED[1]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo128Plus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo128Plus(SEED)); } }
3,079
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LongProviderTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.util.NumberFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; /** * The tests the caching of calls to {@link LongProvider#nextLong()} are used as * the source for {@link LongProvider#nextInt()} and * {@link LongProvider#nextBoolean()}. */ class LongProviderTest { /** * A simple class to return a fixed value as the source for * {@link LongProvider#next()}. */ static final class FixedLongProvider extends LongProvider { /** The value. */ private long value; /** * @param value the value */ FixedLongProvider(long value) { this.value = value; } @Override public long next() { return value; } } /** * A simple class to flip the bits in a number as the source for * {@link LongProvider#next()}. */ static final class FlipLongProvider extends LongProvider { /** The value. */ private long value; /** * @param value the value */ FlipLongProvider(long value) { // Flip the bits so the first call to next() returns to the same state this.value = ~value; } @Override public long next() { // Flip the bits value = ~value; return value; } } /** * This test ensures that the call to {@link LongProvider#nextInt()} returns the * lower and then upper 32-bits from {@link LongProvider#nextLong()}. */ @Test void testNextInt() { final int max = 5; for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { // Pack into lower then upper bits final long value = NumberFactory.makeLong(j, i); final LongProvider provider = new FixedLongProvider(value); Assertions.assertEquals(i, provider.nextInt(), "1st call not the upper 32-bits"); Assertions.assertEquals(j, provider.nextInt(), "2nd call not the lower 32-bits"); Assertions.assertEquals(i, provider.nextInt(), "3rd call not the upper 32-bits"); Assertions.assertEquals(j, provider.nextInt(), "4th call not the lower 32-bits"); } } } /** * This test ensures that the call to {@link LongProvider#nextBoolean()} returns * each of the bits from a call to {@link LongProvider#nextLong()}. * * <p>The order should be from the least-significant bit. */ @Test void testNextBoolean() { for (int i = 0; i < Long.SIZE; i++) { // Set only a single bit in the source final long value = 1L << i; final LongProvider provider = new FlipLongProvider(value); // Test the result for a single pass over the long for (int j = 0; j < Long.SIZE; j++) { final boolean expected = i == j; final int index = j; Assertions.assertEquals(expected, provider.nextBoolean(), () -> "Pass 1, bit " + index); } // The second pass should use the opposite bits for (int j = 0; j < Long.SIZE; j++) { final boolean expected = i != j; final int index = j; Assertions.assertEquals(expected, provider.nextBoolean(), () -> "Pass 2, bit " + index); } } } @ParameterizedTest @CsvSource({ // OK "10, 0, 10", "10, 5, 5", "10, 9, 1", // Allowed edge cases "0, 0, 0", "10, 10, 0", // Bad "10, 0, 11", "10, 10, 1", "10, 10, 2147483647", "10, 0, -1", "10, 5, -1", }) void testNextBytesIndices(int size, int start, int len) { final FixedLongProvider rng = new FixedLongProvider(999); final byte[] bytes = new byte[size]; // Be consistent with System.arraycopy try { System.arraycopy(bytes, start, bytes, start, len); } catch (IndexOutOfBoundsException ex) { // nextBytes should throw under the same conditions. // Note: This is not ArrayIndexOutOfBoundException to be // future compatible with Objects.checkFromIndexSize. Assertions.assertThrows(IndexOutOfBoundsException.class, () -> rng.nextBytes(bytes, start, len)); return; } rng.nextBytes(bytes, start, len); } }
3,080
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoRoShiRo1024PlusPlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoRoShiRo1024PlusPlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 16; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoroshiro1024plusplus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; private static final long[] EXPECTED_SEQUENCE = { 0x3b09ad2dbf0fac01L, 0xc94a7fe723f359b9L, 0xe6e1dd32bf791bfcL, 0xb7544ad223abd2d8L, 0xf30327faf9c86e1bL, 0xe6e5f8e0c3ee32eeL, 0xd0517ea6f0a6dff8L, 0xe7ab840562f624f9L, 0x46974064298e33c5L, 0xb1c7d3a0a763d025L, 0x516b8571e4870ed4L, 0xc6fb23b5d1e49b84L, 0x09aa6d82bcab4254L, 0x23719002bbe8f966L, 0x0d67afdb43a5dca4L, 0x297327cb4057c221L, 0x4b2af2a3dba0da80L, 0x9eda3fa5e098414aL, 0x6806ec1363b1e1b9L, 0x6efe75a6813c59c6L, 0x335baf867960e5fbL, 0x9f4415ecc8830b7aL, 0xe1c6456883daafbdL, 0x50d175bab6ac665cL, 0x8122d5175b11d1f5L, 0xb3671ac101492a4bL, 0x658bac8aa044c300L, 0xa652105130589a28L, 0xf49f0307772db260L, 0x9d18a1bd5200fcbcL, 0x9cd41d5db25d6593L, 0xe34ecdb564717debL, 0x8affe46f54d83679L, 0x67639a77a4199b87L, 0xa0d788390eaa4b68L, 0x67f84eff59949883L, 0xc374a0949d9e7c44L, 0xdb3251d6eb8cfc68L, 0x130ac6799fd3b059L, 0x72258b39becdf313L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x12907f578f2a163cL, 0xc5034828888b0a7dL, 0xb2cc81c062eb12c9L, 0x7cb14f46bc6c47b1L, 0x5f5641c733b6b7c9L, 0x36eed13f65e22019L, 0xa6e9a258ec194d05L, 0xf5a6cf90bfed11f1L, 0xe37a7b9b9e8b429eL, 0xa30129f12e7ca354L, 0x6c38ec46d7707a41L, 0xfd8e399d18e8fccaL, 0xf93f723ea2ba8b80L, 0xef4b6593ecb01139L, 0x774015239c7fd6adL, 0xc868b4129532870aL, 0x7a77dc9ea4cebdddL, 0x217dd8bd12b281e1L, 0x18dbc96aa091bf40L, 0xfbb8397be69034d7L, 0xfb686ead6dcadfd7L, 0x25a17990b448429dL, 0x7476a4cef88b1766L, 0xd6b4eccc2b574014L, 0x89bc48ea54f24968L, 0x9a779e116dd3ac15L, 0xa10f0df74bd66f83L, 0xfbadde536a6ed6b6L, 0xa9b98fcc285f5920L, 0x07d8a0b3fb1c89baL, 0x413de5a03081fac0L, 0xfa12ec0e83efcdc9L, 0x84280bbe5242a8c2L, 0xae6da4ff89c29e50L, 0xe611cd4047f50f31L, 0x972cdee05fc6c463L, 0x69679b42d792ec82L, 0xfb610ac33ca4efd3L, 0xcb78db0ccb62e334L, 0xd7e1ca3dc8db39c4L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x9144383654573d60L, 0x6223e80cec1a7284L, 0xc24ec8b470b103e8L, 0xeeae61f7a3220c62L, 0xafebedd0e1fa737eL, 0x860201c60edbaf3eL, 0x4b0579f618188e95L, 0x767032c20a4553b5L, 0xcf6ac990528dd14fL, 0x047eed71a8bfdd05L, 0xbc3afb7e47c198ccL, 0x10ee99db24684f0bL, 0xe28af1c36d0b80c7L, 0xa648390a61f79e01L, 0x19ed67bc53b7604aL, 0x22454886633affaeL, 0x925cc38296b612f5L, 0x7490feb237c248bcL, 0xea57d10bd26d79adL, 0x31b5fb910df0d3c6L, 0x49ce7616a038a946L, 0x75bff93a6c5a2bc3L, 0xaf75a56b81a4d784L, 0x41b4b1aa4e59172aL, 0xb4ec816fd4484d88L, 0xa8ba3b236a03520aL, 0x88f8c9f2776e8182L, 0x8b4096cdb6668ff8L, 0xaf90a119ab52bab5L, 0x516ebfdce73f105cL, 0x7f04868cc1c439a3L, 0x8cd3eae52205020fL, 0x505a05e2eaf6af10L, 0x9f16121875db8153L, 0x92032da26a981a00L, 0x1486140057b674b1L, 0x335ac573af5d92a3L, 0x72a601de5a4547f9L, 0xb3e8d3309e3f1327L, 0x21a6aae8ec8b1966L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoRoShiRo1024PlusPlus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoRoShiRo1024PlusPlus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo1024PlusPlus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo1024PlusPlus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoRoShiRo1024PlusPlus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoRoShiRo1024PlusPlus(SEED)); } }
3,081
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/AbstractLXMTest.java
/* * 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.rng.core.source64; import java.math.BigInteger; import java.util.SplittableRandom; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Function; import java.util.function.LongSupplier; import java.util.function.Supplier; import java.util.function.UnaryOperator; import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.apache.commons.rng.core.util.NumberFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Base test for LXM generators. These generators all use the same basic steps: * <pre> * s = state of LCG * t = state of XBG * * generate(): * z <- mix(combine(w upper bits of s, w upper bits of t)) * s <- LCG state update * t <- XBG state update * </pre> * * <p>This base class provides a composite generator of an LCG and XBG * both using w = 64-bits. Tests for a RNG implementation must define * a factory for constructing a reference composite LXM generator. Implementations * for the 64-bit and 128-bit LCG used in the LXM family are provided. * Implementations for the XBG are provided based on version in the Commons RNG core library. * * <p>It is assumed the XBG generator is a {@link LongJumpableUniformRandomProvider}. * The composite generator requires the sub-generators to provide a jump and long jump * equivalent operation. This is performed by advancing/rewinding the LCG and XBG the same number * of cycles. In practice this is performed using: * <ul> * <li>A large jump of the XBG that wraps the state of the LCG. * <li>Leaving the XBG state unchanged and advancing the LCG. This effectively * rewinds the state of the LXM by the period of the XBG multiplied by the number of * cycles advanced by the LCG. * </ul> * * <p>The paper by Steele and Vigna suggest advancing the LCG to take advantage of the fast * update step of the LCG. This is particularly true for the 64-bit LCG variants. If * the LCG and XBG sub-generators support jump/longJump then the composite can then be * used to test arbitrary combinations of calls to: generate the next long value; and jump * operations. This is not possible using the reference implementations of the LXM family * in JDK 17 which do not implement jumping (instead providing a split operation to create * new RNGs). * * <p>The test assumes the following conditions: * <ul> * <li>The seed length equals the state size of the LCG and XBG generators. * <li>The LXM generator seed is the seed for the LCG appended with the seed for the XBG. * <li>The LCG seed in order is [additive parameter, initial state]. The additive parameter * must be odd for a maximum period LCG and the test asserts the final bit of the add parameter * from the seed is effectively ignored as it is forced to be odd in the constructor. * <li>If the XBG seed is all zeros, the LXM generator is partially functional. It will output * non-zero values but the sequence may be statistically weak and the period is that of the LCG. * This cannot be practically tested but non-zero output is verified for an all zero seed. * </ul> * * <p>Note: The seed order prioritises the additive parameter first. This parameter can be used to * create independent streams of generators. It allows constructing a generator from a single long * value, where the rest of the state is filled with a procedure generating non-zero values, * which will be independent from a second generator constructed with a different single additive * parameter. The difference must be in the high 63-bits of the seed value for 64-bit LCG variants. * A suitable filling procedure from an initial seed value is provided in the Commons RNG Simple * module. * * <p>The class uses a per-class lifecycle. This allows abstract methods to be overridden * in implementing classes to control test behaviour. */ @TestInstance(Lifecycle.PER_CLASS) abstract class AbstractLXMTest { /** * A function to mix two long values. * Implements the combine and mix functions of the LXM generator. */ interface Mix { /** * Mix the values. * * @param a Value a * @param b Value b * @return the result */ long apply(long a, long b); } /** * A jumpable sub-generator. This can be a linear congruential generator (LCG) * or xor-based generator (XBG) when used in the LXM family. * * <p>For simplicity the steps of obtaining the upper bits of the state * and updating the state are combined to a single operation. */ interface SubGen { /** * Return the upper 64-bits of the current state and update the state. * * @return the upper 64-bits of the old state */ long stateAndUpdate(); /** * Create a copy and then advance the state in a single jump; the copy is returned. * * @return the copy */ SubGen copyAndJump(); /** * Create a copy and then advance the state in a single long jump; the copy is returned. * * @return the copy */ SubGen copyAndLongJump(); } /** * Mix the sum using the star star function. * * @param a Value a * @param b Value b * @return the result */ static long mixStarStar(long a, long b) { return Long.rotateLeft((a + b) * 5, 7) * 9; } /** * Mix the sum using the lea64 function. * * @param a Value a * @param b Value b * @return the result */ static long mixLea64(long a, long b) { return LXMSupport.lea64(a + b); } /** * A 64-bit linear congruential generator. */ static class LCG64 implements SubGen { /** Multiplier. */ private static final long M = LXMSupport.M64; /** Additive parameter (must be odd). */ private final long a; /** State. */ private long s; /** Power of 2 for the jump. */ private final int jumpPower; /** Power of 2 for the long jump. */ private final int longJumpPower; /** * Create an instance with a jump of 1 cycle and long jump of 2^32 cycles. * * @param a Additive parameter (set to odd) * @param s State */ LCG64(long a, long s) { this(a, s, 0, 32); } /** * @param a Additive parameter (set to odd) * @param s State * @param jumpPower Jump size as a power of 2 * @param longJumpPower Long jump size as a power of 2 */ LCG64(long a, long s, int jumpPower, int longJumpPower) { this.a = a | 1; this.s = s; this.jumpPower = jumpPower; this.longJumpPower = longJumpPower; } @Override public long stateAndUpdate() { final long s0 = s; s = M * s + a; return s0; } @Override public SubGen copyAndJump() { final SubGen copy = new LCG64(a, s, jumpPower, longJumpPower); s = LXMSupportTest.lcgAdvancePow2(s, M, a, jumpPower); return copy; } @Override public SubGen copyAndLongJump() { final SubGen copy = new LCG64(a, s, jumpPower, longJumpPower); s = LXMSupportTest.lcgAdvancePow2(s, M, a, longJumpPower); return copy; } } /** * A 128-bit linear congruential generator. */ static class LCG128 implements SubGen { /** Low half of 128-bit multiplier. The upper half is 1. */ private static final long ML = LXMSupport.M128L; /** High bits of additive parameter. */ private final long ah; /** Low bits of additive parameter (must be odd). */ private final long al; /** High bits of state. */ private long sh; /** Low bits of state. */ private long sl; /** Power of 2 for the jump. */ private final int jumpPower; /** Power of 2 for the long jump. */ private final int longJumpPower; /** * Create an instance with a jump of 1 cycle and long jump of 2^64 cycles. * * @param ah High bits of additive parameter * @param al Low bits of additive parameter (set to odd) * @param sh High bits of the 128-bit state * @param sl Low bits of the 128-bit state */ LCG128(long ah, long al, long sh, long sl) { this(ah, al, sh, sl, 0, 64); } /** * @param ah High bits of additive parameter * @param al Low bits of additive parameter (set to odd) * @param sh High bits of the 128-bit state * @param sl Low bits of the 128-bit state * @param jumpPower Jump size as a power of 2 * @param longJumpPower Long jump size as a power of 2 */ LCG128(long ah, long al, long sh, long sl, int jumpPower, int longJumpPower) { this.ah = ah; this.al = al | 1; this.sh = sh; this.sl = sl; this.jumpPower = jumpPower; this.longJumpPower = longJumpPower; } @Override public long stateAndUpdate() { final long s0 = sh; // The LCG is, in effect, "s = m * s + a" where m = ((1LL << 64) + ML) final long u = ML * sl; // High half sh = (ML * sh) + LXMSupport.unsignedMultiplyHigh(ML, sl) + sl + ah; // Low half sl = u + al; // Carry propagation if (Long.compareUnsigned(sl, u) < 0) { ++sh; } return s0; } @Override public SubGen copyAndJump() { final SubGen copy = new LCG128(ah, al, sh, sl, jumpPower, longJumpPower); final long nsl = LXMSupportTest.lcgAdvancePow2(sl, ML, al, jumpPower); sh = LXMSupportTest.lcgAdvancePow2High(sh, sl, 1, ML, ah, al, jumpPower); sl = nsl; return copy; } @Override public SubGen copyAndLongJump() { final SubGen copy = new LCG128(ah, al, sh, sl, jumpPower, longJumpPower); final long nsl = LXMSupportTest.lcgAdvancePow2(sl, ML, al, longJumpPower); sh = LXMSupportTest.lcgAdvancePow2High(sh, sl, 1, ML, ah, al, longJumpPower); sl = nsl; return copy; } } /** * A xor-based generator using XoRoShiRo128. * * <p>The generator can be configured to perform jumps or simply return a copy. * The LXM generator would jump by advancing only the LCG state. */ static class XBGXoRoShiRo128 extends AbstractXoRoShiRo128 implements SubGen { /** Jump flag. */ private final boolean jump; /** * Create an instance with jumping enabled. * * @param seed seed */ XBGXoRoShiRo128(long[] seed) { super(seed); jump = true; } /** * @param seed0 seed element 0 * @param seed1 seed element 1 * @param jump Set to true to perform jumping, otherwise a jump returns a copy */ XBGXoRoShiRo128(long seed0, long seed1, boolean jump) { super(seed0, seed1); this.jump = jump; } /** * Copy constructor. * * @param source the source to copy */ XBGXoRoShiRo128(XBGXoRoShiRo128 source) { // There is no super-class copy constructor so just construct // from the RNG state. // Warning: // This will not copy the cached state from 'source' // which matters if tests use nextInt or nextBoolean. // Currently tests only target the long output. this(source.state0, source.state1, source.jump); } @Override public long stateAndUpdate() { final long s0 = state0; next(); return s0; } @Override public SubGen copyAndJump() { return (SubGen) (jump ? super.jump() : copy()); } @Override public SubGen copyAndLongJump() { return (SubGen) (jump ? super.longJump() : copy()); } @Override protected long nextOutput() { // Not used return 0; } @Override protected XBGXoRoShiRo128 copy() { return new XBGXoRoShiRo128(this); } } /** * A xor-based generator using XoShiRo256. * * <p>The generator can be configured to perform jumps or simply return a copy. * The LXM generator would jump by advancing only the LCG state. */ static class XBGXoShiRo256 extends AbstractXoShiRo256 implements SubGen { /** Jump flag. */ private final boolean jump; /** * Create an instance with jumping enabled. * * @param seed seed */ XBGXoShiRo256(long[] seed) { super(seed); jump = true; } /** * @param seed0 seed element 0 * @param seed1 seed element 1 * @param seed2 seed element 2 * @param seed3 seed element 3 * @param jump Set to true to perform jumping, otherwise a jump returns a copy */ XBGXoShiRo256(long seed0, long seed1, long seed2, long seed3, boolean jump) { super(seed0, seed1, seed2, seed3); this.jump = jump; } /** * Copy constructor. * * @param source the source to copy */ XBGXoShiRo256(XBGXoShiRo256 source) { super(source); jump = source.jump; } @Override public long stateAndUpdate() { final long s0 = state0; next(); return s0; } @Override public SubGen copyAndJump() { return (SubGen) (jump ? super.jump() : copy()); } @Override public SubGen copyAndLongJump() { return (SubGen) (jump ? super.longJump() : copy()); } @Override protected long nextOutput() { // Not used return 0; } @Override protected XBGXoShiRo256 copy() { return new XBGXoShiRo256(this); } } /** * A xor-based generator using XoRoShiRo1024. * * <p>The generator can be configured to perform jumps or simply return a copy. * The LXM generator would jump by advancing only the LCG state. * * <p>Note: To avoid changes to the parent class this test class uses the save/restore * function to initialise the index. The rng state update can be managed by the * super-class and this class holds a reference to the most recent s0 value. */ static class XBGXoRoShiRo1024 extends AbstractXoRoShiRo1024 implements SubGen { /** Jump flag. */ private final boolean jump; /** The most recent s0 variable passed to {@link #transform(long, long)}. */ private long state0; /** * Create an instance with jumping enabled. * * @param seed 16-element seed */ XBGXoRoShiRo1024(long[] seed) { this(seed, true); } /** * @param seed 16-element seed * @param jump Set to true to perform jumping, otherwise a jump returns a copy */ XBGXoRoShiRo1024(long[] seed, boolean jump) { super(seed); this.jump = jump; // Ensure the first state returned corresponds to state[0]. // This requires setting: // index = state.length - 1 // To avoid changing the private visibility of super-class state variables // this is done using the save/restore function. It avoids any issues // with using reflection on the 'index' field but must be maintained // inline with the save/restore logic of the super-class. final byte[] s = super.getStateInternal(); final byte[][] c = splitStateInternal(s, 17 * Long.BYTES); final long[] tmp = NumberFactory.makeLongArray(c[0]); // Here: index = state.length - 1 tmp[16] = 15; c[0] = NumberFactory.makeByteArray(tmp); super.setStateInternal(composeStateInternal(c[0], c[1])); } /** * Copy constructor. * * @param source the source to copy */ XBGXoRoShiRo1024(XBGXoRoShiRo1024 source) { super(source); jump = source.jump; } @Override public long stateAndUpdate() { next(); return state0; } @Override public SubGen copyAndJump() { return (SubGen) (jump ? super.jump() : copy()); } @Override public SubGen copyAndLongJump() { return (SubGen) (jump ? super.longJump() : copy()); } @Override protected long transform(long s0, long s15) { this.state0 = s0; // No transformation required. return 0; } @Override protected XBGXoRoShiRo1024 copy() { return new XBGXoRoShiRo1024(this); } } /** * A composite LXM generator. Implements: * <pre> * s = state of LCG * t = state of XBG * * generate(): * z <- mix(combine(w upper bits of s, w upper bits of t)) * s <- LCG state update * t <- XBG state update * </pre> * <p>w is assumed to be 64-bits. */ static class LXMGenerator extends LongProvider implements LongJumpableUniformRandomProvider { /** Mix implementation. */ private final Mix mix; /** LCG implementation. */ private final SubGen lcg; /** XBG implementation. */ private final SubGen xbg; /** * Create a new instance. * The jump and long jump of the LCG are assumed to appropriately match those of the XBG. * This can be achieved by an XBG jump that wraps the period of the LCG; or advancing * the LCG and leaving the XBG state unchanged, effectively rewinding the LXM generator. * * @param mix Mix implementation * @param lcg LCG implementation * @param xbg XBG implementation */ LXMGenerator(Mix mix, SubGen lcg, SubGen xbg) { this.lcg = lcg; this.xbg = xbg; this.mix = mix; } @Override public long next() { return mix.apply(lcg.stateAndUpdate(), xbg.stateAndUpdate()); } @Override public LXMGenerator jump() { return new LXMGenerator(mix, lcg.copyAndJump(), xbg.copyAndJump()); } @Override public LXMGenerator longJump() { return new LXMGenerator(mix, lcg.copyAndLongJump(), xbg.copyAndLongJump()); } } /** * A factory for creating LXMGenerator objects. */ interface LXMGeneratorFactory { /** * Return the size of the LCG long seed array. * * @return the LCG seed size */ int lcgSeedSize(); /** * Return the size of the XBG long seed array. * * @return the XBG seed size */ int xbgSeedSize(); /** * Return the size of the long seed array. * The default implementation adds the size of the LCG and XBG seed. * * @return the seed size */ default int seedSize() { return lcgSeedSize() + xbgSeedSize(); } /** * Creates a new LXMGenerator. * * <p>Tests using the LXMGenerator assume the seed is a composite containing the * LCG seed and then the XBG seed. * * @param seed the seed * @return the generator */ LXMGenerator create(long[] seed); /** * Gets the mix implementation. This is used to test initial output of the generator. * The default implementation is {@link AbstractLXMTest#mixLea64(long, long)}. * * @return the mix */ default Mix getMix() { return AbstractLXMTest::mixLea64; } } /** * Test the LCG implementations. These tests should execute only once, and not * for each instance of the abstract outer class (i.e. the test is not {@code @Nested}). * * <p>Note: The LCG implementations are not present in the main RNG core package and * this test ensures an LCG update of: * <pre> * s = m * s + a * * s = state * m = multiplier * a = addition * </pre> * * <p>A test is made to ensure the LCG can perform jump and long jump operations using * small jumps that can be verified by an equal number of single state updates. */ static class LCGTest { /** 2^63. */ private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63); /** 65-bit multiplier for the 128-bit LCG. */ private static final BigInteger M = BigInteger.ONE.shiftLeft(64).add(toUnsignedBigInteger(LXMSupport.M128L)); /** 2^128. Used as the modulus for the 128-bit LCG. */ private static final BigInteger MOD = BigInteger.ONE.shiftLeft(128); /** A count {@code k} where a jump of {@code 2^k} will wrap the LCG state. */ private static final int NO_JUMP = -1; /** A count {@code k=2} for a jump of {@code 2^k}, or 4 cycles. */ private static final int JUMP = 2; /** A count {@code k=4} for a long jump of {@code 2^k}, or 16 cycles. */ private static final int LONG_JUMP = 4; @RepeatedTest(value = 10) void testLCG64DefaultJump() { final SplittableRandom rng = new SplittableRandom(); final long state = rng.nextLong(); final long add = rng.nextLong(); final SubGen lcg1 = new LCG64(add, state); final SubGen lcg2 = new LCG64(add, state, 0, 32); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } lcg1.copyAndJump(); lcg2.copyAndJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } lcg1.copyAndLongJump(); lcg2.copyAndLongJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); } } @RepeatedTest(value = 10) void testLCG64() { final SplittableRandom rng = new SplittableRandom(); final long state = rng.nextLong(); final long add = rng.nextLong(); long s = state; final long a = add | 1; final SubGen lcg = new LCG64(add, state, NO_JUMP, NO_JUMP); for (int j = 0; j < 10; j++) { Assertions.assertEquals(s, lcg.stateAndUpdate(), () -> String.format("seed %d,%d", state, add)); s = LXMSupport.M64 * s + a; } } @RepeatedTest(value = 10) void testLCG64Jump() { final SplittableRandom rng = new SplittableRandom(); final long state = rng.nextLong(); final long add = rng.nextLong(); final Supplier<String> msg = () -> String.format("seed %d,%d", state, add); long s = state; final long a = add | 1; final SubGen lcg = new LCG64(add, state, JUMP, LONG_JUMP); final SubGen copy1 = lcg.copyAndJump(); for (int j = 1 << JUMP; j-- != 0;) { Assertions.assertEquals(s, copy1.stateAndUpdate(), msg); s = LXMSupport.M64 * s + a; } Assertions.assertEquals(s, lcg.stateAndUpdate(), msg); s = LXMSupport.M64 * s + a; final SubGen copy2 = lcg.copyAndLongJump(); for (int j = 1 << LONG_JUMP; j-- != 0;) { Assertions.assertEquals(s, copy2.stateAndUpdate(), msg); s = LXMSupport.M64 * s + a; } Assertions.assertEquals(s, lcg.stateAndUpdate(), msg); } @RepeatedTest(value = 10) void testLCG128DefaultJump() { final SplittableRandom rng = new SplittableRandom(); final long stateh = rng.nextLong(); final long statel = rng.nextLong(); final long addh = rng.nextLong(); final long addl = rng.nextLong(); final SubGen lcg1 = new LCG128(addh, addl, stateh, statel); final SubGen lcg2 = new LCG128(addh, addl, stateh, statel, 0, 64); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d,%d,%d", stateh, statel, addh, addl)); } lcg1.copyAndJump(); lcg2.copyAndJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d,%d,%d", stateh, statel, addh, addl)); } lcg1.copyAndLongJump(); lcg2.copyAndLongJump(); for (int j = 0; j < 10; j++) { Assertions.assertEquals(lcg1.stateAndUpdate(), lcg2.stateAndUpdate(), () -> String.format("seed %d,%d,%d,%d", stateh, statel, addh, addl)); } } @RepeatedTest(value = 10) void testLCG128() { final SplittableRandom rng = new SplittableRandom(); final long stateh = rng.nextLong(); final long statel = rng.nextLong(); final long addh = rng.nextLong(); final long addl = rng.nextLong(); BigInteger s = toUnsignedBigInteger(stateh).shiftLeft(64).add(toUnsignedBigInteger(statel)); final BigInteger a = toUnsignedBigInteger(addh).shiftLeft(64).add(toUnsignedBigInteger(addl | 1)); final SubGen lcg = new LCG128(addh, addl, stateh, statel, NO_JUMP, NO_JUMP); for (int j = 0; j < 10; j++) { Assertions.assertEquals(s.shiftRight(64).longValue(), lcg.stateAndUpdate(), () -> String.format("seed %d,%d,%d,%d", stateh, statel, addh, addl)); s = M.multiply(s).add(a).mod(MOD); } } @RepeatedTest(value = 10) void testLCG128Jump() { final SplittableRandom rng = new SplittableRandom(); final long stateh = rng.nextLong(); final long statel = rng.nextLong(); final long addh = rng.nextLong(); final long addl = rng.nextLong(); final Supplier<String> msg = () -> String.format("seed %d,%d,%d,%d", stateh, statel, addh, addl); BigInteger s = toUnsignedBigInteger(stateh).shiftLeft(64).add(toUnsignedBigInteger(statel)); final BigInteger a = toUnsignedBigInteger(addh).shiftLeft(64).add(toUnsignedBigInteger(addl | 1)); final SubGen lcg = new LCG128(addh, addl, stateh, statel, JUMP, LONG_JUMP); final SubGen copy1 = lcg.copyAndJump(); for (int j = 1 << JUMP; j-- != 0;) { Assertions.assertEquals(s.shiftRight(64).longValue(), copy1.stateAndUpdate(), msg); s = M.multiply(s).add(a).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcg.stateAndUpdate(), msg); s = M.multiply(s).add(a).mod(MOD); final SubGen copy2 = lcg.copyAndLongJump(); for (int j = 1 << LONG_JUMP; j-- != 0;) { Assertions.assertEquals(s.shiftRight(64).longValue(), copy2.stateAndUpdate(), msg); s = M.multiply(s).add(a).mod(MOD); } Assertions.assertEquals(s.shiftRight(64).longValue(), lcg.stateAndUpdate(), msg); } /** * Create a big integer treating the value as unsigned. * * @param v Value * @return the big integer */ private static BigInteger toUnsignedBigInteger(long v) { return v < 0 ? TWO_POW_63.add(BigInteger.valueOf(v & Long.MAX_VALUE)) : BigInteger.valueOf(v); } } /** * Test the XBG implementations. These tests should execute only once, and not * for each instance of the abstract outer class (i.e. the test is not {@code @Nested}). * * <p>Note: The XBG implementation are extensions of RNGs already verified against * reference implementations. These tests ensure that the upper bits of the state is * identical after {@code n} cycles then a jump; or a jump then {@code n} cycles. * This is the functionality required for a jumpable XBG generator. */ static class XBGTest { @RepeatedTest(value = 5) void testXBGXoRoShiRo128NoJump() { final SplittableRandom r = new SplittableRandom(); assertNoJump(new XBGXoRoShiRo128(r.nextLong(), r.nextLong(), false)); } @RepeatedTest(value = 5) void testXBGXoShiRo256NoJump() { final SplittableRandom r = new SplittableRandom(); assertNoJump(new XBGXoShiRo256(r.nextLong(), r.nextLong(), r.nextLong(), r.nextLong(), false)); } @RepeatedTest(value = 5) void testXBGXoRoShiRo1024NoJump() { final SplittableRandom r = new SplittableRandom(); assertNoJump(new XBGXoRoShiRo1024(r.longs(16).toArray(), false)); } void assertNoJump(SubGen g) { final SubGen g1 = g.copyAndJump(); Assertions.assertNotSame(g, g1); for (int i = 0; i < 10; i++) { Assertions.assertEquals(g.stateAndUpdate(), g1.stateAndUpdate()); } final SubGen g2 = g.copyAndLongJump(); Assertions.assertNotSame(g, g2); for (int i = 0; i < 10; i++) { Assertions.assertEquals(g.stateAndUpdate(), g2.stateAndUpdate()); } } @RepeatedTest(value = 5) void testXBGXoRoShiRo128Jump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 2, XBGXoRoShiRo128::new, SubGen::copyAndJump); } @RepeatedTest(value = 5) void testXBGXoRoShiRo128LongJump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 2, XBGXoRoShiRo128::new, SubGen::copyAndLongJump); } @RepeatedTest(value = 5) void testXBGXoShiRo256Jump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 4, XBGXoShiRo256::new, SubGen::copyAndJump); } @RepeatedTest(value = 5) void testXBGXShiRo256LongJump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 4, XBGXoShiRo256::new, SubGen::copyAndLongJump); } @RepeatedTest(value = 5) void testXBGXoRoShiRo1024Jump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 16, XBGXoRoShiRo1024::new, SubGen::copyAndJump); } @RepeatedTest(value = 5) void testXBGXoRoShiRo1024LongJump() { assertJumpAndCycle(ThreadLocalRandom.current().nextLong(), 16, XBGXoRoShiRo1024::new, SubGen::copyAndLongJump); } /** * Assert alternating jump and cycle of the XBG. * * <p>This test verifies one generator can jump and advance {@code n} steps * while the other generator advances {@code n} steps then jumps. The two should * be in the same state. The copy left behind by the first jump should match the * state of the other generator as it cycles. * * @param testSeed A seed for the test * @param seedSize The seed size * @param factory Factory to create the XBG * @param jump XBG jump function */ private static void assertJumpAndCycle(long testSeed, int seedSize, Function<long[], SubGen> factory, UnaryOperator<SubGen> jump) { final SplittableRandom r = new SplittableRandom(testSeed); final long[] seed = r.longs(seedSize).toArray(); final int[] steps = createSteps(r, seedSize); final int cycles = 2 * seedSize; final SubGen rng1 = factory.apply(seed); final SubGen rng2 = factory.apply(seed); // Try jumping and stepping with a number of steps up to the seed size for (int i = 0; i < steps.length; i++) { final int step = steps[i]; final int index = i; // Jump 1 final SubGen copy = jump.apply(rng1); // Advance 2; it should match the copy for (int j = 0; j < step; j++) { Assertions.assertEquals(rng2.stateAndUpdate(), copy.stateAndUpdate(), () -> String.format("[%d] Incorrect trailing copy, seed=%d", index, testSeed)); // Advance in parallel rng1.stateAndUpdate(); } // Catch up 2; it should match 1 jump.apply(rng2); for (int j = 0; j < cycles; j++) { Assertions.assertEquals(rng1.stateAndUpdate(), rng2.stateAndUpdate(), () -> String.format("[%d] Incorrect after catch up jump, seed=%d", index, testSeed)); } } } } ///////////////////////////////////// // Tests for the LXM implementation ///////////////////////////////////// /** * Gets the factory to create a composite LXM generator. * The generator is used to provide reference output for the generator under test. * * @return the factory */ abstract LXMGeneratorFactory getFactory(); /** * Creates a new instance of the RNG under test. * * @param seed the seed * @return the RNG */ abstract LongJumpableUniformRandomProvider create(long[] seed); /** * Gets a stream of reference data. Each Argument consist of the long array seed and * the long array of the expected output from the LXM generator. * <pre> * long[] seed; * long[] expected; * return Stream.of(Arguments.of(seed, expected)); * </pre> * * @return the reference data */ abstract Stream<Arguments> getReferenceData(); /** * Test the reference data with the LXM generator. * The reference data should be created with a reference implementation of the * LXM algorithm, for example the implementations in JDK 17. */ @ParameterizedTest @MethodSource(value = "getReferenceData") final void testReferenceData(long[] seed, long[] expected) { RandomAssert.assertEquals(expected, create(seed)); } /** * Test the reference data with the composite LXM generator. This ensures the composite * correctly implements the LXM algorithm. */ @ParameterizedTest @MethodSource(value = "getReferenceData") final void testReferenceDataWithComposite(long[] seed, long[] expected) { RandomAssert.assertEquals(expected, getFactory().create(seed)); } /** * Test initial output is the mix of the most significant bits of the LCG and XBG state. * This test ensures the LXM algorithm applies the mix function to the current state * before state update. Thus the initial output should be a direct mix of the seed state. */ @RepeatedTest(value = 5) final void testInitialOutput() { final long[] seed = createRandomSeed(); // Upper 64 bits of LCG state final long s = seed[getFactory().lcgSeedSize() / 2]; // Upper 64 bits of XBG state final long t = seed[getFactory().lcgSeedSize()]; Assertions.assertEquals(getFactory().getMix().apply(s, t), create(seed).nextLong()); } @Test final void testConstructorWithZeroSeedIsPartiallyFunctional() { // Note: It is impractical to demonstrate the RNG is partially functional: // output quality may be statistically poor and the period is that of the LCG. // The test demonstrates the RNG is not totally non-functional with a zero seed // which is not the case for a standard XBG. final int seedSize = getFactory().seedSize(); RandomAssert.assertNextLongNonZeroOutput(create(new long[seedSize]), 0, seedSize); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testConstructorWithoutFullLengthSeed(long[] seed) { // Hit the case when the input seed is self-seeded when not full length final int seedSize = getFactory().seedSize(); RandomAssert.assertNextLongNonZeroOutput(create(new long[] {seed[0]}), seedSize, seedSize); } @RepeatedTest(value = 5) final void testConstructorIgnoresFinalAddParameterSeedBit() { final long[] seed1 = createRandomSeed(); final long[] seed2 = seed1.clone(); final int seedSize = getFactory().seedSize(); // seed1 unset final add parameter bit; seed2 set final bit final int index = getFactory().lcgSeedSize() / 2 - 1; seed1[index] &= -1L << 1; seed2[index] |= 1; Assertions.assertEquals(1, seed1[index] ^ seed2[index]); final LongJumpableUniformRandomProvider rng1 = create(seed1); final LongJumpableUniformRandomProvider rng2 = create(seed2); RandomAssert.assertNextLongEquals(seedSize * 2, rng1, rng2); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testJump(long[] seed, long[] expected) { final long[] expectedAfter = createExpectedSequence(seed, expected.length, false); RandomAssert.assertJumpEquals(expected, expectedAfter, create(seed)); } @ParameterizedTest @MethodSource(value = "getReferenceData") final void testLongJump(long[] seed, long[] expected) { final long[] expectedAfter = createExpectedSequence(seed, expected.length, true); RandomAssert.assertLongJumpEquals(expected, expectedAfter, create(seed)); } @RepeatedTest(value = 5) final void testJumpAndOutput() { assertJumpAndOutput(false, ThreadLocalRandom.current().nextLong()); } @RepeatedTest(value = 5) final void testLongJumpAndOutput() { assertJumpAndOutput(true, ThreadLocalRandom.current().nextLong()); } /** * Assert alternating jump and output from the LXM generator and the * reference composite LXM generator. * * <p>The composite generator uses an LCG that is tested to match * output after a jump (see {@link LCGTest} or a series of cycles. * The XBG generator is <em>assumed</em> to function similarly. * The {@link XBGTest} verifies that the generator is in the same * state when using {@code n} steps then a jump; or a jump then * {@code n} steps. * * <p>This test verifies one LXM generator can jump and advance * {@code n} steps while the other generator advances {@code n} * steps then jumps. The two should be in the same state. The copy * left behind by the first jump should match the output of the other * generator as it cycles. * * @param longJump If true use the long jump; otherwise the jump * @param testSeed A seed for the test */ private void assertJumpAndOutput(boolean longJump, long testSeed) { final SplittableRandom r = new SplittableRandom(testSeed); final long[] seed = createRandomSeed(r::nextLong); final int[] steps = createSteps(r); final int cycles = getFactory().seedSize(); LongJumpableUniformRandomProvider rng1 = create(seed); LongJumpableUniformRandomProvider rng2 = getFactory().create(seed); final UnaryOperator<LongJumpableUniformRandomProvider> jump = longJump ? x -> (LongJumpableUniformRandomProvider) x.longJump() : x -> (LongJumpableUniformRandomProvider) x.jump(); // Alternate jumping and stepping then stepping and jumping. for (int i = 0; i < steps.length; i++) { final int step = steps[i]; final int index = i; // Jump 1 LongJumpableUniformRandomProvider copy = jump.apply(rng1); // Advance 2; it should match the copy for (int j = 0; j < step; j++) { Assertions.assertEquals(rng2.nextLong(), copy.nextLong(), () -> String.format("[%d] Incorrect trailing copy, seed=%d", index, testSeed)); // Advance 1 in parallel rng1.nextLong(); } // Catch up 2; it should match 1 jump.apply(rng2); for (int j = 0; j < cycles; j++) { Assertions.assertEquals(rng1.nextLong(), rng2.nextLong(), () -> String.format("[%d] Incorrect after catch up jump, seed=%d", index, testSeed)); } // Swap copy = rng1; rng1 = rng2; rng2 = copy; } } @RepeatedTest(value = 5) final void testSaveRestoreAfterJump() { assertSaveRestoreAfterJump(false, ThreadLocalRandom.current().nextLong()); } @RepeatedTest(value = 5) final void testSaveRestoreAfterLongJump() { assertSaveRestoreAfterJump(true, ThreadLocalRandom.current().nextLong()); } /** * Assert that the precomputation of the jump coefficients functions * with saved/restore. * * @param longJump If true use the long jump; otherwise the jump * @param testSeed A seed for the test */ private void assertSaveRestoreAfterJump(boolean longJump, long testSeed) { final SplittableRandom r = new SplittableRandom(testSeed); final int cycles = getFactory().seedSize(); final UnaryOperator<LongJumpableUniformRandomProvider> jump = longJump ? x -> (LongJumpableUniformRandomProvider) x.longJump() : x -> (LongJumpableUniformRandomProvider) x.jump(); // Create 2 generators and jump them final LongJumpableUniformRandomProvider rng1 = create(createRandomSeed(r::nextLong)); final LongJumpableUniformRandomProvider rng2 = create(createRandomSeed(r::nextLong)); jump.apply(rng1); jump.apply(rng2); // Restore the state from one to the other RestorableUniformRandomProvider g1 = (RestorableUniformRandomProvider) rng1; RestorableUniformRandomProvider g2 = (RestorableUniformRandomProvider) rng2; g2.restoreState(g1.saveState()); // They should have the same output RandomAssert.assertNextLongEquals(cycles, rng1, rng2); // They should have the same output after a jump too jump.apply(rng1); jump.apply(rng2); RandomAssert.assertNextLongEquals(cycles, rng1, rng2); } /** * Create the expected sequence after a jump by advancing the XBG and LCG * sub-generators. This is done by creating and jumping the composite LXM * generator. * * @param seed the seed * @param length the length of the expected sequence * @param longJump If true use the long jump; otherwise the jump * @return the expected sequence */ private long[] createExpectedSequence(long[] seed, int length, boolean longJump) { final LXMGenerator rng = getFactory().create(seed); if (longJump) { rng.longJump(); } else { rng.jump(); } return LongStream.generate(rng::nextLong).limit(length).toArray(); } /** * Creates a random seed of the correct length. Used when the seed can be anything. * * @return the seed */ private long[] createRandomSeed() { return createRandomSeed(new SplittableRandom()::nextLong); } /** * Creates a random seed of the correct length using the provided generator. * Used when the seed can be anything. * * @param gen the generator * @return the seed */ private long[] createRandomSeed(LongSupplier gen) { return LongStream.generate(gen).limit(getFactory().seedSize()).toArray(); } /** * Creates a random permutation of steps in [1, seed size]. * The seed size is obtained from the LXM generator factory. * * @param rng Source of randomness * @return the steps */ private int[] createSteps(SplittableRandom rng) { return createSteps(rng, getFactory().seedSize()); } /** * Creates a random permutation of steps in [1, seed size]. * * @param rng Source of randomness * @param seedSize Seed size * @return the steps */ private static int[] createSteps(SplittableRandom rng, int seedSize) { final int[] steps = IntStream.rangeClosed(1, seedSize).toArray(); // Fisher-Yates shuffle for (int i = steps.length; i > 1; i--) { final int j = rng.nextInt(i); final int tmp = steps[i - 1]; steps[i - 1] = steps[j]; steps[j] = tmp; } return steps; } }
3,082
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/SplitMix64Test.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class SplitMix64Test { @Test void testReferenceCode() { final long[] expectedSequence = { 0x4141302768c9e9d0L, 0x64df48c4eab51b1aL, 0x4e723b53dbd901b3L, 0xead8394409dd6454L, 0x3ef60e485b412a0aL, 0xb2a23aee63aecf38L, 0x6cc3b8933c4fa332L, 0x9c9e75e031e6fccbL, 0x0fddffb161c9f30fL, 0x2d1d75d4e75c12a3L, 0xcdcf9d2dde66da2eL, 0x278ba7d1d142cfecL, 0x4ca423e66072e606L, 0x8f2c3c46ebc70bb7L, 0xc9def3b1eeae3e21L, 0x8e06670cd3e98bceL, 0x2326dee7dd34747fL, 0x3c8fff64392bb3c1L, 0xfc6aa1ebe7916578L, 0x3191fb6113694e70L, 0x3453605f6544dac6L, 0x86cf93e5cdf81801L, 0x0d764d7e59f724dfL, 0xae1dfb943ebf8659L, 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, 0x7bf121347c06677fL, 0x4fd0c88d25db5ccbL, 0x99af3be9ebe0a272L, 0x94f2b33b74d0bdcbL, 0x24b5d9d7a00a3140L, 0x79d983d781a34a3cL, 0x582e4a84d595f5ecL, 0x7316fe8b0f606d20L, }; final long seed = 0x1a2b3c4d5e6f7531L; RandomAssert.assertEquals(expectedSequence, new SplitMix64(seed)); // Test with Long RandomAssert.assertEquals(expectedSequence, new SplitMix64(Long.valueOf(seed))); } }
3,083
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256PlusPlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo256PlusPlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro256plusplus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, }; private static final long[] EXPECTED_SEQUENCE = { 0x83256c3efe494810L, 0xb6a32c7a2f427e87L, 0xea4a4faa5f25c89cL, 0xbc7eccdda31316ccL, 0x13fd0f7150d989c6L, 0x547138cbae221c4bL, 0x9a2ed08e202ccdd4L, 0x71c76beffd5ffaf7L, 0x4a82a53f9bf0e159L, 0x82b8fee551e226f6L, 0xc8a7cb2002fbabd2L, 0xe9fd4b8e8420b6caL, 0xc4fee10ff73a4513L, 0xbeee1386595fd5bbL, 0x1ca9ea9f7af81173L, 0x1182e0f6515e7a82L, 0x92f033c288c73349L, 0xf929b1c910f5d6fdL, 0xd74f135a22456c58L, 0x5db7c5f1bab2ba95L, 0x35bd2e90555e90ffL, 0x82f57164f0a873e8L, 0xfe8c06ad1f4322a3L, 0xb5830910972042f4L, 0x01b098fccc86e9a1L, 0x0a401cbff79d2968L, 0xaee758e14fc8b6c4L, 0x9b69b1669c551c7bL, 0xc424e07de89d8003L, 0x2f54c2bc2413685cL, 0x18ed020132f7fd78L, 0x1296df883b21ddb7L, 0x08ce35eb04245592L, 0x5b379c8c2a13dd1fL, 0xd5d72bff230d0038L, 0xe8fa9e75a5b11653L, 0x01cda02ee361fc5dL, 0x458c1ba437db0e66L, 0x653d400afec2f1a5L, 0xce41edefbfc16d19L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0x097817815ff7650eL, 0xe3a4a1ba31fe551cL, 0xd7edac1f857eb72eL, 0xeaf103ab604c1f33L, 0x274bf134459253efL, 0xb2c037e04c354378L, 0x42f0d50f53d01cfaL, 0x170b7b0992291c41L, 0xab2f51abd6144ce5L, 0xb1b29ec57f17c875L, 0xeee5b6846ef8731fL, 0xc93a9a89f885741bL, 0xeb2c0ef80f38209aL, 0x8b11ae67b295bf5dL, 0x102ff04bf5f6a514L, 0x011491b4c8d59849L, 0xd949e7c675fa13e0L, 0xf03f2b6bb95f63dfL, 0x0394a28de3853a88L, 0xc6bf4defddf657b3L, 0x137edf82bc6da988L, 0xdd3da2a860fe7aaaL, 0xd562a0fceb57f6cfL, 0x7ad187b77b82ed41L, 0xcee517096675814bL, 0x9cb90e61cd248c36L, 0x08e7d20e76147477L, 0xeb3be1e1fa580f29L, 0xed5803c04e7f457dL, 0x57736d360cf9b389L, 0xf98478390475f655L, 0x077fe75d911a51d2L, 0x0acd5c157adfb636L, 0x4e17872ccc3d84b0L, 0x9e31d8f0e2943738L, 0xaaee22c711ec0602L, 0x48998bdfa462be6dL, 0xff066ab2d6250e56L, 0xe319e9c2ab1ad697L, 0x2b14627b78929b4aL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x0a6b9e34dbf9c7caL, 0x2214a78c97dc2187L, 0x9f44832a1e1ba9c6L, 0x49f5a7c5d1618518L, 0xbca9ab8bed062466L, 0x21da06a34efaf84aL, 0xfc22b0fe3dda8c05L, 0x9b85b0b6cfc93dafL, 0x252992459938a6c2L, 0x57e8cc4f4dd5316cL, 0x7942b8174c68b3f7L, 0x0687eb4b9902d56cL, 0x76c25e3f67a134b1L, 0x9b4a908b5fc601e4L, 0x28019ff6b951f9baL, 0xb23ce81de5d3e05dL, 0x44908a02c9cacabcL, 0xf527591a7870712dL, 0x4c8647740ac64a92L, 0xbf51a8298a198b8dL, 0x2a899624fd875afdL, 0xbd9806ef5269c9bfL, 0xd75990e2678307c8L, 0x2984ae7ce86bcbdcL, 0x948a1a74b031559cL, 0xf560ffba9a474a3cL, 0x8c82a4075190e936L, 0x1735d50cb3619419L, 0x488716879f534f96L, 0x359aadcdd6a5d5adL, 0x314e31dac415bc2bL, 0x066cb902db3e6bb9L, 0xd4d8c53a90d7480dL, 0x721670f7e471039fL, 0x0d7193e03567d61cL, 0xdb6d987c352b9fd0L, 0x3c22102b526bff0cL, 0x24836afdeaab511eL, 0x9f098d3918b5fd3cL, 0x1f6c643f8a0b8c1dL, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo256PlusPlus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo256PlusPlus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo256PlusPlus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo256PlusPlus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo256PlusPlus rng1 = new XoShiRo256PlusPlus(SEED); final XoShiRo256PlusPlus rng2 = new XoShiRo256PlusPlus(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo256PlusPlus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo256PlusPlus(SEED)); } }
3,084
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo256StarStarTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo256StarStarTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 4; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro256starstar.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, }; private static final long[] EXPECTED_SEQUENCE = { 0x462c422df780c48eL, 0xa82f1f6031c183e6L, 0x8a113820e8d2ca8dL, 0x1ac7023a26534958L, 0xac8e41d0101e109cL, 0x46e34bc13edd63c4L, 0x3a26776adcd665c3L, 0x9ac6c9bea8fc518cL, 0x1cef0aa07cc738c4L, 0x5136a5f070244b1dL, 0x12e2e12edee691ffL, 0x28942b20799b71b4L, 0xbe2d5c4267af2469L, 0x9dbec53728b2b9b7L, 0x893cf86611b14a96L, 0x712c226c79f066d6L, 0x1a8a11ef81d2ac60L, 0x28171739ef8f2f46L, 0x073baa93525f8b1dL, 0xa73c7f3cb93df678L, 0xae5633ab977a3531L, 0x25314041ba2d047eL, 0x31e6819dea142672L, 0x9479fa694f4c2965L, 0xde5b771a968472b7L, 0xf0501965d9eeb4a3L, 0xef25a2a8ec90b911L, 0x1f58f71a75392659L, 0x32d9547188781f3cL, 0x2d13b036ccf65bc0L, 0x289f9cc038dd952fL, 0x6ae2d5231e50824aL, 0x75651acfb42ab170L, 0x7369aeb4f10056cfL, 0x0297ed632a97cf75L, 0x19f534c778015b72L, 0x5d1d111c5ff182a8L, 0x861cdfe8e8014b96L, 0x07c6071e08112c83L, 0x15601582dcf4e4feL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xb416bd07926c6735L, 0x2f91faf5c6c9f79aL, 0x538d25a148318bc1L, 0x79ffea0eb76500e6L, 0x7b74b0513602a5e1L, 0xdc114ef0bb881ac5L, 0xa0845293613f458bL, 0xb650a96e30f09819L, 0xbd2aeb7eb2ac1a6aL, 0x724e2d39d21b00baL, 0x4b38be1deb8553caL, 0xd83f40c399601212L, 0x97aba644588c210aL, 0x5caa9f64a83047b6L, 0x36ade013e70660abL, 0xf1bf69a51790aadaL, 0x4c25aad6aac062ddL, 0x072e1ab91c2ec7c1L, 0x9343a09f5f5eec61L, 0xdcdd0baaaa38bce7L, 0x0c0ea0bcd389f16aL, 0x0765633fee36d533L, 0xfba7f80666c43a76L, 0x896323052d851b9cL, 0x60bdd013e4a0a3f3L, 0x244be4b11b49ca4cL, 0x1513dcbe57a23089L, 0x809e9476dd32f1baL, 0x09e914013550ced8L, 0x68873250d4a070b9L, 0x0c7709d63a915660L, 0x97014b396d121b71L, 0xc50b646fe0f40c95L, 0x4edff941823be25cL, 0x5310e5528d79fa55L, 0xb7353ccef26265e9L, 0x3346bda5d2ac2d7dL, 0xbeab7520abd736f1L, 0x7195e9c9f28eac6aL, 0x64d959048b71d87bL, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x01aeb600840f594fL, 0xb658457c13139b18L, 0x45de59065e34c7a1L, 0xee4e2dc3272cbdddL, 0xab76ae6ad7b58827L, 0x1125e963c7de503dL, 0x262a3e31960c225cL, 0x6959383a6ca6db93L, 0x162e98220db47855L, 0x8c241774ab03fb0fL, 0xa574997e9135c756L, 0x7d69f1c620f6e354L, 0xebcaa8a26b1e0d11L, 0x7013a78241c67e80L, 0xd653dc4a68e9f576L, 0x54f483e05528cdeeL, 0x0f46d76b266f1bdeL, 0xb5364248293168b0L, 0x83328b16fdd08b22L, 0x3c9241622a8ed2d3L, 0x4fb5158c8ba832e9L, 0x98a540967c042253L, 0xfc215e6a07670358L, 0xafc3ccd56bc029beL, 0xf0b16f5c1edf807aL, 0x02792082f4adc46fL, 0xe6203988ebcd9f8fL, 0xa3f9c62dc60e3a05L, 0x9ec363a473ce3affL, 0x2e787e5b4ff29d4dL, 0x89899eb9b705963fL, 0xc9114da1cad45697L, 0xdb8fc78dc1fb839eL, 0xe537b60ba49474d5L, 0xcffb3215f6208209L, 0xbfdabe221f9c308cL, 0x3d30cabb172af4b2L, 0xfd64f857f0f3b8d8L, 0x4b554d6b026bf8c1L, 0xf5ebb49acd5d6f24L, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo256StarStar(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo256StarStar(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo256StarStar.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo256StarStar(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo256StarStar rng1 = new XoShiRo256StarStar(SEED); final XoShiRo256StarStar rng2 = new XoShiRo256StarStar(SEED[0], SEED[1], SEED[2], SEED[3]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo256StarStar(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo256StarStar(SEED)); } }
3,085
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/XoShiRo512PlusTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class XoShiRo512PlusTest { /** The size of the array SEED. */ private static final int SEED_SIZE = 8; /* * Data from running the executable compiled from the author's C code: * http://xoshiro.di.unimi.it/xoshiro512plus.c */ private static final long[] SEED = { 0x012de1babb3c4104L, 0xa5a818b8fc5aa503L, 0xb124ea2b701f4993L, 0x18e0374933d8c782L, 0x2af8df668d68ad55L, 0x76e56f59daa06243L, 0xf58c016f0f01e30fL, 0x8eeafa41683dbbf4L, }; private static final long[] EXPECTED_SEQUENCE = { 0xb252cbe62b5b8a97L, 0xa4aaec677f60aaa2L, 0x1c8bd694b50fd00eL, 0x02753e0294233973L, 0xbfec0be86d152e2dL, 0x5b9cd7265f320e98L, 0xf8ec45eccc703724L, 0x83fcbefa0359b3c1L, 0xbd27fcdb7e79265dL, 0x88934227d8bf3cf0L, 0x99e1e79384f40371L, 0xe7e7fd0af2014912L, 0xebdd19cbcd35745dL, 0x218994e1747243eeL, 0x80628718e5d310daL, 0x88ba1395debd989cL, 0x72e025c0928c6f55L, 0x51400eaa050bbb0aL, 0x72542ad3e7fe29e9L, 0x3a3355b9dcb9c8b0L, 0x2f6618f3df6126f4L, 0x34307608d886d40fL, 0x34f5a22e98fe3375L, 0x558f6560d08b9ec3L, 0xae78928bcb041d6cL, 0xe7afe32a7caf4587L, 0x22dcfb5ca129d4bdL, 0x7c5a41864a6f2cf6L, 0xbe1186add0fe46a7L, 0xd019fabc10dc96a5L, 0xafa642ef6837d342L, 0xdc4924811f62cf03L, 0xdeb486ccebccf747L, 0xd827b16c9189f637L, 0xf1aab3c3c690a71dL, 0x6551214a7f04a2a5L, 0x44b8edb239f2a141L, 0xb840cb37cfbeab59L, 0x0e9558adc0987ca2L, 0xc60442d5ff290606L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_JUMP = { 0xda8c2f51c0a12fedL, 0xcc63a107350b8d8bL, 0x3730965c235cdc8bL, 0xdff53b55412bf8d4L, 0xa75dca084c1a404eL, 0x395c5e03c3d51b84L, 0x0c57783cfb429d7bL, 0x402c0857310c0804L, 0x5fb34f057266575aL, 0x196b0694db94ee83L, 0x31ce1b0c4d40a337L, 0x1f21143738a48e84L, 0x4c00bde2f7d7184fL, 0xaad1564500e3b773L, 0xba2729da2d1bb5d7L, 0xcd1e33914dd13ac3L, 0xf98130cc1b0053baL, 0x44eb6a48353a1e5eL, 0x490ae7ce04dfeda3L, 0xb553106ef217b297L, 0xc073ae69eb507d0bL, 0xa056894e1deea79cL, 0xef69db4765dc1479L, 0x8575bf9f4686ab44L, 0x35cbaf0c0fb38f4bL, 0xfa30396425d7f722L, 0x312f12282a479019L, 0x40d7ae6d2b24254eL, 0xa370e089e50b34d6L, 0x5f364b5ae2a36a00L, 0x0a923136b57bb730L, 0x8e46536fce01229cL, 0x3b2c38bc61116bb6L, 0x3f933d48f4a99c53L, 0x6e0b6ef2a27cd0ddL, 0xb53409e3aa42274aL, 0xb0389318ac95388fL, 0x12c69799c7c33350L, 0x7e37dbd8210b480cL, 0xf3ab8bf173c83485L, }; private static final long[] EXPECTED_SEQUENCE_AFTER_LONG_JUMP = { 0x98ec0fe940d88036L, 0x3e52a613181661d2L, 0xeb21cca2c39a958aL, 0x54ebdbbcf454bc00L, 0x35b40a46c5a2db60L, 0xba92b5b8ec604df6L, 0xb8a9d151e1de068cL, 0xf932e23c318739c7L, 0x7d37ce0d0251a4f9L, 0x3294c0651c007662L, 0x3baa7ebc5883451dL, 0x5f074c651e12f539L, 0x173759a4f89afd6fL, 0xb84f6162c377111cL, 0x8ff2ae4a11140c3bL, 0x90f58d08cd59d92bL, 0x3a3fc7591a19dc9cL, 0x7abde79e2d744124L, 0xb501dcc26191260dL, 0xc579a01d3b8060e7L, 0x44f8ff268669152bL, 0xa64fc5f1793acc93L, 0xe8e846be0146eeacL, 0x78508943a2f9f185L, 0x8ac83b0956d74f6dL, 0x9b2edb8573b06d42L, 0x7043f31d7d3b0072L, 0xcef8bdd056a672aeL, 0xa598d8ca8da699baL, 0xd3f4d5229fcd63e0L, 0xc32969e1c8b3344bL, 0x5cd49a0984c25cbeL, 0xe611854c41080e47L, 0x2bb80e455908083dL, 0x76b63c69756b1c60L, 0xf1b5cb3e99e921b7L, 0x3eec38ebbff82d51L, 0xf0d1900eb73cf3c0L, 0x0d852253155da740L, 0xa0b237932c01e9eaL, }; @Test void testReferenceCode() { RandomAssert.assertEquals(EXPECTED_SEQUENCE, new XoShiRo512Plus(SEED)); } @Test void testConstructorWithZeroSeedIsNonFunctional() { RandomAssert.assertNextIntZeroOutput(new XoShiRo512Plus(new long[SEED_SIZE]), 2 * SEED_SIZE); } @Test void testConstructorWithSingleBitSeedIsFunctional() { RandomAssert.assertLongArrayConstructorWithSingleBitSeedIsFunctional(XoShiRo512Plus.class, SEED_SIZE); } @Test void testConstructorWithoutFullLengthSeed() { // Hit the case when the input seed is self-seeded when not full length RandomAssert.assertNextLongNonZeroOutput(new XoShiRo512Plus(new long[] {SEED[0]}), SEED_SIZE, SEED_SIZE); } @Test void testElementConstructor() { final XoShiRo512Plus rng1 = new XoShiRo512Plus(SEED); final XoShiRo512Plus rng2 = new XoShiRo512Plus(SEED[0], SEED[1], SEED[2], SEED[3], SEED[4], SEED[5], SEED[6], SEED[7]); RandomAssert.assertNextLongEquals(SEED.length * 2, rng1, rng2); } @Test void testJump() { RandomAssert.assertJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_JUMP, new XoShiRo512Plus(SEED)); } @Test void testLongJump() { RandomAssert.assertLongJumpEquals(EXPECTED_SEQUENCE, EXPECTED_SEQUENCE_AFTER_LONG_JUMP, new XoShiRo512Plus(SEED)); } }
3,086
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L128X256MixTest.java
/* * 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.rng.core.source64; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L128X256Mix}. */ class L128X256MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 4; } @Override public int xbgSeedSize() { return 4; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG128(seed[0], seed[1], seed[2], seed[3]), new XBGXoShiRo256(seed[4], seed[5], seed[6], seed[7], false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L128X256Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 17: * java.util.random.RandomGeneratorFactory.of("L128X256MixRandom").create(seed) * Full seed expanded from a single long value using the same method in the * OpenJDK source (v17) and recorded in the order used by the Commons RNG code. * * Note: This is a different from the other LXMTest instances due to a constructor * bug in JDK 17 L128X256MixRandom. The result is an initial state for the LCG * that is always 1L. * See: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8283083 */ return Stream.of( Arguments.of( new long[] { 0xaac2f67d761dadc6L, 0xe584ab0136fa95fcL, 0x0000000000000000L, 0x0000000000000001L, 0xa56f550e4455ad6bL, 0xfa8c6a4c0b4f87c1L, 0x97aa5a6091689f0cL, 0x97774f9a7b01252aL, }, new long[] { 0x0f6839e2df51d066L, 0x63828bfa952b7223L, 0x1449a6518fc6698aL, 0xf0d255739e8a95a9L, 0xdc2277916582ab84L, 0x855172869dc4ad71L, 0x9f1e38cd53f3aeceL, 0x8f0cfee1ed210171L, 0xdb86b178e98ef8d0L, 0x6e53a3629d1485f7L, 0x033da8ec17c6256cL, 0x94a44e70d72cd494L, 0x71a2171e09f1503dL, 0x6355ff323a49300eL, 0x4d4f7e37beec3a76L, 0x5391b119c23afe81L, 0x21a28a3f83d71acaL, 0xe157ce29ed8a468dL, 0xfbcbeab9dfa54c0aL, 0x5bd0072c9751b499L, 0x1f3ea8c1456a2cd3L, 0x656f6535b5a3d4d4L, 0xba7d0ee7b7cf61b8L, 0x8c26ad19a4660e2aL, 0x9ca6f47a205fdcdeL, 0xa2ae34ee95f8d89eL, 0xb6de34b282c7b220L, 0xf8c1d88a8d284430L, 0x4df71c7f08c3ad4bL, 0x9c7a7168f01b7905L, 0x93252635246315e0L, 0xcaaa648c929f8b87L, 0x9d5f169be9b8050dL, 0xa24bb43e098948daL, 0x47d1147f27e102eeL, 0xcf80915231874ea4L, 0xfb5a2832f261afc7L, 0xe4ed459ebe0e8d4dL, 0xa5f9df78cfed42d3L, 0x1cc368014904ab73L, }), Arguments.of( new long[] { 0x6e1a741ae7ec03ebL, 0xf5363e7b44211d57L, 0x0000000000000000L, 0x0000000000000001L, 0x0114edb0b4bbfa18L, 0x83154f7914d38972L, 0x8d932b636513ae0eL, 0xa0bb24e85d97c9fcL, }, new long[] { 0x360b1762f284ac1aL, 0xc7e08eb6c5264259L, 0x60eabd90a111f141L, 0xbd3d95bdc05e96fdL, 0xc12366f63a5cb505L, 0x30141aab158cf2deL, 0xefb1f62f804c4c02L, 0x198e4d8fdceed3f7L, 0x178f01697a119a29L, 0x5ab2081161a38a08L, 0x0e02978d9d84e577L, 0x17286dbb65ec4b83L, 0x15e5ccb7a1f5085aL, 0x11fdb06b66597f8cL, 0x4b8057b570a377c3L, 0xb274608cf0c9ec10L, 0x9f220c4df7966a96L, 0x15bc1babf827161dL, 0x7ed132f78f8be153L, 0x38522a2d55b16e76L, 0x70f56b472b9a589cL, 0x0c16b3de606a20b3L, 0x1e691d63b1e01619L, 0xe43b1de605c2efd8L, 0x6fd7144c0a6b2f7eL, 0x9d2c424422c1e228L, 0xc3fdd9daf170c845L, 0xb5416500b7597222L, 0x9d5beb65e35c57a4L, 0x3610afc9f107b341L, 0x26487e11ebcf8709L, 0x7e3f0ea4bbbe0d7bL, 0xfeaa2d257997c2edL, 0xcf8974fcac8abcfaL, 0x1ae3d7733f7d9cffL, 0xdeb446665fe7c07eL, 0x58a66ab0134febd5L, 0xcda459bb0c024431L, 0x109e6cfa2953268cL, 0xa74609ee30817c67L, }), Arguments.of( new long[] { 0x75576ea649c68bdfL, 0x988200da91af3658L, 0x0000000000000000L, 0x0000000000000001L, 0xb610c4732da06560L, 0x60dfa764b8ae927dL, 0x24867e0eecc8eb78L, 0x952f3d5dc17c0cf2L, }, new long[] { 0x7c2bf5f25669c985L, 0x65f5ce67c1250090L, 0x131fb13c3f2b0af0L, 0xab6f976bc3783faeL, 0xbac9b9bac68c1bb2L, 0x969a2438afc9eb6cL, 0x7e652efa93fc93b9L, 0xf1c6ccbd1333ea5bL, 0x2a25a2c25ad454e8L, 0x002ed102a1146657L, 0x8f482f8aed7b4b41L, 0x1cc0b65ed2ce02c1L, 0xad2c3f04da92abb0L, 0x3783c7d4f5bff2d8L, 0x8f1ddf96128c3d3eL, 0x8e10ceca6da015eeL, 0x6bc2a9963a300b32L, 0xe7283e10d87a55eeL, 0x7737c78b6497ed8dL, 0x509181e37cbf2e52L, 0x135e66e65ec985baL, 0xb191dd61abb669c7L, 0x82551e94692b0058L, 0x6acab0125b911923L, 0x7ad6ebb617bfd61eL, 0xa2854d9a0b1fb89cL, 0xb5088284b426fe15L, 0x74621d1fc29f0ad7L, 0xea5cf8ac302b12deL, 0xa8222d387b40a5a7L, 0x2dd23fd77aab83e0L, 0xd88147e1c98e69beL, 0x7aaba7d84838fd21L, 0x933440a3dd8ecf5aL, 0xc6fe6e8cdb7d09e7L, 0xa00cab23b7a20207L, 0xb769003d4abbef6bL, 0x119e829d8cdb859eL, 0x4cd41960d97c80a6L, 0xfb65ffaaf81144d3L, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(long[] seed, long[] expected) { final L128X256Mix rng1 = new L128X256Mix(seed); final L128X256Mix rng2 = new L128X256Mix(seed[0], seed[1], seed[2], seed[3], seed[4], seed[5], seed[6], seed[7]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L128X256Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L128X256Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,087
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/JenkinsSmallFast64Test.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class JenkinsSmallFast64Test { @Test void testReferenceCode() { /* * Tested with respect to the original reference: * See : https://burtleburtle.net/bob/rand/smallprng.html */ final long[] expectedSequence = { 0xb2eb2f629a2818c2L, 0xe6c4df3bd8e4a0c8L, 0x2b3ab71e4e888b46L, 0x12a6088f5960738dL, 0x95715b21fcb1a7d9L, 0x7acafc3916723b0fL, 0x3a0c5f8c4caff822L, 0x9b47b7a1e9784699L, 0x9c399839261a024fL, 0x56a2fa6eaa7a62aaL, 0xca6995ea5baeb8daL, 0x56cad0c4dee9cbb9L, 0xbb5df57850f117a5L, 0x147a41dad6a87b7bL, 0xf9225f2aa6485812L, 0x812b9d2c9b99aaa0L, 0x266ad947cac0acfcL, 0x19bcfc1b69831866L, 0xc5486e1cfa0eca28L, 0x80ca1802e7dd04b7L, 0x003addd1e44ff095L, 0xb9eaa245ce7c040bL, 0xe607e64b31a6e9b4L, 0x1553718b8013007bL, 0x86dcd29120fd807bL, 0xeb5b8ec5d73dc39eL, 0x3c26147f6b7ff7d7L, 0xe0b994497bf55bb5L, 0x24fb3dc33de779c6L, 0x022aba70fc48e04aL, 0xbcf938e19b81f27fL, 0x9022bd08a8ac7511L, 0x79ad91f7404ecef1L, 0x291858706a2286dbL, 0xf395681f493eb602L, 0xf85ed536da160b93L, 0x5dd685454dd0d913L, 0x150e7b8f99b10f7dL, 0xcd1c0b519cc69c05L, 0xca92e08bf2676077L, }; RandomAssert.assertEquals(expectedSequence, new JenkinsSmallFast64(0x012de1babb3c4104L)); } }
3,088
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L128X1024MixTest.java
/* * 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.rng.core.source64; import java.util.Arrays; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.Arguments; /** * Test for {@link L128X1024Mix}. */ class L128X1024MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 4; } @Override public int xbgSeedSize() { return 16; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG128(seed[0], seed[1], seed[2], seed[3]), new XBGXoRoShiRo1024(Arrays.copyOfRange(seed, 4, 20), false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L128X1024Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L128X1024MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0x6b58aa6ffc337cacL, 0x7554ffc31477c792L, 0xadb09543e412c559L, 0xaec37b44eedbebebL, 0x1b5f8ce14fd365aaL, 0xdd9583b20d633d9aL, 0x047b8ee58629d33dL, 0x1455a9f0a1b5072cL, 0x22176a92ad7fc152L, 0xdd5787b7217de80fL, 0xf2a39d303e489c65L, 0xc639427ce47fdf8fL, 0x719635b081f3f914L, 0xa697379a497e7e39L, 0x9e7ae3aa56a287ccL, 0x8b4abd91a352faa5L, 0x7b5f405155546a12L, 0x5b4043b9f1140d8fL, 0x2cb19f45483f80fbL, 0x0b6e5226e1c85467L, }, new long[] { 0x5fa2cf23d60cf67cL, 0xc31c2b6451aa2fbaL, 0xd2af323c077ee4d4L, 0x213bed298b3ad585L, 0xdcce8c3e6f63d6c6L, 0x3dfc05a23b884532L, 0xbcbfe3860bef2eaaL, 0xf656a285e4ebbce1L, 0x8f9505806a414372L, 0xbcff459696a30316L, 0xa700a8d63166a0aeL, 0x6407b055a4f0b3c7L, 0xe77333b14d15309aL, 0x70a9155fd1c1961bL, 0xdfc035a8c748ae7eL, 0x534f5830fe5297caL, 0xca187d9d2eab2437L, 0xcc3a1e20a36fa6abL, 0xf8f4960737866c0cL, 0x7eab49fd524b840dL, 0x9937413f08979628L, 0x017565584f1da654L, 0x13c76265c74cc0a6L, 0x54de60aea30cedecL, 0xe80ad8897a74b5a8L, 0xcd1555f1abbf30acL, 0x015d519e2a4c7d53L, 0xad32c13b44c39ffbL, 0xcd6a12650b3753f8L, 0x1486ea548da30363L, 0x98987c807f9660d0L, 0x6bb14b89a9643040L, 0x8ee4d97ec0bced99L, 0x3b9e0ee8a39a959aL, 0x5565cd513ab34ef9L, 0xd81ed95e235db404L, 0x9167e57917401421L, 0x30808415959689a4L, 0x1b868f963e2be44cL, 0x70f98922267d2397L, }), Arguments.of( new long[] { 0x72831df890850049L, 0x14d5fad4e42f9302L, 0xde0b08b519b5ed35L, 0xb074db75c9a488eeL, 0x9b32cb087f31bb65L, 0xc47709977d2f66bdL, 0x801689b1d45ccec6L, 0x17b9a964bd7c2914L, 0xf4691614d2280435L, 0x8e891da749b9afc6L, 0xe452871043428ec9L, 0x3412688e450d367cL, 0xb47eaadd6c656be4L, 0x4e4c09a9d9055ec9L, 0x60e549d120c759caL, 0xb1e0ebc749dc7df4L, 0xf479ceebad6b798aL, 0xb71d9e212eb1d1c1L, 0x28f0025d7748addaL, 0x6063774bbe188331L, }, new long[] { 0x237759030495d6bdL, 0xa1085516169c5723L, 0xf2938d898f81a4dcL, 0x3f2472fa6229549fL, 0x7b5c7d86db5cd897L, 0x2a3e9a1a243ab91fL, 0xad94f5d540a46bccL, 0x7cf02d130867296eL, 0x9ad5f8a5e883b2b2L, 0xc37ac97fff9ba5bdL, 0xf1d836c448e74e62L, 0xdface229fa5babe0L, 0x83fac2cce3a51eeeL, 0xfde459054fb9a650L, 0xf2289c9c7a3ce827L, 0x6096deea7085c16fL, 0xa7542a7e16638f15L, 0x0f38a3642d12078aL, 0x46f08e525547a329L, 0x6ffec9295514502cL, 0xef0ae056313ac76aL, 0x0972748d1e10ce75L, 0x45d76330939da7abL, 0x49a51c499b44c48fL, 0xcda6ac64fcb7e891L, 0xe08859838fb0fec5L, 0xbd1e49427e9460d3L, 0x69ab2b69ce6a6aacL, 0xc4cf49487adaf18fL, 0xf18641ff23c6d099L, 0xe25d8d4195a84f10L, 0x85602ba0e16337cfL, 0x118f4640b8f3550bL, 0xae642cb3a6d01717L, 0x5d3f27a3d6534f66L, 0xbf4ed49c67cd4a4cL, 0x1400824879988551L, 0xad2a6637c4512f53L, 0xf424063005575699L, 0xb29f63cbd1acb668L, }), Arguments.of( new long[] { 0x1362c961e1630fbeL, 0xa15f80cdcb56460fL, 0x54a55905815c1d21L, 0x3ae5f0c7bb27dddcL, 0x829c93e6c7c7025bL, 0xb0213f4c376814c1L, 0xba8cbf51f44bb2a5L, 0xeb34355868993b1fL, 0x08f3cda12137f730L, 0x4059c90010dc3b11L, 0xf7268debee731db0L, 0xba1e6900e52effb0L, 0x9901ff51f11fa35bL, 0x28d5e83d8aee75ceL, 0xf38c8641eb866c6dL, 0x9cfa85edc14efaa0L, 0x9e55a1737705d52bL, 0x15a494ad0cf92e68L, 0x74b70ce02553f7ddL, 0x1270569fe023553eL, }, new long[] { 0x6959d677d00dd35dL, 0x06faab2179b4dbe7L, 0x40ffae141e27250dL, 0x77fdf94f98ee84bfL, 0x981562b903976493L, 0x54036193c77a82fbL, 0x2cf0959da786b980L, 0x7b2481c1a56508dcL, 0x319b4e1e4fc56d20L, 0xc54d3ad54cfd1499L, 0xbb32cf5518dd6be9L, 0xaa4e32f37f7a2586L, 0xf67aacc627b86195L, 0x88980c4616943de3L, 0xb837ab119857807dL, 0x0cfbc2544dbf48b8L, 0xe8b9884fa0ae4e34L, 0x489aaa0d0627a22cL, 0x5443cebef68ea0a6L, 0x5ca4968dfd31b40aL, 0x74cfc71092585a27L, 0x0bda495018792daeL, 0x654affcb7eedad76L, 0xfcb97ca8b72c4382L, 0x89b6d82c5e526ef0L, 0x1d7c02820d6de8d5L, 0x69fe9b84565391a2L, 0xecca48b5a815e5c5L, 0x8f1503297cd02494L, 0x440ee5546a18b02cL, 0xf45519c36b910074L, 0x43dc905fa1732ccbL, 0x27440d7f50533e2eL, 0xab553d44b699875bL, 0x6162c477b201aabbL, 0x5a1addc6a8151187L, 0x33e56bf53aacefb4L, 0x108e02cfdb44c699L, 0xc985a4c385edf80dL, 0x4384e8799019d966L, })); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L128X1024Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L128X1024Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,089
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/L64X128MixTest.java
/* * 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.rng.core.source64; import java.util.stream.Stream; import org.apache.commons.rng.LongJumpableUniformRandomProvider; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** * Test for {@link L64X128Mix}. */ class L64X128MixTest extends AbstractLXMTest { /** * Factory to create a composite LXM generator that is equivalent * to the RNG under test. */ private static class Factory implements LXMGeneratorFactory { static final Factory INSTANCE = new Factory(); @Override public int lcgSeedSize() { return 2; } @Override public int xbgSeedSize() { return 2; } @Override public LXMGenerator create(long[] seed) { return new LXMGenerator(getMix(), new LCG64(seed[0], seed[1]), new XBGXoRoShiRo128(seed[2], seed[3], false)); } } @Override LXMGeneratorFactory getFactory() { return Factory.INSTANCE; } @Override LongJumpableUniformRandomProvider create(long[] seed) { return new L64X128Mix(seed); } @Override Stream<Arguments> getReferenceData() { /* * Reference data from JDK 19: * java.util.random.RandomGeneratorFactory.of("L64X128MixRandom").create(seed) * * Full byte[] seed created using SecureRandom.nextBytes. The seed was converted * to long[] by filling the long bits sequentially starting at the most significant * byte matching the method used by the JDK, e.g. * long[] result = new long[seed.length / Long.BYTES]; * ByteBuffer.wrap(seed).order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(result); * * Note: Seed order: LCG addition; LCG state; XBG state. */ return Stream.of( Arguments.of( new long[] { 0xa2fc3db3faf20b60L, 0x0ca17f844355c30bL, 0x966393c3c699b9c4L, 0x26d0b369e961d05dL, }, new long[] { 0x431e95bfddd868f1L, 0x11d41649fc250a2bL, 0x386107fb4229f3c6L, 0x58809538283ec2beL, 0x792d8502b636fa57L, 0xdfdd635a8d4b513bL, 0x639e33d9d46709a4L, 0xac064fae27c58ae2L, 0xf503c18f9b81e221L, 0xc8e9239b41d75ac2L, 0x305035c93feb8da0L, 0xcb639286b0b77172L, 0x134cb8dea2744fe3L, 0x849f85c7979eaaa8L, 0x7ec51bffd104b0b8L, 0xc9fb22d1f0ce8392L, 0xdcbedf16be02f77dL, 0x8779acb6ffd5cb89L, 0x24be000a509e92ebL, 0x73b09559615c1540L, 0x25f9fba5d4818f13L, 0x171607e2956fef8dL, 0x8950dc3f40d5f8cfL, 0xd11e7c72b36f1c72L, 0xe687a38bb9887225L, 0x078daf1a151ba805L, 0x2aeb69f8638fda77L, 0xff50d66233a5078dL, 0x9ed224880f7564a9L, 0x298f140cc162145dL, 0xeb2ba17c17e97b8fL, 0x59d785fd7c80a45bL, 0x45afefc04e164b8fL, 0x6282afa6f7a13d16L, 0x59a5232c189d6c95L, 0x7c36546951518872L, 0x7973a0bdd8a7d494L, 0x711c54e35cc6d30aL, 0xc931dd22fcc5e6a9L, 0x079e4436817465d5L, }), Arguments.of( new long[] { 0x36d2370e6207bab2L, 0xc4753d31834da409L, 0xecef76bb60e6f8daL, 0xb6b63fbeb3710b5eL, }, new long[] { 0x4a3b46e4c46330c6L, 0x1edb14ee973103cbL, 0x82ac31b5cfd9b725L, 0xbaa145314d88b5c1L, 0xced95922c6f6fdcfL, 0xdba6a91b0fb3ecc1L, 0x3d479c0083bc99e8L, 0x4935d8779f92155cL, 0xb375363b0f724142L, 0x2a849a0a4d3328bbL, 0x13d9384867b9e73dL, 0x692a97c3730699c7L, 0x1110586b1ba6aaf7L, 0x8b49b5c42008a6b4L, 0xb099f56ded714e73L, 0xe47202801ea2edeeL, 0x90a3d0c6c4cd6616L, 0x69e27ce6f839bffeL, 0xd5f1dacba40dfa28L, 0xcbc3b8207d22ab3cL, 0xbcfebfe31140ebcfL, 0xc5ade4d27d907a6eL, 0xa4c576fd470abdacL, 0x9a06630004e2434cL, 0xe5c91ffe2e649403L, 0x79e8cde35d8a01c3L, 0xfe6e6a2eac80c265L, 0x5a906723692cf576L, 0xef004a00e60db421L, 0xa15ee20151bb95f6L, 0x12cb5e8a4c7adaceL, 0x80172ad9c0ed3661L, 0x492c31dc6ad30a4dL, 0x0ac761509dd595efL, 0x9349f08282fd74d8L, 0xd2dcbdbb42c1cfddL, 0xcc21040c1e9d367bL, 0x62b448f73d61c8a4L, 0x567ef895e846cebdL, 0x8238e9fbccabbb44L, }), Arguments.of( new long[] { 0xc705d150b09c1f94L, 0xa828a2ba98ac9c61L, 0x343daac9fd050fdbL, 0xce65de67934aebadL, }, new long[] { 0x86cbfd59d51c2b12L, 0x2015e74ab836e71fL, 0x76c59d63993224fbL, 0x5dfd3a311e5df341L, 0xeb1654b961c05168L, 0xb0085df8d9127280L, 0xc9ec3f9c1ce14534L, 0x44cae5abad7cc428L, 0xaac545985ef4a83dL, 0xbdffb30f032cc9aaL, 0x8309e6733ddef867L, 0x84b62aa79a9de881L, 0x8ac5790bda006d61L, 0x2d66e83835309543L, 0x9a69810f00faad5eL, 0xfb52e0c7e2387ac6L, 0xb7edafdec4002910L, 0x18b3feeac49b7e4eL, 0x43a4f03145408ca5L, 0x9dc51ffbaca77d03L, 0xbb7ad1a943a1daceL, 0xc41841a02d67ceeaL, 0xb88033e5005f190eL, 0x79a40a956ed7d916L, 0xc58bad848a86fee9L, 0x3a311ee2f4d9e712L, 0xe4b32160ddf8e17cL, 0x723d7ac41d2496aaL, 0x96f96a92d33bc637L, 0x724b68f3e1d969e7L, 0x80edfd99dfc0ef39L, 0x7924f19f4d8bfb4eL, 0x4fd956126608af2dL, 0xf81eb1b489993b23L, 0x05d2514191a38239L, 0x819dda6b5dd8eb2bL, 0xb31d2eba66041796L, 0x196d3972d36f36b9L, 0x9ee7893afecfd4eeL, 0x2359a24a67a8e98bL, })); } @ParameterizedTest @MethodSource(value = "getReferenceData") void testElementConstructor(long[] seed, long[] expected) { final L64X128Mix rng1 = new L64X128Mix(seed); final L64X128Mix rng2 = new L64X128Mix(seed[0], seed[1], seed[2], seed[3]); RandomAssert.assertNextLongEquals(seed.length * 2, rng1, rng2); } /** * Test split with zero bits from the source. This should be robust to escape the state * of all zero bits that will create an invalid state for the xor-based generator (XBG). */ @Test void testSplitWithZeroBits() { final UniformRandomProvider zeroSource = () -> 0; final long[] seed = new long[Factory.INSTANCE.seedSize()]; // Here we copy the split which sets the LCG increment to odd seed[(Factory.INSTANCE.lcgSeedSize() / 2) - 1] = 1; final SplittableUniformRandomProvider rng1 = new L64X128Mix(seed); final SplittableUniformRandomProvider rng2 = rng1.split(zeroSource); RandomAssert.assertNextLongNotEquals(seed.length * 2, rng1, rng2); // Since we know how the zero seed is amended long z = 0; for (int i = Factory.INSTANCE.lcgSeedSize(); i < seed.length; i++) { seed[i] = LXMSupport.lea64(z); z += LXMSupport.GOLDEN_RATIO_64; } final SplittableUniformRandomProvider rng3 = new L64X128Mix(seed); final SplittableUniformRandomProvider rng4 = rng1.split(zeroSource); RandomAssert.assertNextLongEquals(seed.length * 2, rng3, rng4); } }
3,090
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/TwoCmresTest.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.apache.commons.rng.core.source64.TwoCmres.Cmres; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.ArrayList; class TwoCmresTest { @Test void testAsymmetric() { final int index1 = 2; final int index2 = 5; final int seed = -123456789; final TwoCmres rng1 = new TwoCmres(seed, index1, index2); final TwoCmres rng2 = new TwoCmres(seed, index2, index1); // Try a few values. final int n = 1000; for (int[] i = {0}; i[0] < n; i[0]++) { Assertions.assertNotEquals(rng1.nextLong(), rng2.nextLong(), () -> "i=" + i[0]); } } /** * This test targets the seeding procedure to verify any bit of the input seed contributes * to the output. Note: The seeding routine creates 2 16-bit integers from the 32-bit seed, * thus a change of any single bit should make a different output. */ @Test void testSeedingWithASingleBitProducesDifferentOutputFromZeroSeed() { final int n = 100; // Output with a zero seed final long[] values = new long[n]; final TwoCmres rng = new TwoCmres(0); for (int i = 0; i < n; i++) { values[i] = rng.nextLong(); } // Seed with a single bit for (int bit = 0; bit < 32; bit++) { final int seed = 1 << bit; RandomAssert.assertNotEquals(values, new TwoCmres(seed)); } } @Test void testSubcycleGeneratorsMustBeDifferent() { final int max = TwoCmres.numberOfSubcycleGenerators(); for (int i = 0; i < max; i++) { final int subCycle = i; Assertions.assertThrows(IllegalArgumentException.class, () -> new TwoCmres(-97845, subCycle, subCycle)); } } @Test void testSubcycleGeneratorsIndex() { final int seed = 246810; // Valid indices are between 0 (included) and max (excluded). final int max = TwoCmres.numberOfSubcycleGenerators(); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { if (i != j) { // Subcycle generators must be different. // Can be instantiated. new TwoCmres(seed, i, j); } } } for (int wrongIndex : new int[] {-1, max}) { Assertions.assertThrows(IndexOutOfBoundsException.class, () -> new TwoCmres(seed, wrongIndex, 1), () -> "Exception expected for index i = " + wrongIndex); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> new TwoCmres(seed, 1, wrongIndex), () -> "Exception expected for index j = " + wrongIndex); } } @Test void testCmresFactoryThrowsWithDuplicateMultiplier() { final ArrayList<Cmres> list = new ArrayList<>(); final long multiply = 0; final int rotate = 3; final int start = 5; list.add(new Cmres(multiply, rotate, start)); long nextMultiply = multiply + 1; Assertions.assertDoesNotThrow( () -> Cmres.Factory.checkUnique(list, nextMultiply), () -> "The next multiply should be unique: " + nextMultiply); list.add(new Cmres(nextMultiply, rotate, start)); // This should throw as the list now contains the multiply value Assertions.assertThrows(IllegalStateException.class, () -> Cmres.Factory.checkUnique(list, nextMultiply)); } }
3,091
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/MersenneTwister64Test.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class MersenneTwister64Test { @Test void testMakotoNishimura() { final MersenneTwister64 rng = new MersenneTwister64(new long[] {0x12345L, 0x23456L, 0x34567L, 0x45678L}); /* * Data from * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/mt19937-64.out.txt * converted to hexadecimal. */ final long[] expectedSequence = { 0x64d79b552a559d7fL, 0x44a572665a6ee240L, 0xeb2bf6dc3d72135cL, 0xe3836981f9f82ea0L, 0x43a38212350ee392L, 0xce77502bffcacf8bL, 0x5d8a82d90126f0e7L, 0xc0510c6f402c1e3cL, 0x48d895bf8b69f77bL, 0x8d9fbb371f1de07fL, 0x1126b97be8c91ce2L, 0xf05e1c9dc2674be2L, 0xe4d5327a12874c1eL, 0x7c1951ea43a7500dL, 0xbba2bbfbecbc239aL, 0xc5704350b17f0215L, 0x823a67c5f88337e7L, 0xd9bf140bfeb4c1a9L, 0x9fbe3cfcd1f08059L, 0xdc29309412e352b9L, 0x5a0ff7908b1b3c57L, 0x46f39cb43b126c55L, 0x9648168491f3b126L, 0xdd3e72538fd39a1cL, 0xd65a3663fc9b0898L, 0x421ee7823c2af2ebL, 0xcba3a4b69b6ed152L, 0x0348399b7d2b8428L, 0xbdb750bf00c34a38L, 0xcf36d95eae514f52L, 0x7b9231d5308d7534L, 0xb225e28cfc5aa663L, 0xa833f6d5c72448a4L, 0xdaa565f5815de899L, 0x4b051d1e4cc78eb8L, 0x97fcd1b4d342e575L, 0xef6a48be001729c7L, 0x3982f1fa31afeab8L, 0xfdc570ba2fe979fbL, 0xb57697121dfdfe93L, 0x96524e209b767c29L, 0x55aad0ebca994043L, 0xb22687b88856b63eL, 0xb313b667a4d999d6L, 0x07c7fa1bd6fd7deaL, 0x0ee9f4c15c57e92aL, 0xc5fb71b8f4bf5f56L, 0xa251f93a4b335492L, 0xb9bad7f9e5b07befL, 0x62fc9ac35ccde7aaL, 0x933792382b0218a3L, 0x7d497d2f7a15eaf8L, 0xb2f0624214f522a2L, 0xd9895bbb810ec3d6L, 0x22d91b683f251121L, 0xc8fe9a347247affdL, 0x3462898a2ae7b001L, 0x468bc3a10a34890cL, 0x84ff6ce56552b185L, 0xed95ff232c511188L, 0x4869be47a8137c83L, 0x934606951e6fcd81L, 0x1ab5e8e453bde710L, 0x6386b61b30fa1157L, 0x97f3a778e242d0cfL, 0xd870d281b293af3dL, 0xc3a5f903a836fafdL, 0x088bd6a24d49cd77L, 0x4e38ddc2719162a5L, 0xf48286b4f22cad94L, 0x080f6f650c337557L, 0x05e6daf6aae1ad59L, 0x7450f7229f336762L, 0xb75b43fb4c81784aL, 0xebd37a514f153148L, 0x0d4b3a39e0bc52c7L, 0x562f36fae610a2e1L, 0x0e0e413e555bd736L, 0xd452549efe08402dL, 0xf2e2ff7be2c75e21L, 0xf2385606c18aaf0dL, 0xdb609b507d8c6b8bL, 0x74ac5663c6c4f45bL, 0x0d84c9a356858060L, 0x19d5b3643bc029b6L, 0x0dd8131e97ffc842L, 0xfa7303606bfffc05L, 0xf98c6d63ff48a16eL, 0x34df46aa2d610767L, 0x83490ef054537f7eL, 0xe071f833e55ebfe6L, 0x0d4b94537ed4a6aaL, 0x3cf85e4e333966fdL, 0xba15364649384016L, 0xc0e6bd623ca72405L, 0xdae6e879b8eab74aL, 0xe4a41f17e70d3e0aL, 0x56e10c00dd580f70L, 0xa9a66bb41781d872L, 0x58e42dbdffe21573L, 0x69450e1ce9674e6aL, 0x47fe345a350aa605L, 0xac958aa80455a5a4L, 0xbc1abca3fbeeb2f3L, 0x08f760d9228900a4L, 0x9e1eb8a2dfec4387L, 0xe91bd1321f5fdc89L, 0xda11a24c514b9dc7L, 0xb1f63d976e0e639bL, 0x41c11098f6123861L, 0x3d7736979f978f68L, 0x0820685b38c926beL, 0x0e8c3dcab075b112L, 0x5e37299d89089ba4L, 0xa1f735eb8235b32fL, 0x2289d719e7b146eeL, 0x1c9c9d0284d96719L, 0x5f8b358546775be8L, 0x317e34c009a07a39L, 0xb16b073eb0ee0a19L, 0x423b36fd459f2a66L, 0x5f45053666f3f84fL, 0x63e7074f03c73d92L, 0x22080cf23288e895L, 0xba4e71bf61dac16fL, 0x9643b3707db2cfb5L, 0x98e2db6c665e7178L, 0xcbc57de0ef3eabb6L, 0x6239a2197582a8a8L, 0xf2ae968e55fda13dL, 0x36e7ac11d1f3a617L, 0x508f0acb609bd756L, 0x6f42d435193a1ac2L, 0x2df2cab9d65e0b00L, 0x4584c1fde5f1ad55L, 0x602c500bdc8317c2L, 0xc80d5b04f6337337L, 0x98abcf971892a773L, 0x5d33cf557e6c4475L, 0x05b5a78be74ccd40L, 0x3ec2cce5290785f4L, 0x2eef1e9c4b36828bL, 0xdd274241a0ce8c55L, 0x3c4cb919b35c221cL, 0xc1fd68d779db9923L, 0x7ff345b4eb7f3639L, 0x804d5881b2eefef3L, 0xa15f9c2826cb34dbL, 0x64822b68adefa772L, 0x761e92f8d279850dL, 0xa5d049ab1061dba3L, 0x5f46fb02d10d2219L, 0xc1cdaa5f9ca79b19L, 0xdd713a74701ebe60L, 0x6b626ec963951798L, 0x1d3ec8d78b96d16dL, 0xdb885d52960e7f34L, 0xe39849cf3ea178f8L, 0xc1e37acdf807130bL, 0x3645880ebf333913L, 0x3af81a7bec346c22L, 0x871c563e94324804L, 0xac55fb5e2817db4cL, 0x035b04c42565ebe2L, 0x5094cafab11cbc3aL, 0x94d40a57481346b5L, 0x0f91a8492df723e3L, 0x126a70b84f779accL, 0x4409e9a5d5c3f133L, 0xb1655339864151efL, 0x6564e506d11e9de1L, 0xd9a06f7b8860b488L, 0x0d493e410b68b6c6L, 0x4e6fbf4b3b985a07L, 0x71c1b0ba9e52a2deL, 0x5775784ad3cb99d9L, 0xbab84cec312107a6L, 0xd9066f5ccd59cf9eL, 0x8c656651dbb3ed84L, 0xa7448d0059484568L, 0x2819237e5e8cb03aL, 0xd57eaf5239931b4bL, 0x6cd436fd5f7c1e73L, 0xf03b845f2a584931L, 0x8847b9f1f2d55b7aL, 0xd49a38f8e59db8faL, 0xd5052cc47685dbfaL, 0x91e060fb399ecf2cL, 0x5748fbea12dd2869L, 0xd0cee85adb889226L, 0xa35e9dfa5a64f56aL, 0x3118398bd0e3cbf0L, 0x5e650b9a3cb34308L, 0xf575ccbebf49b91cL, 0xb3f8dd73257c80e2L, 0x13d7954e8294819bL, 0x90b57ccd00f1591dL, 0xa8b13ef52ca7e284L, 0xe482d24e5b239833L, 0xb0648475f2b4d63fL, 0x847e8fc889e2389bL, 0xa8b501ee1fc59ba6L, 0x29702f6acba4379eL, 0xfaba9600f3d2cd0fL, 0x52ed7d9f45c6b578L, 0xa02b167546d6e2d0L, 0x9a41cb166618a390L, 0x83d464d7349d9f3aL, 0x805485c9d2c70c6cL, 0x332f7ce3e077ecceL, 0x1ead372c068ebb04L, 0xd6866c5b3b5eb82cL, 0x5b057c64bda94a33L, 0x11e1388f59653c66L, 0xffd2aca771c0abb8L, 0x2fabdd0e8e8ba606L, 0xe974ffd57219e5e6L, 0x2b4e5c1e5f98e506L, 0xe7819b2cb44db4c0L, 0x6cbd78c408376520L, 0x244285f39307f083L, 0xd152927f3a3af853L, 0x5b799885a8ba66e3L, 0x9300da64ea1a0644L, 0x67399bf8688a0cabL, 0x047c301af4a94bb2L, 0x6750ecdf35c8471bL, 0x7598ae5c876d4080L, 0x269e0cf307467534L, 0x2ef4d8dcaedbc549L, 0x2c6983c911c958afL, 0xb2fd7c07ae0bfa60L, 0x3220a56d67450e80L, 0x7385883420eb9f69L, 0xdb1fd2951f15b047L, 0x0f08b35df55977bcL, 0x42939b9f2e58127eL, 0x4d1d77e72414aa9aL, 0xfd8137f8b59bd084L, 0x167cc7f46a511504L, 0x0263de0c6b50290dL, 0x2a1c2feb488ffab8L, 0x1194815038360d85L, 0x36374630d0ecb4e8L, 0x609d38e22c59a974L, 0xee23867f7c9b5f54L, 0x40e53a7804b0ef15L, 0x8f287f1a3be6e458L, 0xba7334b0f0af9e75L, 0x09f003e8e0e9c6c0L, 0xc02dd0d35c42bc56L, 0x63dca83acd6be04aL, 0x9617b58a79fdd826L, 0x563d25e6f891bb33L, 0xe3c3d3f3f6d58588L, 0x359977baa315b1b4L, 0x0df431301d9e6bc5L, 0x4074bb10d0003ca5L, 0xf440159140f2b453L, 0x3a6cc6e14820f5e2L, 0x4b352bdacf3a37c4L, 0x9ef3b8df89ea4c29L, 0x8a1b2495a1414892L, 0x670b7f423f78b7c9L, 0x7513c7ccf6ee3c3aL, 0x9ba96cb53c24408bL, 0x3316c3dc4ec859f0L, 0x501337e1a7f1e218L, 0x9a1544a6029c1eb5L, 0x9b43b15859c3e96fL, 0x058011e22698bd4bL, 0x589b8eaea60d54a4L, 0x68ccb8c6cd7ec24dL, 0xe55beb5896455705L, 0xba6069cf90a8f1e2L, 0x896a18c3eb90a6d6L, 0x870d3d80ae0b9323L, 0x48688d8b457f501bL, 0x8f1a8c1b84b3ba62L, 0xd1b7b64dc136f6b3L, 0x3c6a7025428547e9L, 0x199bcc50a190fb6fL, 0xa5de0eed4bda0979L, 0x31041667821cddb5L, 0xe9df34e2678fb4e3L, 0xdd7222eaee54e524L, 0xaae6488b26c7af56L, 0xe8a560dbbd2eb6b3L, 0xe37c99a7f5defceaL, 0x1572be8d78f3afacL, 0xf69ffb64131633aeL, 0xa837ae34963616fbL, 0xaf0a9f03d137c170L, 0x0d3e02b464018a48L, 0x11753aea03bb0ecdL, 0x32d9cca610dceb34L, 0x2622bb6a6e7a11e3L, 0xdc99a44c515ec8b3L, 0xd7d720ad0a770b28L, 0xe322f742d6d051c2L, 0x745f2b6e3ea9cd2bL, 0x951f21478e6b9662L, 0x227f21d8c0713385L, 0x0cb729235e6876eeL, 0xd323b857d9721a53L, 0xb2f5f599eb743346L, 0x0efb30babe65a283L, 0x94c1190da321d470L, 0x117c63209fba9efeL, 0x738cf92baa4bc2cdL, 0xc3bdd29b33277165L, 0xd78a2fab38f6dc46L, 0x35c44aafdefb91e2L, 0x321e26bf321fb60bL, 0x12db436288b37172L, 0x158a2d49e51c261aL, 0xc9202ac8ba71c873L, 0xd02fa93dc97cc7b7L, 0x1f8bd11b747e901eL, 0xf7a17b2f74aa321cL, 0x284d02d7552a3e90L, 0xeb86a8251533c574L, 0xb3fd774eaf4e77f8L, 0x31df2951c3ff37b1L, 0x86e38546195a69e7L, 0x6737aaf165a1389aL, 0x2e2e925079feee0fL, 0xf7bd5a988596c1bbL, 0xccf835db6a10d2dfL, 0x6f42700f37c94701L, 0xa6e86f7ba2779a5cL, 0x0a43a4f7036d1ec2L, 0xd798bd6d52ad26daL, 0x218f6912af38b818L, 0xd48684f266f2e186L, 0x8f675048b7b012e5L, 0xe5e469aac68eaf1dL, 0xe2740035697de79eL, 0xc22d6bd6d08baf1dL, 0x341774636bdc8f41L, 0x7dfc6b73f7ba322bL, 0x7566343607bb525bL, 0xae94d116ccf1e74bL, 0x6ca1b59934cc2697L, 0x4c2fb1c45b749cedL, 0x989999bbdd2ec893L, 0xcc4e27afa81bc8a8L, 0x6ddadf15ebf85830L, 0x38fa9cb2ce72bc16L, 0xacdaffe39db4bbdaL, 0xbcc4682ebd095d93L, 0x483f539d354559adL, 0x45de92e997e2915cL, 0x7ec5c881c5344a55L, 0x9d1844fffa091545L, 0xcd9b08d4dcda27baL, 0x1f7495a5f36c34ceL, 0x4f0fc9647d99afd7L, 0x5ac375ec59321cf2L, 0x5c07ce6df7e1d9a2L, 0x49f211880d688b4cL, 0xf85fdd8ccad0867aL, 0x7d510164d8f197a8L, 0xc64108c5732cfa0eL, 0xb262d660d3a2c648L, 0xd5d5614571dd2efaL, 0x0ec1a6d0dd5d5391L, 0xbf07d939d2535f02L, 0x035bce3021e51045L, 0x423ae115ec99d154L, 0x22ea1d3abd893838L, 0x517fc1107eaa6a83L, 0xc5967cdf353aeac6L, 0x096ae2c3dff65ce6L, 0xab1b908b97dc911bL, 0xf3d84c286f22611cL, 0x256823815030d8a2L, 0x3bd9b119887342e4L, 0x59926f3401f437d4L, 0x74edf41038d3986fL, 0xe2b35bf615038253L, 0x4d09740a6b44db89L, 0xa37edaf089c0eeabL, 0x8263ba2c23e2d62dL, 0x8784aff57d390c3cL, 0xfb49ab0635d66c3aL, 0xdeba73c2562bff1cL, 0xe2e6bf8cb6e29717L, 0x0e70431c63d86e46L, 0x20d717e16aae6010L, 0x031af57cdcf2cd36L, 0xd55fbeef1c5357feL, 0xd361d871f4e393feL, 0xb3416e718d32d214L, 0x7b351f93f909fc00L, 0x16916de7b96a26bdL, 0x4fec1248b5dedb65L, 0xfae1aa9a62bfa096L, 0x92e7910a6b0084a9L, 0xd12bba8672e8aaa9L, 0x316558d69efc8f6bL, 0xb0dde29eb96fee87L, 0x2125a2be5bf67eb3L, 0x5905903f46531fe4L, 0x2a9927e8175ff60fL, 0x794376f2bb5d6d96L, 0xfa9f65d2b4848b12L, 0x2b92665a263a5091L, 0xabcaccfab8464c65L, 0x05b2fb2a46d1a0bdL, 0xa879920d28c0d54cL, 0x50394088a8ea4953L, 0x61b0c87f0084129dL, 0x29ebcd1078d6e2b0L, 0x2440c652f6bacf89L, 0xbd74d596cf4c8eb1L, 0xe4b009e5c334766bL, 0x07db26843cf72cb8L, 0x4171d5edd5468876L, 0x608d5c2c348c143cL, 0xa19e0a2b81da6eb5L, 0xb65a7be9354c1390L, 0xf4f4c437cb9bb324L, 0xfc24806650c823bdL, 0x4c2331521e7f2966L, 0x54f66e42eb73bafeL, 0xf06c11f3d2fe29aeL, 0x8ba8d0f28cbb0fcbL, 0xf3617850d1ae7976L, 0x96463b47cd9a7286L, 0x8edc2133b35c3779L, 0xae43f70f181d9371L, 0xe7628d75c9a3c2e7L, 0x978499ba4193b333L, 0x99bb4bf79b0a46c1L, 0x4c52676d7e4d0a58L, 0x2064ee3910693deeL, 0xfc43514d16633997L, 0x1bc1741ce05c4cceL, 0x6e9588d40f3396f6L, 0x146fe816bb3a3708L, 0x2b3db8ee05eefa87L, 0x6ec21a91189ec0c0L, 0xa8a907b34108faf1L, 0x708b80912235684fL, 0x2bc8ba70edfe680cL, 0x4d118826481266efL, 0x8f93a3a5de887175L, 0x3308e9133a950fe3L, 0x939ed8b0d7e91f87L, 0x666beee64002b6b0L, 0xc8f129ec69ce7811L, 0xd57593c68ce93ea0L, 0x02d6a3e66edcc862L, 0xbe1d00d16a2271a6L, 0x34fbeaf95e0c673fL, 0x9845ab59483a0e86L, 0x257d47d5abf88962L, 0x28af39f39319545bL, 0xe3fce03abd8171eeL, 0xa4c5f606dccc96f1L, 0x4d414846267c4962L, 0x6ccf77f81d9dbf70L, 0x947bf43c729a71ecL, 0xfd656c39c4fa824cL, 0x8f652cf2d1e04fd6L, 0x8cb11929a65b6aeeL, 0x094948f16a8064daL, 0x7434e703a4d03d5fL, 0x9361d3f63af4aa35L, 0xa998c1eeec3fb422L, 0x51eb94754b5992a2L, 0x6e109c0347ef6979L, 0xe3c9738d67c582e2L, 0x9c735e3857ec57bfL, 0xbe6415659e12c64bL, 0x73924584e31b9099L, 0x8f676821e60b0945L, 0x5614e3a695d5289dL, 0x7ecd448787517ebaL, 0xc96db02038dbaf5bL, 0x069299ed774fa6c8L, 0x0b4ace5a8ea16ac8L, 0xbf2f4f23a6c92295L, 0x90bdc4f1e931656cL, 0x7cd5b0b95ac34d3aL, 0x2032bc59d3dc1710L, 0x702c1a0cd5609379L, 0x609d33abc01ff3feL, 0x8ae5d8f283b2748bL, 0x2cf3778fa7eaae1dL, 0xe8a0d7b1919df9e3L, 0xe487894f6d602a0bL, 0x929858549609626aL, 0x46e540cd86bf46e7L, 0xd1daf4382128d9eaL, 0xc47239c06b22ef75L, 0x8b7aad8ffea1b991L, 0xd6c1d2e315273fa0L, 0x2fda11cd74177e6bL, 0x333cb0a145919fd7L, 0x5970b31a49f37b16L, 0x7890bc68793bb959L, 0x2a060f45a1719347L, 0xeb298f0264bf379dL, 0xd7c4fd7921707400L, 0x374635e7713ed165L, 0xc60c008df0296d05L, 0xbf13739a8d3c7dbbL, 0xbfb945ef1cf94d1dL, 0x75fe953c3a3a8315L, 0x09f83064f4150c02L, 0x6784a3b452055343L, 0x73ed26d185738f51L, 0x6c59094e8c998390L, 0xeade93e19d60d4b5L, 0x8cf7cc8e62bc869dL, 0x22f85626f7f69298L, 0x6679c449ac22edc0L, 0x7017d0003e897435L, 0x308fc450a6c62bf8L, 0x2578b45bc6f34cabL, 0xcbb936c9d253db39L, 0xc4e70e5bbc5e002cL, 0x29db6985be6c9459L, 0x96afe876f5f6250cL, 0x829f766f138f95e6L, 0x4369632017c8fa0dL, 0x0da90c817ca890a2L, 0x38d160dd675e2376L, 0x20df15ad986408eaL, 0xd192623c3d9b3f41L, 0xd846f79123baf4aaL, 0x6cb058a0edfbf056L, 0x1b192f0be8dc77a7L, 0x3a11b3dfcc81a441L, 0xe914410093ad7767L, 0x3126257e578bdf60L, 0xd5d5e470410cb6eaL, 0x4e1bf5d4209248aeL, 0xe1e4c2924f35192eL, 0xac9944825cb7ef5eL, 0x8d2cbe6996eb3475L, 0x1bc05d2a079592d7L, 0x564a9f06755e71f3L, 0x9bb767d68e9f2537L, 0xe4b045acf13978a2L, 0x4b7519cb9028ac83L, 0x9df655284198b85cL, 0xdc32ab4d421a2b61L, 0x4c5d7f5323c1960cL, 0xe4273ff318f5c7b3L, 0xd73ef5ea88a3e99eL, 0xda2ffb6a863c850bL, 0x9555a4144e05ad82L, 0x950104dc15092ebfL, 0x39d121a61f19dbfeL, 0xc6804e29d60d7814L, 0x7e98bec5ba17d58bL, 0x8b2c6b0e6c3b749aL, 0x0301a07c84aaccdbL, 0x93dee719932225a3L, 0x381611a50bac0ae1L, 0x572a8816f6e407b4L, 0x0420efe85aa75232L, 0xc1f53f78b9ffcf4dL, 0xbeda53bc95b96ec3L, 0x9f357114059c8eabL, 0xe38239260b584150L, 0xccbca17f4eed2ad0L, 0x1528080b61f54198L, 0x5b8cdc4c40d49f30L, 0x1617db48eb6640d0L, 0x6fed27f88a516c99L, 0x37056e05b4724179L, 0xec7bc122da9538d8L, 0x9fbfe01ca2c0fd57L, 0x2fc96b31dccafd9fL, 0xe26a72009daa1249L, 0xe9fb2e3998d16a25L, 0x4a87dc39d24133aeL, 0xd5340e98fde806deL, 0x272b62b5dd0d7fe3L, 0xca4625581bf9dbe1L, 0x8677af77de374a90L, 0x27dbec9e28f857edL, 0xb4aefc44d036612aL, 0xcf2e8ebdb0f6bb11L, 0x76023506c94e0532L, 0x864e72d4488c7a7aL, 0x0b81058fdac18fd3L, 0xdf93ee5b6674a0f4L, 0xdb30565511789d77L, 0xcf5fe22dc0375f30L, 0xa6e62e6e4edb4043L, 0xbce383957a728669L, 0xfe4dd4e9633db2c6L, 0x24e68818b2a6d6cdL, 0x048a89c5424b4cdbL, 0x7fc7bc75bbbe5768L, 0x79596343191e0ff2L, 0x5510b9cd8306839fL, 0xff2668b4eae7bb53L, 0xb4c03e6363c9e244L, 0xc9e3c0c1c015eb6fL, 0x52531f5f898a744fL, 0x0484005b2a805083L, 0x31673b70c6f23c53L, 0x5bffe158f323a7c2L, 0xc742bc0d0c55f125L, 0xd95c32fe7e18379dL, 0xc1f2f613ee3c2e21L, 0x3217a43ff0daaa0eL, 0x3a9fa27258257e53L, 0x80b42af5a393bcf2L, 0xb6967fd6a302f65eL, 0xfdc07bc592dbb125L, 0xcb83b8b9f64c3c3bL, 0x9cb572b041015355L, 0xc12dc512aedc530cL, 0xc8db824276c083ceL, 0x86923b0e2903627fL, 0x1385cf2be22827cdL, 0x21b7616ced869ef1L, 0xc74d497d079901e6L, 0x9e03c843bb13f658L, 0x915b89077a81ec7fL, 0x288a10b00768d244L, 0xd88eb6745a557569L, 0xb3c98071a3d13b20L, 0x8f23aff44d352f03L, 0x2bf39ca10e45bdbbL, 0x0f1bea47e2c68a4cL, 0xf8d5ab01c1ad6b55L, 0x679e0601953d1e31L, 0xd793f3aacb3c520fL, 0x96fc350ccdb76eabL, 0x9fa0178362df447bL, 0xc11c63febf83598aL, 0x3aa88df3a1a71323L, 0xab2f8338a09ca82aL, 0x32a2133050a71357L, 0xffecf97ca3ff65e1L, 0xfb6fd13318f5cb79L, 0x3acf76875acad366L, 0xc577ffff529f74cfL, 0x368a90182031dd12L, 0xafbf2311ad656d52L, 0x80cd4f9f23fcafddL, 0x451717a061972d1fL, 0x0bbcbdae779cfbf3L, 0x133ca541293fd40dL, 0x6f241a21fc40b108L, 0x9adecbcf0c28110bL, 0xfab528d93bac6d3aL, 0xf4ea3d459b0654aaL, 0x7e2e9ef35a5aafbaL, 0x28730469eded0fc7L, 0x3cbae97a12632fbeL, 0xded6960c0be007a8L, 0x2a11758a7c52c43eL, 0x289de4875bda262dL, 0x6e13eea58caf3fa7L, 0x020c8ed0d5d673c1L, 0xdb4b3e7719d523b6L, 0x49143c819d111fc3L, 0xe07479f9ddf45d8eL, 0x68f4654bcc07435bL, 0x513bd537af510064L, 0xcf956c3a3933ba38L, 0x97e1eaa33f88eeccL, 0x18be860a2504a1c5L, 0x84408412fc0bf397L, 0x0b6bdba7e154bdf7L, 0x1d8f8b446b544be6L, 0x6f06b3dcdef17a03L, 0x30c6e14df59f8cefL, 0x01c97ba9910219cfL, 0x33ddcc087d1aeb5cL, 0xe31b94300cfbcbcfL, 0x0adeb8a98786bb28L, 0x3f69d5b0e3ec8f17L, 0x99f5a15f635296a6L, 0xce9fac7526862e86L, 0x3a88964201bd7524L, 0xec94d643ea71be51L, 0xc4257084d97ab1c5L, 0xf369b10a73b4d382L, 0xac02bb473dbc5fd1L, 0x4fe73a86d95d7222L, 0x858806616fe3d553L, 0x10680debcb0693e4L, 0xcaa9aac77c954093L, 0xf29c7530415d71e5L, 0xc32b319e09de9e48L, 0x1c67107ed497ebc4L, 0x731da71593324021L, 0x49774770588c055aL, 0xf978dfdc28084220L, 0x058b3f2780b5a7ecL, 0xe4ebf2ca21410715L, 0xd3841ed97708421fL, 0x0dbc9401dc51eb4eL, 0xf47a96de499aa2e1L, 0x224da94d8542ba0cL, 0xa3426a80b4dd0a4aL, 0x857caef48ef7e5b6L, 0x11356ad6ede44bf5L, 0x1a32471bd26acd7aL, 0x199396e31de7b358L, 0xb7ca7950dbbb4a92L, 0x6ab23720409790a7L, 0x2abfdb93a3159d10L, 0x23913b403946c4a7L, 0xac7c9f339a822344L, 0x12cffe9625cbe744L, 0x89558b98548b1946L, 0x77be65945c191139L, 0x3ba8d1fc701f4347L, 0xb143664560327f20L, 0x48baccc3ef2081ffL, 0x450c379d24beb8e9L, 0x1990b609485db827L, 0x6c6a565d7129ccb4L, 0xf9724a82872bd619L, 0xcfe629aa56717e20L, 0xfde48d87e844ec93L, 0xb32f79e5dc9ce4c1L, 0x7c9d88364238519fL, 0xe943aceba65150f9L, 0x5301e8550cbdd076L, 0xabb8392364453b3cL, 0xdfb4b4a3cf84aa2dL, 0x269e45f7a6b48a42L, 0xd6783043ab383fcfL, 0xe4ec475d296a69e5L, 0xe2e273ef65555361L, 0x6bd3084210a75af7L, 0xf2ebc493b909d8c7L, 0x4d20f3d435e9bc94L, 0xa465e41c3c36d433L, 0xc1b259456f4341c0L, 0x260093703d6cf2ebL, 0xda68d9dea0aa9bdaL, 0x5662a12a210b2a47L, 0x54675bd1a1b4b467L, 0x9dbd416302ec2468L, 0x3c7130a5032d823aL, 0xabfdef2d9a4fd92fL, 0xd4034e276021451fL, 0x13834d3d0e43ab73L, 0xdc181442b438b2d6L, 0x1736ffb392c25e23L, 0x289b94003a946252L, 0x99705629b221ca37L, 0xa7b22a5bb26775d6L, 0x2dc12f9f04435661L, 0xaadd48b556bc9e7cL, 0xf6992e8e94b68a49L, 0xd50420466c9456e3L, 0x0ea8305ecdfb1266L, 0xfe0b1d7e4f0ef297L, 0x563de834c4e56a46L, 0xc62b8099b5b264c6L, 0xf6e76aeaf533c784L, 0x0d4680470b790968L, 0x288a50754707431eL, 0x8ad167ed38df547eL, 0x9052fed81a8ca4faL, 0x5975ac56f0548ef1L, 0x588bf7d0130111aeL, 0x9ec02036a6688a24L, 0x8c9a454af9e09984L, 0x333ee6727bd12dcbL, 0x9847468f925dc38cL, 0x446ed5203696abacL, 0x71fddf9ef5b5def6L, 0xd4d61614cc333541L, 0xd08a0694cd7f72a7L, 0x686cfe3ea1889281L, 0xf039404e0dd3333fL, 0x052c620eb18b4246L, 0x4e4de47f86d84713L, 0xfe0450396b209851L, 0x99d6e893b01ed92bL, 0xd94cf8705f8eba86L, 0x763451110c00291bL, 0xdf4f60b9aa45d064L, 0xf473d4bfd86ad526L, 0x41b9e3fa1a6dba94L, 0xbaa7cdeb00796a4aL, 0xf668194c40626450L, 0xb894e0ae40a9c87fL, 0x5bc1eea8587d3ddbL, 0xc4c0ecb91bb50d75L, 0x819fdfd17ff2917bL, 0x681484e54b6b12f7L, 0x2f510aa2f8977995L, 0x7d1582a293b8fa3dL, 0x3dad5a0f0da45470L, 0x33c113aefb480520L, 0xbd524b2da7ce6c1fL, 0xe4cc051d00d8ddc0L, 0x2995950e206efa90L, 0x8b0e5dca588e3f50L, 0xaabb3583f7f87082L, 0x75dbbecfa34cb4d6L, 0xf195977068849ae8L, 0x9223ca6fbb72767cL, 0xda7211029d59f04eL, 0x18d9987c6566405cL, 0x57833aa39ef75a04L, 0xd1750e36481f654bL, 0x0ce2b66bc8796acbL, 0xc7e79aa76c96b057L, 0x68f95b6b3c5cdc1dL, 0x2f5725cf5fc583aeL, 0x6b973013fd4484a6L, 0xaaeb2687f2d8bb96L, 0xad29cce061ba3934L, 0xcb60dd1c437eb1d9L, 0x5cd6f46b78181bb4L, 0x1561cdc95ace24d9L, 0xbb774e6705806245L, 0xdc29c8df29b2e975L, 0x6ee5ba502839dccdL, 0x670869bb64c60f69L, 0x008ca2931e927ae7L, 0x35cf6c0a27d8de77L, 0x94a3d86209af3920L, 0x4095a276475df5b7L, 0x1119e4c257ccf7fbL, 0x33376166d9064fe8L, 0xd68c2399f968b905L, 0xb7bf2902f40fc101L, 0x4ec18604cfd551e0L, 0xeb8e7fe1b6678e99L, 0xdcfe68fc0e042fa6L, 0xd2e58dfb1a8e3866L, 0xf4322bc57fb9a35aL, 0xe0c665c8cf1fb49bL, 0x60de1f1050684297L, 0xf400c04cb00784cdL, 0xfc2a216f12016984L, 0xa808b477fd65fb4aL, 0xd9b614adfcb5d0f1L, 0x50afdbc66e3efad2L, 0x82337b3f1764851cL, 0xdc98850eb93ef45eL, 0xe1c314bbc2c6af27L, 0xd35614ba27e74a71L, 0xa5d592e04a31bb6fL, 0x3f143cd0bc243fbeL, 0x081641ac25408b21L, 0xd4166b32a26fc1c6L, 0xccd088ee4d4a1f67L, 0x698c913d46c1ec99L, 0x0f6b9086a5b986abL, 0x4a73c05ef72e3595L, 0x0307aeeb350ab081L, 0x43e20045bcb06b0eL, 0x3f58d1d6cd3aa0dcL, 0xd71cd7c996faba80L, 0x4431d8268eebfb71L, 0x254246df109e3dd5L, 0xa7ca1449a238b06aL, 0x49b40e7b082493e7L, 0x45d80e6bd330d613L, 0xaf3d8a578b6d6232L, 0xa4b98341785262d1L, 0xf4f1f424af963102L, 0xa84a986395146774L, 0x90da037fb61d5c88L, 0xb645534b2cf5b89fL, 0x3fdb3073310934b4L, 0x1a0307d01f57f514L, 0x509c9b87a4a1e66cL, 0xbf320cf0888d8aa8L, 0x45a51f76c5f76892L, 0x23eb7a2b99a64402L, 0x4c600e5675dd7757L, 0x4896757aa01a5c34L, 0xb808dbbda7a8a1daL, 0x762c21058ba50349L, 0x99b0a9d5deeebb37L, 0xd6d98ef70a1e465dL, 0xb052f2c1163894f6L, 0xc55e73526f8bc8faL, 0xb31a0537f5b3b269L, 0xf09c1819c0c7f78aL, 0xc36d4e2187e430f8L, 0xf141831a47299c7bL, 0x62f938047903ef34L, 0xf2a0dd678f92e0a5L, 0x0c7fe6a53efaaa65L, 0xcc539fcdcc466310L, 0x55199357cdc55491L, 0x6917fb45babb399eL, 0xb098da3c7b012b1aL, 0x054916438f426c41L, 0x1a5ff3356d77d43bL, 0x74e71995e0aacf1aL, 0x6562a8da6b5e69eeL, 0xacbc2b8d1fb16ea8L, 0x400ea8e1f3f5535eL, 0x2ea792dad3a4538fL, 0xf580fe481db60b5cL, 0xcb101198dd0aba9fL, 0x259acbe0461cb837L, 0x30033c3964b56a40L, 0x6c15d4283eeb6fa6L, 0xdea7b626998ea3eeL, 0xdbb2e1b8c0c2abdbL, 0x3a856a6742b6edc2L, 0x0777ed6b1683f48bL, 0xde72fd7d6db3bb63L, 0xc8766969b599dc68L, 0xb39a5b76dce26160L, 0x97464948ce81d8a2L, 0xae20fda5af404ae6L, 0xde1100c4f1ae3265L, 0x05a94d43bf60f574L, 0xc087a2116f52d0fcL, 0xfacb3be87e615d89L, 0x0e184cb9fba7b0feL, 0x824779bede6d84ffL, 0xa0852e96875da152L, 0x620046e8ba89baefL, 0x247c32c5f34b08ddL, 0x49294468356e7298L, 0xaf6d6e0f8b5009ebL, 0x8c25bfcdb8abd77fL, 0x4f5464a1bc417e38L, 0x2df8fbe8993f8c9fL, 0x6540566281dd6d91L, 0x0b90690dcfb03a83L, 0xe270b7c7f8fab463L, 0x898ead41792a7f87L, 0xa1b1248822b7c292L, 0xfa2c0d61dd383eabL, 0x5574c091830bd677L, 0x43640e20702986e5L, 0x622d0a1c860d0302L, 0x9528ea0051990eb9L, 0x28f057ef30af388fL, 0x88320e974a2721a0L, 0x8a12cb33cdd88b60L, 0xd91a9763f991780dL, 0xdf22e332867c0e97L, 0xad95801b6c801f10L, 0xb34e21d4afe2c4a0L, 0xd5465bf172494dd3L, 0x16594af34f1b5767L, 0xa675dceaee1591e6L, 0x53db891db5e1d768L, 0x39a80f5d365c71afL, 0xdce01c73eac54372L, 0x1087fb03e5ce69e9L, 0x067cac3905594378L, 0x275d24c9aa1607f0L, 0x9163a77a53e361b8L, 0x17d10f8254fa7f0bL, 0x49efeab6642e9e45L, 0x376e24839b1df1beL, 0x0c46221cc408546fL, 0x98eb5bb7001ebf5cL, 0xc6c4d56e3c9a78efL, 0x023c0723e123a899L, 0x145912ec44b57548L, 0x488a34fe824ff4c3L, 0xac3bc6de9929c707L, 0x1dbac6e98813a70fL, 0xf566054941858266L, 0x18e0a3a2a8b8f2f1L, 0xcc6245a26564a399L, 0x14416ca0e1a84a9aL, 0x4eaf095631a6e7bfL, 0xf2f89f104c9d0b8dL, 0x8fb278a5953e52d8L, 0x8fcee83a30a8be30L, 0xb66850da1a0ceb33L, 0x5f37d31bad76f4dcL, 0xff4d956ffea8dea4L, 0x078c583b396635b3L, 0xad268fb5b1105028L, 0xa480149a0dcbc5f4L, 0xb0e8d69c8b15c864L, 0x6ed49c46f19bb8eaL, 0x7f1871fdf321818dL, 0x1ec5816f5a9843eaL, 0x77c8da91b5313675L, 0x4cdb66ad515e0717L, 0x2ec4712b0bfdfcd6L, 0x6c6f5767fff27330L, 0x071083b972d80c0cL, 0x8d8325e82c4fdcdcL, 0xb47a658dad8e13a4L, 0x88710bf005fda027L, 0x69bd3edaf7111200L, 0x0dccdd0c65c810ffL, }; RandomAssert.assertEquals(expectedSequence, rng); } @Test void testConstructorWithEmptySeed() { // An empty seed is allowed final MersenneTwister64 rng = new MersenneTwister64(new long[0]); // It should be functional so check it returns different values. Assertions.assertNotEquals(rng.nextLong(), rng.nextLong(), "Empty seed creates sequence with same values"); } }
3,092
0
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/PcgRxsMXs64Test.java
/* * 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.rng.core.source64; import org.apache.commons.rng.core.RandomAssert; import org.junit.jupiter.api.Test; class PcgRxsMXs64Test { @Test void testReferenceCode() { /* * Tested with respect to pcg_engines::setseq_rxs_m_xs_64_64(x, y) of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final long[] expectedSequence = { 0xc147f2291fa40ccfL, 0x8edbcbf8a5f49877L, 0x61e05a1d5213f0b4L, 0xc039f9369032e638L, 0x95146e605b2e4a96L, 0x5480af6332262d03L, 0x7cbfb3a67a714557L, 0x5c9f0a25eba41575L, 0x6e23dba403318decL, 0x7b230e581b829dbcL, 0x0617d61457cce844L, 0x661c9bd85d60eb09L, 0x48acc16a0113d8f1L, 0xd7c6a4b516ccf126L, 0x85e72ab4e1819cfbL, 0x578cfcc9d7f55036L, 0xcfc87f2b332b581dL, 0x50db098980bb8bf4L, 0xe50bab9f780884d5L, 0xbcd91f1aa5240febL, 0xf45c3398207f4f55L, 0x390a6132c0e31e56L, 0x0594d18864e34a6aL, 0x10268ae6df979e24L, 0x69bc35b3539195eeL, 0x4931c30cf8c9342fL, 0x63bc344007b30e4aL, 0x6da4c43e42bdf49eL, 0x9a52bbf48ebbcd52L, 0xd7e0040b82efa84fL, 0xd601c8e7917a1db9L, 0xb905ecaf6864e799L, 0x32877df7625ae7b5L, 0xa3dc41742161f87dL, 0x9556e15438c1aca1L, 0xb2c890ac0e32cd37L, 0xf1d53427ff980d09L, 0x0e227593be626d22L, 0x0fcbdacbf19d6ae1L, 0xe425b9f0345bd813L, }; RandomAssert.assertEquals(expectedSequence, new PcgRxsMXs64(new long[] { 0x012de1babb3c4104L, 0xc8161b4202294965L })); } @Test void testReferenceCodeFixedIncrement() { /* * Tested with respect to pcg_engines::setseq_rxs_m_xs_64_64(x) of the C++ implementation. * See : http://www.pcg-random.org/download.html#cpp-implementation */ final long[] expectedSequence = { 0xa5ace6c92c5fa6c7L, 0xac02118387228764L, 0xa6e796e49dc36e00L, 0x4713f32552134368L, 0xa2ad36cb4e6b7cc9L, 0x6bbce7db898fa11dL, 0x134cb18300fe9eb0L, 0x3f705c0d635cbc23L, 0x4bd7531b62a59b62L, 0x413cc95f3c3e9952L, 0xbc77749b270d987cL, 0xd2c74089bc6489f5L, 0xc2debc07a31bb1a8L, 0x5163cfcc77ebd4fbL, 0x6f41b5621cba1b2dL, 0x72dd618ae82f792fL, 0x76888898287eeaa2L, 0xf5c7de46ad2739a0L, 0xc9d63bfe7b405a66L, 0xefc0161a3119efd0L, 0xbc7a7e23220b53c8L, 0x6efb5e3e2b510988L, 0xe70ce3d64ed4ee82L, 0x3e5c15687252a94dL, 0x95530066e3a7f3a6L, 0x6c9a303ab74d9a21L, 0x93ff7e36cf46cdeaL, 0xd5173d3428745856L, 0x4fb30e4c6e8bf68eL, 0x6466bbcaf078ad4fL, 0x846768c1bd451c96L, 0xd9c7d6b4aabce95dL, 0x4f789941d453a26fL, 0x802e40798afb9cf9L, 0x4f9fd27240f2303bL, 0xb9c25cd3e029e4eaL, 0x7d8ecedb3334b077L, 0x9011d404cf44b7c7L, 0xe6f26367c52b12a6L, 0xcbfb6a1dd20f2df4L, }; RandomAssert.assertEquals(expectedSequence, new PcgRxsMXs64(0x012de1babb3c4104L)); } }
3,093
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/RandomProviderDefaultState.java
/* * 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.rng.core; import java.util.Arrays; import org.apache.commons.rng.RandomProviderState; /** * Wraps the internal state of a generator instance. * Its purpose is to store all the data needed to recover the same * state in order to restart a sequence where it left off. * External code should not try to modify the data contained in instances * of this class. * * @since 1.0 */ public class RandomProviderDefaultState implements RandomProviderState { /** Internal state. */ private final byte[] state; /** * Initializes an instance. * The contents of the {@code state} argument is unspecified, and is * guaranteed to be valid only if it was generated by implementations * provided by this library. * * @param state Mapping of all the data which an implementation of * {@link org.apache.commons.rng.UniformRandomProvider} needs in order * to reset its internal state. */ public RandomProviderDefaultState(byte[] state) { this.state = Arrays.copyOf(state, state.length); } /** * @return the internal state. */ public byte[] getState() { return Arrays.copyOf(state, state.length); } }
3,094
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/BaseProvider.java
/* * 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.rng.core; import java.util.Arrays; import org.apache.commons.rng.RestorableUniformRandomProvider; import org.apache.commons.rng.RandomProviderState; /** * Base class with default implementation for common methods. */ public abstract class BaseProvider implements RestorableUniformRandomProvider { /** * The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd. * <pre> * phi = (sqrt(5) - 1) / 2) * 2^64 * </pre> * @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a> */ private static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L; /** The fractional part of the golden ratio, phi, scaled to 32-bits and rounded to odd. */ private static final int GOLDEN_RATIO_32 = 0x9e3779b9; /** {@inheritDoc} */ @Override public RandomProviderState saveState() { return new RandomProviderDefaultState(getStateInternal()); } /** {@inheritDoc} */ @Override public void restoreState(RandomProviderState state) { if (state instanceof RandomProviderDefaultState) { setStateInternal(((RandomProviderDefaultState) state).getState()); } else { throw new IllegalArgumentException("Foreign instance"); } } /** {@inheritDoc} */ @Override public String toString() { return getClass().getName(); } /** * Combine parent and subclass states. * This method must be called by all subclasses in order to ensure * that state can be restored in case some of it is stored higher * up in the class hierarchy. * * I.e. the body of the overridden {@link #getStateInternal()}, * will end with a statement like the following: * <pre> * <code> * return composeStateInternal(state, * super.getStateInternal()); * </code> * </pre> * where {@code state} is the state needed and defined by the class * where the method is overridden. * * @param state State of the calling class. * @param parentState State of the calling class' parent. * @return the combined state. * Bytes that belong to the local state will be stored at the * beginning of the resulting array. */ protected byte[] composeStateInternal(byte[] state, byte[] parentState) { final int len = parentState.length + state.length; final byte[] c = new byte[len]; System.arraycopy(state, 0, c, 0, state.length); System.arraycopy(parentState, 0, c, state.length, parentState.length); return c; } /** * Splits the given {@code state} into a part to be consumed by the caller * in order to restore its local state, while the reminder is passed to * the parent class. * * I.e. the body of the overridden {@link #setStateInternal(byte[])}, * will contain statements like the following: * <pre> * <code> * final byte[][] s = splitState(state, localStateLength); * // Use "s[0]" to recover the local state. * super.setStateInternal(s[1]); * </code> * </pre> * where {@code state} is the combined state of the calling class and of * all its parents. * * @param state State. * The local state must be stored at the beginning of the array. * @param localStateLength Number of elements that will be consumed by the * locally defined state. * @return the local state (in slot 0) and the parent state (in slot 1). * @throws IllegalStateException if {@code state.length < localStateLength}. */ protected byte[][] splitStateInternal(byte[] state, int localStateLength) { checkStateSize(state, localStateLength); final byte[] local = new byte[localStateLength]; System.arraycopy(state, 0, local, 0, localStateLength); final int parentLength = state.length - localStateLength; final byte[] parent = new byte[parentLength]; System.arraycopy(state, localStateLength, parent, 0, parentLength); return new byte[][] {local, parent}; } /** * Creates a snapshot of the RNG state. * * @return the internal state. */ protected byte[] getStateInternal() { // This class has no state (and is the top-level class that // declares this method). return new byte[0]; } /** * Resets the RNG to the given {@code state}. * * @param state State (previously obtained by a call to * {@link #getStateInternal()}). * @throws IllegalStateException if the size of the given array is * not consistent with the state defined by this class. * * @see #checkStateSize(byte[],int) */ protected void setStateInternal(byte[] state) { if (state.length != 0) { // This class has no state. throw new IllegalStateException("State not fully recovered by subclasses"); } } /** * Simple filling procedure. * It will * <ol> * <li> * fill the beginning of {@code state} by copying * {@code min(seed.length, state.length)} elements from * {@code seed}, * </li> * <li> * set all remaining elements of {@code state} with non-zero * values (even if {@code seed.length < state.length}). * </li> * </ol> * * @param state State. Must be allocated. * @param seed Seed. Cannot be null. */ protected void fillState(int[] state, int[] seed) { final int stateSize = state.length; final int seedSize = seed.length; System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize)); if (seedSize < stateSize) { for (int i = seedSize; i < stateSize; i++) { state[i] = (int) (scrambleWell(state[i - seed.length], i) & 0xffffffffL); } } } /** * Simple filling procedure. * It will * <ol> * <li> * fill the beginning of {@code state} by copying * {@code min(seed.length, state.length)} elements from * {@code seed}, * </li> * <li> * set all remaining elements of {@code state} with non-zero * values (even if {@code seed.length < state.length}). * </li> * </ol> * * @param state State. Must be allocated. * @param seed Seed. Cannot be null. */ protected void fillState(long[] state, long[] seed) { final int stateSize = state.length; final int seedSize = seed.length; System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize)); if (seedSize < stateSize) { for (int i = seedSize; i < stateSize; i++) { state[i] = scrambleWell(state[i - seed.length], i); } } } /** * Checks that the {@code state} has the {@code expected} size. * * @param state State. * @param expected Expected length of {@code state} array. * @throws IllegalStateException if {@code state.length < expected}. * @deprecated Method is used internally and should be made private in * some future release. */ @Deprecated protected void checkStateSize(byte[] state, int expected) { if (state.length < expected) { throw new IllegalStateException("State size must be larger than " + expected + " but was " + state.length); } } /** * Checks whether {@code index} is in the range {@code [min, max]}. * * @param min Lower bound. * @param max Upper bound. * @param index Value that must lie within the {@code [min, max]} interval. * @throws IndexOutOfBoundsException if {@code index} is not within the * {@code [min, max]} interval. */ protected void checkIndex(int min, int max, int index) { if (index < min || index > max) { throw new IndexOutOfBoundsException(index + " is out of interval [" + min + ", " + max + "]"); } } /** * Transformation used to scramble the initial state of * a generator. * * @param n Seed element. * @param mult Multiplier. * @param shift Shift. * @param add Offset. * @return the transformed seed element. */ private static long scramble(long n, long mult, int shift, int add) { // Code inspired from "AbstractWell" class. return mult * (n ^ (n >> shift)) + add; } /** * Transformation used to scramble the initial state of * a generator. * * @param n Seed element. * @param add Offset. * @return the transformed seed element. * @see #scramble(long,long,int,int) */ private static long scrambleWell(long n, int add) { // Code inspired from "AbstractWell" class. return scramble(n, 1812433253L, 30, add); } /** * Extend the seed to the specified minimum length. If the seed is equal or greater than the * minimum length, return the same seed unchanged. Otherwise: * <ol> * <li>Create a new array of the specified length * <li>Copy all elements of the seed into the array * <li>Fill the remaining values. The additional values will have at most one occurrence * of zero. If the original seed is all zero, the first extended value will be non-zero. * </li> * </ol> * * <p>This method can be used in constructors that must pass their seed to the super class * to avoid a duplication of seed expansion to the minimum length required by the super class * and the class: * <pre> * public RNG extends AnotherRNG { * public RNG(long[] seed) { * super(seed = extendSeed(seed, SEED_SIZE)); * // Use seed for additional state ... * } * } * </pre> * * <p>Note using the state filling procedure provided in {@link #fillState(long[], long[])} * is not possible as it is an instance method. Calling a seed extension routine must use a * static method. * * <p>This method functions as if the seed has been extended using a * {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64} * generator seeded with {@code seed[0]}, or zero if the input seed length is zero. * <pre> * if (seed.length &lt; length) { * final long[] s = Arrays.copyOf(seed, length); * final SplitMix64 rng = new SplitMix64(s[0]); * for (int i = seed.length; i &lt; length; i++) { * s[i] = rng.nextLong(); * } * return s; * }</pre> * * @param seed Input seed * @param length The minimum length * @return the seed * @since 1.5 */ protected static long[] extendSeed(long[] seed, int length) { if (seed.length < length) { final long[] s = Arrays.copyOf(seed, length); // Fill the rest as if using a SplitMix64 RNG long x = s[0]; for (int i = seed.length; i < length; i++) { s[i] = stafford13(x += GOLDEN_RATIO_64); } return s; } return seed; } /** * Extend the seed to the specified minimum length. If the seed is equal or greater than the * minimum length, return the same seed unchanged. Otherwise: * <ol> * <li>Create a new array of the specified length * <li>Copy all elements of the seed into the array * <li>Fill the remaining values. The additional values will have at most one occurrence * of zero. If the original seed is all zero, the first extended value will be non-zero. * </li> * </ol> * * <p>This method can be used in constructors that must pass their seed to the super class * to avoid a duplication of seed expansion to the minimum length required by the super class * and the class: * <pre> * public RNG extends AnotherRNG { * public RNG(int[] seed) { * super(seed = extendSeed(seed, SEED_SIZE)); * // Use seed for additional state ... * } * } * </pre> * * <p>Note using the state filling procedure provided in {@link #fillState(int[], int[])} * is not possible as it is an instance method. Calling a seed extension routine must use a * static method. * * <p>This method functions as if the seed has been extended using a * {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64}-style 32-bit * generator seeded with {@code seed[0]}, or zero if the input seed length is zero. The * generator uses the 32-bit mixing function from MurmurHash3. * * @param seed Input seed * @param length The minimum length * @return the seed * @since 1.5 */ protected static int[] extendSeed(int[] seed, int length) { if (seed.length < length) { final int[] s = Arrays.copyOf(seed, length); // Fill the rest as if using a SplitMix64-style RNG for 32-bit output int x = s[0]; for (int i = seed.length; i < length; i++) { s[i] = murmur3(x += GOLDEN_RATIO_32); } return s; } return seed; } /** * Perform variant 13 of David Stafford's 64-bit mix function. * This is the mix function used in the * {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64} RNG. * * <p>This is ranked first of the top 14 Stafford mixers. * * <p>This function can be used to mix the bits of a {@code long} value to * obtain a better distribution and avoid collisions between similar values. * * @param x the input value * @return the output value * @see <a href="https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html">Better * Bit Mixing - Improving on MurmurHash3&#39;s 64-bit Finalizer.</a> */ private static long stafford13(long x) { x = (x ^ (x >>> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >>> 27)) * 0x94d049bb133111ebL; return x ^ (x >>> 31); } /** * Perform the finalising 32-bit mix function of Austin Appleby's MurmurHash3. * * <p>This function can be used to mix the bits of a {@code int} value to * obtain a better distribution and avoid collisions between similar values. * * @param x the input value * @return the output value * @see <a href="https://github.com/aappleby/smhasher">SMHasher</a> */ private static int murmur3(int x) { x = (x ^ (x >>> 16)) * 0x85ebca6b; x = (x ^ (x >>> 13)) * 0xc2b2ae35; return x ^ (x >>> 16); } }
3,095
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/package-info.java
/* * 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. */ /** * Base classes for the {@link org.apache.commons.rng.UniformRandomProvider * generation of uniformly distributed random numbers}. * * <p> * <b>For internal use only:</b> Direct access to classes in this package * and below, is discouraged, as they could be modified without notice. * </p> * * <p><b>Notes for developers</b></p> * * <p> * This package contains the common functionality. * <br> * Implementations that produce * {@link org.apache.commons.rng.core.source32.RandomIntSource int} * values are defined in the * {@link org.apache.commons.rng.core.source32 source32} package. * <br> * Implementations that produce * {@link org.apache.commons.rng.core.source64.RandomLongSource long} * values are defined in the * {@link org.apache.commons.rng.core.source64 source64} package. * </p> * * <p> * Implementations are expected to match an algorithm reference output * for the source output type (32-bit or 64-bit integers). This should * be maintained across release versions. Output for derived types * obtained from the source output type, such as float or boolean, are * not required to maintain functional compatibility to allow algorithm * improvements. * </p> * * <p> * Each implementation must have an identifier in * {@code ProviderBuilder.RandomSourceInternal} defined in module * "commons-rng-simple" (to allow instantiation through the * {@code RandomSource} factory methods. * </p> */ package org.apache.commons.rng.core;
3,096
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/RandomStreams.java
/* * 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.rng.core.util; import java.util.Objects; import java.util.Spliterator; import java.util.function.Consumer; import java.util.stream.Stream; import java.util.stream.StreamSupport; import org.apache.commons.rng.SplittableUniformRandomProvider; import org.apache.commons.rng.UniformRandomProvider; /** * Utility for creating streams using a source of randomness. * * @since 1.5 */ public final class RandomStreams { /** The number of bits of each random character in the seed. * The generation algorithm will work if this is in the range [2, 30]. */ private static final int SEED_CHAR_BITS = 4; /** * A factory for creating objects using a seed and a using a source of randomness. * * @param <T> the object type * @since 1.5 */ public interface SeededObjectFactory<T> { /** * Creates the object. * * @param seed Seed used to initialise the instance. * @param source Source of randomness used to initialise the instance. * @return the object */ T create(long seed, UniformRandomProvider source); } /** * Class contains only static methods. */ private RandomStreams() {} /** * Returns a stream producing the given {@code streamSize} number of new objects * generated using the supplied {@code source} of randomness and object {@code factory}. * * <p>A {@code long} seed is provided for each object instance using the stream position * and random bits created from the supplied {@code source}. * * <p>The stream supports parallel execution by splitting the provided {@code source} * of randomness. Consequently objects in the same position in the stream created from * a sequential stream may be created from a different source of randomness than a parallel * stream; it is not expected that parallel execution will create the same final * collection of objects. * * @param <T> the object type * @param streamSize Number of objects to generate. * @param source A source of randomness used to initialise the new instances; this may * be split to provide a source of randomness across a parallel stream. * @param factory Factory to create new instances. * @return a stream of objects; the stream is limited to the given {@code streamSize}. * @throws IllegalArgumentException if {@code streamSize} is negative. * @throws NullPointerException if {@code source} or {@code factory} is null. */ public static <T> Stream<T> generateWithSeed(long streamSize, SplittableUniformRandomProvider source, SeededObjectFactory<T> factory) { if (streamSize < 0) { throw new IllegalArgumentException("Invalid stream size: " + streamSize); } Objects.requireNonNull(source, "source"); Objects.requireNonNull(factory, "factory"); final long seed = createSeed(source); return StreamSupport .stream(new SeededObjectSpliterator<>(0, streamSize, source, factory, seed), false); } /** * Creates a seed to prepend to a counter. The seed is created to satisfy the following * requirements: * <ul> * <li>The least significant bit is set * <li>The seed is composed of characters from an n-bit alphabet * <li>The character used in the least significant bits is unique * <li>The other characters are sampled uniformly from the remaining (n-1) characters * </ul> * * <p>The composed seed is created using {@code ((seed << shift) | count)} * where the shift is applied to ensure non-overlap of the shifted seed and * the count. This is achieved by ensuring the lowest 1-bit of the seed is * above the highest 1-bit of the count. The shift is a multiple of n to ensure * the character used in the least significant bits aligns with higher characters * after a shift. As higher characters exclude the least significant character * no shifted seed can duplicate previously observed composed seeds. This holds * until the least significant character itself is shifted out of the composed seed. * * <p>The seed generation algorithm starts with a random series of bits with the lowest bit * set. Any occurrences of the least significant character in the remaining characters are * replaced using {@link UniformRandomProvider#nextInt()}. * * <p>The remaining characters will be rejected at a rate of 2<sup>-n</sup>. The * character size is a compromise between a low rejection rate and the highest supported * count that may receive a prepended seed. * * <p>The JDK's {@code java.util.random} package uses 4-bits for the character size when * creating a stream of SplittableGenerator. This achieves a rejection rate * of {@code 1/16}. Using this size will require 1 call to generate a {@code long} and * on average 1 call to {@code nextInt(15)}. The maximum supported stream size with a unique * seed per object is 2<sup>60</sup>. The algorithm here also uses a character size of 4-bits; * this simplifies the implementation as there are exactly 16 characters. The algorithm is a * different implementation to the JDK and creates an output seed with similar properties. * * @param rng Source of randomness. * @return the seed */ static long createSeed(UniformRandomProvider rng) { // Initial random bits. Lowest bit must be set. long bits = rng.nextLong() | 1; // Mask to extract characters. // Can be used to sample from (n-1) n-bit characters. final long n = (1L << SEED_CHAR_BITS) - 1; // Extract the unique character. final long unique = bits & n; // Check the rest of the characters do not match the unique character. // This loop extracts the remaining characters and replaces if required. // This will work if the characters do not evenly divide into 64 as we iterate // over the count of remaining bits. The original order is maintained so that // if the bits already satisfy the requirements they are unchanged. for (int i = SEED_CHAR_BITS; i < Long.SIZE; i += SEED_CHAR_BITS) { // Next character long c = (bits >>> i) & n; if (c == unique) { // Branch frequency of 2^-bits. // This code is deliberately branchless. // Avoid nextInt(n) using: c = floor(n * ([0, 2^32) / 2^32)) // Rejection rate for non-uniformity will be negligible: 2^32 % 15 == 1 // so any rejection algorithm only has to exclude 1 value from nextInt(). c = (n * Integer.toUnsignedLong(rng.nextInt())) >>> Integer.SIZE; // Ensure the sample is uniform in [0, n] excluding the unique character c = (unique + c + 1) & n; // Replace by masking out the current character and bitwise add the new one bits = (bits & ~(n << i)) | (c << i); } } return bits; } /** * Spliterator for streams of a given object type that can be created from a seed * and source of randomness. The source of randomness is splittable allowing parallel * stream support. * * <p>The seed is mixed with the stream position to ensure each object is created using * a unique seed value. As the position increases the seed is left shifted until there * is no bit overlap between the seed and the position, i.e the right-most 1-bit of the seed * is larger than the left-most 1-bit of the position. *s * @param <T> the object type */ private static final class SeededObjectSpliterator<T> implements Spliterator<T> { /** Message when the consumer action is null. */ private static final String NULL_ACTION = "action must not be null"; /** The current position in the range. */ private long position; /** The upper limit of the range. */ private final long end; /** Seed used to initialise the new instances. The least significant 1-bit of * the seed must be above the most significant bit of the position. This is maintained * by left shift when the position is updated. */ private long seed; /** Source of randomness used to initialise the new instances. */ private final SplittableUniformRandomProvider source; /** Factory to create new instances. */ private final SeededObjectFactory<T> factory; /** * @param start Start position of the stream (inclusive). * @param end Upper limit of the stream (exclusive). * @param source Source of randomness used to initialise the new instances. * @param factory Factory to create new instances. * @param seed Seed used to initialise the instances. The least significant 1-bit of * the seed must be above the most significant bit of the {@code start} position. */ SeededObjectSpliterator(long start, long end, SplittableUniformRandomProvider source, SeededObjectFactory<T> factory, long seed) { position = start; this.end = end; this.seed = seed; this.source = source; this.factory = factory; } @Override public long estimateSize() { return end - position; } @Override public int characteristics() { return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE; } @Override public Spliterator<T> trySplit() { final long start = position; final long middle = (start + end) >>> 1; if (middle <= start) { return null; } // The child spliterator can use the same seed as the position does not overlap final SeededObjectSpliterator<T> s = new SeededObjectSpliterator<>(start, middle, source.split(), factory, seed); // Since the position has increased ensure the seed does not overlap position = middle; while (seed != 0 && Long.compareUnsigned(Long.lowestOneBit(seed), middle) <= 0) { seed <<= SEED_CHAR_BITS; } return s; } @Override public boolean tryAdvance(Consumer<? super T> action) { Objects.requireNonNull(action, NULL_ACTION); final long pos = position; if (pos < end) { // Advance before exceptions from the action are relayed to the caller position = pos + 1; action.accept(factory.create(seed | pos, source)); // If the position overlaps the seed, shift it by 1 character if ((position & seed) != 0) { seed <<= SEED_CHAR_BITS; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super T> action) { Objects.requireNonNull(action, NULL_ACTION); long pos = position; final long last = end; if (pos < last) { // Ensure forEachRemaining is called only once position = last; final SplittableUniformRandomProvider s = source; final SeededObjectFactory<T> f = factory; do { action.accept(f.create(seed | pos, s)); pos++; // If the position overlaps the seed, shift it by 1 character if ((pos & seed) != 0) { seed <<= SEED_CHAR_BITS; } } while (pos < last); } } } }
3,097
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/NumberFactory.java
/* * 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.rng.core.util; /** * Utility for creating number types from one or two {@code int} values * or one {@code long} value, or a sequence of bytes. */ public final class NumberFactory { /** * The multiplier to convert the least significant 24-bits of an {@code int} to a {@code float}. * See {@link #makeFloat(int)}. * * <p>This is equivalent to {@code 1.0f / (1 << 24)}. */ private static final float FLOAT_MULTIPLIER = 0x1.0p-24f; /** * The multiplier to convert the least significant 53-bits of a {@code long} to a {@code double}. * See {@link #makeDouble(long)} and {@link #makeDouble(int, int)}. * * <p>This is equivalent to {@code 1.0 / (1L << 53)}. */ private static final double DOUBLE_MULTIPLIER = 0x1.0p-53d; /** Lowest byte mask. */ private static final long LONG_LOWEST_BYTE_MASK = 0xffL; /** Number of bytes in a {@code long}. */ private static final int LONG_SIZE = 8; /** Lowest byte mask. */ private static final int INT_LOWEST_BYTE_MASK = 0xff; /** Number of bytes in a {@code int}. */ private static final int INT_SIZE = 4; /** * Class contains only static methods. */ private NumberFactory() {} /** * Creates a {@code boolean} from an {@code int} value. * * @param v Number. * @return a boolean. * * @deprecated Since version 1.2. Method has become obsolete following * <a href="https://issues.apache.org/jira/browse/RNG-57">RNG-57</a>. */ @Deprecated public static boolean makeBoolean(int v) { return v < 0; } /** * Creates a {@code boolean} from a {@code long} value. * * @param v Number. * @return a boolean. * * @deprecated Since version 1.2. Method has become obsolete following * <a href="https://issues.apache.org/jira/browse/RNG-57">RNG-57</a>. */ @Deprecated public static boolean makeBoolean(long v) { return v < 0; } /** * Creates a {@code double} from a {@code long} value. * * @param v Number. * @return a {@code double} value in the interval {@code [0, 1]}. */ public static double makeDouble(long v) { // Require the least significant 53-bits so shift the higher bits across return (v >>> 11) * DOUBLE_MULTIPLIER; } /** * Creates a {@code double} from two {@code int} values. * * @param v Number (high order bits). * @param w Number (low order bits). * @return a {@code double} value in the interval {@code [0, 1]}. */ public static double makeDouble(int v, int w) { // Require the least significant 53-bits from a long. // Join the most significant 26 from v with 27 from w. final long high = ((long) (v >>> 6)) << 27; // 26-bits remain final int low = w >>> 5; // 27-bits remain return (high | low) * DOUBLE_MULTIPLIER; } /** * Creates a {@code float} from an {@code int} value. * * @param v Number. * @return a {@code float} value in the interval {@code [0, 1]}. */ public static float makeFloat(int v) { // Require the least significant 24-bits so shift the higher bits across return (v >>> 8) * FLOAT_MULTIPLIER; } /** * Creates a {@code long} from two {@code int} values. * * @param v Number (high order bits). * @param w Number (low order bits). * @return a {@code long} value. */ public static long makeLong(int v, int w) { return (((long) v) << 32) | (w & 0xffffffffL); } /** * Creates an {@code int} from a {@code long}. * * @param v Number. * @return an {@code int} value made from the "xor" of the * {@link #extractHi(long) high order bits} and * {@link #extractLo(long) low order bits} of {@code v}. * * @deprecated Since version 1.2. Method has become obsolete following * <a href="https://issues.apache.org/jira/browse/RNG-57">RNG-57</a>. */ @Deprecated public static int makeInt(long v) { return extractHi(v) ^ extractLo(v); } /** * Creates an {@code int} from a {@code long}, using the high order bits. * * <p>The returned value is such that if</p> * <pre><code> * vL = extractLo(v); * vH = extractHi(v); * </code></pre> * * <p>then {@code v} is equal to {@link #makeLong(int,int) makeLong(vH, vL)}.</p> * * @param v Number. * @return an {@code int} value made from the most significant bits * of {@code v}. */ public static int extractHi(long v) { return (int) (v >>> 32); } /** * Creates an {@code int} from a {@code long}, using the low order bits. * * <p>The returned value is such that if</p> * * <pre><code> * vL = extractLo(v); * vH = extractHi(v); * </code></pre> * * <p>then {@code v} is equal to {@link #makeLong(int,int) makeLong(vH, vL)}.</p> * * @param v Number. * @return an {@code int} value made from the least significant bits * of {@code v}. */ public static int extractLo(long v) { return (int) v; } /** * Splits a {@code long} into 8 bytes. * * @param v Value. * @return the bytes that compose the given value (least-significant * byte first). */ public static byte[] makeByteArray(long v) { final byte[] b = new byte[LONG_SIZE]; putLong(v, b, 0); return b; } /** * Puts the {@code long} into the buffer starting at the given position. * Adds 8 bytes (least-significant first). * * @param v Value. * @param buffer the buffer. * @param index the index. */ private static void putLong(long v, byte[] buffer, int index) { buffer[index ] = (byte) (v & LONG_LOWEST_BYTE_MASK); buffer[index + 1] = (byte)((v >>> 8) & LONG_LOWEST_BYTE_MASK); buffer[index + 2] = (byte)((v >>> 16) & LONG_LOWEST_BYTE_MASK); buffer[index + 3] = (byte)((v >>> 24) & LONG_LOWEST_BYTE_MASK); buffer[index + 4] = (byte)((v >>> 32) & LONG_LOWEST_BYTE_MASK); buffer[index + 5] = (byte)((v >>> 40) & LONG_LOWEST_BYTE_MASK); buffer[index + 6] = (byte)((v >>> 48) & LONG_LOWEST_BYTE_MASK); buffer[index + 7] = (byte) (v >>> 56); } /** * Creates a {@code long} from 8 bytes. * * @param input Input. * @return the value that correspond to the given bytes assuming * that the order is in increasing byte significance (i.e. the * first byte in the array is the least-significant). * @throws IllegalArgumentException if {@code input.length != 8}. */ public static long makeLong(byte[] input) { checkSize(LONG_SIZE, input.length); return getLong(input, 0); } /** * Gets the {@code long} from the buffer starting at the given position. * Uses 8 bytes (least-significant first). * * @param input the input bytes. * @param index the index. * @return the value that correspond to the given bytes assuming * that the order is in increasing byte significance (i.e. the * first byte in the array is the least-significant). */ private static long getLong(byte[] input, int index) { return (input[index ] & LONG_LOWEST_BYTE_MASK) | (input[index + 1] & LONG_LOWEST_BYTE_MASK) << 8 | (input[index + 2] & LONG_LOWEST_BYTE_MASK) << 16 | (input[index + 3] & LONG_LOWEST_BYTE_MASK) << 24 | (input[index + 4] & LONG_LOWEST_BYTE_MASK) << 32 | (input[index + 5] & LONG_LOWEST_BYTE_MASK) << 40 | (input[index + 6] & LONG_LOWEST_BYTE_MASK) << 48 | (input[index + 7] & LONG_LOWEST_BYTE_MASK) << 56; } /** * Splits an array of {@code long} values into a sequence of bytes. * This method calls {@link #makeByteArray(long)} for each element of * the {@code input}. * * @param input Input. * @return an array of bytes. */ public static byte[] makeByteArray(long[] input) { final int size = input.length * LONG_SIZE; final byte[] b = new byte[size]; for (int i = 0; i < input.length; i++) { putLong(input[i], b, i * LONG_SIZE); } return b; } /** * Creates an array of {@code long} values from a sequence of bytes. * This method calls {@link #makeLong(byte[])} for each subsequence * of 8 bytes. * * @param input Input. * @return an array of {@code long}. * @throws IllegalArgumentException if {@code input.length} is not * a multiple of 8. */ public static long[] makeLongArray(byte[] input) { final int size = input.length; final int num = size / LONG_SIZE; checkSize(num * LONG_SIZE, size); final long[] output = new long[num]; for (int i = 0; i < num; i++) { output[i] = getLong(input, i * LONG_SIZE); } return output; } /** * Splits an {@code int} into 4 bytes. * * @param v Value. * @return the bytes that compose the given value (least-significant * byte first). */ public static byte[] makeByteArray(int v) { final byte[] b = new byte[INT_SIZE]; putInt(v, b, 0); return b; } /** * Puts the {@code int} into the buffer starting at the given position. * Adds 4 bytes (least-significant first). * * @param v the value. * @param buffer the buffer. * @param index the index. */ private static void putInt(int v, byte[] buffer, int index) { buffer[index ] = (byte) (v & INT_LOWEST_BYTE_MASK); buffer[index + 1] = (byte)((v >>> 8) & INT_LOWEST_BYTE_MASK); buffer[index + 2] = (byte)((v >>> 16) & INT_LOWEST_BYTE_MASK); buffer[index + 3] = (byte) (v >>> 24); } /** * Creates an {@code int} from 4 bytes. * * @param input Input. * @return the value that correspond to the given bytes assuming * that the order is in increasing byte significance (i.e. the * first byte in the array is the least-significant). * @throws IllegalArgumentException if {@code input.length != 4}. */ public static int makeInt(byte[] input) { checkSize(INT_SIZE, input.length); return getInt(input, 0); } /** * Gets the {@code int} from the buffer starting at the given position. * Uses 4 bytes (least-significant first). * * @param input the input bytes. * @param index the index. * @return the value that correspond to the given bytes assuming * that the order is in increasing byte significance (i.e. the * first byte in the array is the least-significant). */ private static int getInt(byte[] input, int index) { return (input[index ] & INT_LOWEST_BYTE_MASK) | (input[index + 1] & INT_LOWEST_BYTE_MASK) << 8 | (input[index + 2] & INT_LOWEST_BYTE_MASK) << 16 | (input[index + 3] & INT_LOWEST_BYTE_MASK) << 24; } /** * Splits an array of {@code int} values into a sequence of bytes. * This method calls {@link #makeByteArray(int)} for each element of * the {@code input}. * * @param input Input. * @return an array of bytes. */ public static byte[] makeByteArray(int[] input) { final int size = input.length * INT_SIZE; final byte[] b = new byte[size]; for (int i = 0; i < input.length; i++) { putInt(input[i], b, i * INT_SIZE); } return b; } /** * Creates an array of {@code int} values from a sequence of bytes. * This method calls {@link #makeInt(byte[])} for each subsequence * of 4 bytes. * * @param input Input. Length must be a multiple of 4. * @return an array of {@code int}. * @throws IllegalArgumentException if {@code input.length} is not * a multiple of 4. */ public static int[] makeIntArray(byte[] input) { final int size = input.length; final int num = size / INT_SIZE; checkSize(num * INT_SIZE, size); final int[] output = new int[num]; for (int i = 0; i < num; i++) { output[i] = getInt(input, i * INT_SIZE); } return output; } /** * @param expected Expected value. * @param actual Actual value. * @throws IllegalArgumentException if {@code expected != actual}. */ private static void checkSize(int expected, int actual) { if (expected != actual) { throw new IllegalArgumentException("Array size: Expected " + expected + " but was " + actual); } } }
3,098
0
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core
Create_ds/commons-rng/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/package-info.java
/* * 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. */ /** * This package contains utilities to combine/split primitive types. */ package org.apache.commons.rng.core.util;
3,099