parent
827d9644c8
commit
eeeb553cea
6
pom.xml
6
pom.xml
|
@ -360,6 +360,12 @@
|
|||
</contributors>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-rng</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.apache.commons.math4.exception.MathInternalError;
|
|||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Base class for multivariate probability distributions.
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.apache.commons.math4.analysis.solvers.UnivariateSolverUtils;
|
|||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NumberIsTooSmallException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.special.Beta;
|
||||
import org.apache.commons.math4.special.Gamma;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
package org.apache.commons.math4.distribution;
|
||||
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Implementation of the constant real distribution.
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
|||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.stat.descriptive.StatisticalSummary;
|
||||
import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathUtils;
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.apache.commons.math4.exception.NotPositiveException;
|
|||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
import org.apache.commons.math4.util.Pair;
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ import org.apache.commons.math4.exception.MathArithmeticException;
|
|||
import org.apache.commons.math4.exception.NotANumberException;
|
||||
import org.apache.commons.math4.exception.NotFiniteNumberException;
|
||||
import org.apache.commons.math4.exception.NotPositiveException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.Pair;
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,7 +28,7 @@ import org.apache.commons.math4.exception.NotANumberException;
|
|||
import org.apache.commons.math4.exception.NotFiniteNumberException;
|
||||
import org.apache.commons.math4.exception.NotPositiveException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.Pair;
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.commons.math4.distribution;
|
|||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.CombinatoricsUtils;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.ResizableDoubleArray;
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.special.Gamma;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Interface for distributions on the integers.
|
||||
|
|
|
@ -20,7 +20,7 @@ package org.apache.commons.math4.distribution;
|
|||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.special.Erf;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
|
|||
import org.apache.commons.math4.exception.MathArithmeticException;
|
||||
import org.apache.commons.math4.exception.NotPositiveException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.Pair;
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.apache.commons.math4.linear.EigenDecomposition;
|
|||
import org.apache.commons.math4.linear.NonPositiveDefiniteMatrixException;
|
||||
import org.apache.commons.math4.linear.RealMatrix;
|
||||
import org.apache.commons.math4.linear.SingularMatrixException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
package org.apache.commons.math4.distribution;
|
||||
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Base interface for multivariate distributions on the reals.
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
|||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.special.Erf;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.apache.commons.math4.special.Gamma;
|
|||
import org.apache.commons.math4.util.CombinatoricsUtils;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathUtils;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Implementation of the Poisson distribution.
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Base interface for distributions on the reals.
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Implementation of the uniform integer distribution.
|
||||
|
|
|
@ -20,7 +20,7 @@ package org.apache.commons.math4.distribution;
|
|||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Implementation of the uniform real distribution.
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,8 +18,8 @@ package org.apache.commons.math4.genetics;
|
|||
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Implementation of a genetic algorithm. All factors that govern the operation
|
||||
|
|
|
@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
|||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* N-point crossover policy. For each iteration a random crossover point is
|
||||
|
|
|
@ -25,7 +25,7 @@ import java.util.Set;
|
|||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
|
|||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Perform Uniform Crossover [UX] on the specified chromosomes. A fixed mixing
|
||||
|
|
|
@ -28,8 +28,8 @@ import org.apache.commons.math4.linear.MatrixUtils;
|
|||
import org.apache.commons.math4.linear.RealMatrix;
|
||||
import org.apache.commons.math4.ml.distance.DistanceMeasure;
|
||||
import org.apache.commons.math4.ml.distance.EuclideanDistance;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
import org.apache.commons.math4.util.MathUtils;
|
||||
|
|
|
@ -28,8 +28,8 @@ import org.apache.commons.math4.exception.NumberIsTooSmallException;
|
|||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.ml.distance.DistanceMeasure;
|
||||
import org.apache.commons.math4.ml.distance.EuclideanDistance;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.stat.descriptive.moment.Variance;
|
||||
import org.apache.commons.math4.util.MathUtils;
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ import org.apache.commons.math4.analysis.UnivariateFunction;
|
|||
import org.apache.commons.math4.analysis.function.Constant;
|
||||
import org.apache.commons.math4.distribution.RealDistribution;
|
||||
import org.apache.commons.math4.distribution.UniformRealDistribution;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Creates functions that will select the initial values of a neuron's
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.apache.commons.math4.optim.OptimizationData;
|
|||
import org.apache.commons.math4.optim.PointValuePair;
|
||||
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
|
||||
import org.apache.commons.math4.optim.nonlinear.scalar.MultivariateOptimizer;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.distribution.RealDistribution;
|
||||
import org.apache.commons.math4.distribution.NormalDistribution;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.commons.math4.exception.util.LocalizedFormats;
|
|||
import org.apache.commons.math4.optim.MaxEval;
|
||||
import org.apache.commons.math4.optim.OptimizationData;
|
||||
import org.apache.commons.math4.optim.nonlinear.scalar.GoalType;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Special implementation of the {@link UnivariateOptimizer} interface
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
package org.apache.commons.math4.random;
|
||||
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.distribution.RealDistribution;
|
||||
import org.apache.commons.math4.distribution.NormalDistribution;
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import java.io.ObjectOutputStream;
|
|||
import java.io.IOException;
|
||||
import org.apache.commons.math4.exception.MathInternalError;
|
||||
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.distribution.RealDistribution;
|
||||
import org.apache.commons.math4.distribution.NormalDistribution;
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ package org.apache.commons.math4.random;
|
|||
* Interface extracted from <code>java.util.Random</code>.
|
||||
*
|
||||
* @since 1.1
|
||||
* @deprecated As of 4.0. Please use {@link org.apache.commons.math4.rng.UniformRandomProvider} instead.
|
||||
* @deprecated As of 4.0. Please use {@link org.apache.commons.rng.UniformRandomProvider} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public interface RandomGenerator {
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.apache.commons.math4.exception.NotFiniteNumberException;
|
|||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,8 +21,8 @@ import java.io.IOException;
|
|||
import java.io.ObjectOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
|
||||
/**
|
||||
* Adaptor that delegates to a {@link UniformRandomProvider} instance.
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
package org.apache.commons.math4.random;
|
||||
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
package org.apache.commons.math4.random;
|
||||
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* This class implements a normalized uniform random generator.
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
|
||||
package org.apache.commons.math4.random;
|
||||
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.distribution.RealDistribution;
|
||||
import org.apache.commons.math4.distribution.NormalDistribution;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
|
|
@ -1,418 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng;
|
||||
|
||||
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
|
||||
import org.apache.commons.math4.rng.internal.ProviderBuilder;
|
||||
import org.apache.commons.math4.rng.internal.BaseProvider;
|
||||
import org.apache.commons.math4.rng.internal.util.SeedFactory;
|
||||
import org.apache.commons.math4.rng.internal.source64.TwoCmres;
|
||||
|
||||
/**
|
||||
* This class provides the API for creating generators of random numbers.
|
||||
* <p>
|
||||
* Usage examples:
|
||||
* <pre><code>
|
||||
* UniformRandomProvider rng = RandomSource.create(RandomSource.MT);
|
||||
* </code></pre>
|
||||
* or
|
||||
* <pre><code>
|
||||
* final int[] seed = new int[] { 196, 9, 0, 226 };
|
||||
* UniformRandomProvider rng = RandomSource.create(RandomSource.MT, seed);
|
||||
* </code></pre>
|
||||
* or
|
||||
* <pre><code>
|
||||
* final int[] seed = RandomSource.createIntArray(256);
|
||||
* UniformRandomProvider rng = RandomSource.create(RandomSource.MT, seed);
|
||||
* </code></pre>
|
||||
* where the first argument to method {@code create} is the identifier
|
||||
* of the generator's concrete implementation, and the second the is the
|
||||
* (optional) seed.
|
||||
* <br>
|
||||
* In the first form, a random seed will be {@link SeedFactory generated
|
||||
* automatically}; in the second form, a fixed seed is used; a random seed
|
||||
* is explicitly generated in the third form.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Seeding is the procedure by which a value (or set of values) is
|
||||
* used to <i>initialize</i> a generator instance.
|
||||
* The requirement that a given seed will always result in the same
|
||||
* internal state allows to create different instances of a generator
|
||||
* that will produce the same sequence of pseudo-random numbers.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The type of data used as a seed depends on the concrete implementation
|
||||
* as some types may not provide enough information to fully initialize
|
||||
* the generator's internal state.
|
||||
* <br>
|
||||
* The reference algorithm's seeding procedure (if provided) operates
|
||||
* on a value of a (single) <i>native</i> type:
|
||||
* Each concrete implementation's constructor creates an instance using
|
||||
* the native type whose information contents is used to set the
|
||||
* internal state.
|
||||
* <br>
|
||||
* When the seed value passed by the caller is of the native type, it is
|
||||
* expected that the sequences produced will be identical to those
|
||||
* produced by other implementations of the same reference algorithm.
|
||||
* <br>
|
||||
* However, when the seed value passed by the caller is not of the native
|
||||
* type, a transformation is performed by this library and the resulting
|
||||
* native type value will <i>not</i> contain more information than the
|
||||
* original seed value.
|
||||
* If the algorithm's native type is "simpler" than the type passed by
|
||||
* the caller, then some (unused) information will even be lost.
|
||||
* <br>
|
||||
* The transformation from non-native to native seed type is arbitrary,
|
||||
* as long as it does not reduce the amount of information required by
|
||||
* the algorithm to initialize its state.
|
||||
* The consequence of the transformation is that sequences produced
|
||||
* by this library may <i>not</i> be the same as the sequences produced
|
||||
* by other implementations of the same algorithm!
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class provides methods to generate random seeds (single values
|
||||
* or arrays of values, of {@code int} or {@code long} types) that can
|
||||
* be passed to the {@link RandomSource#create(RandomSource,Object,Object[])
|
||||
* generators factory method}.
|
||||
* <br>
|
||||
* Although the seed-generating methods defined in this class will likely
|
||||
* return different values each time they are called, there is no guarantee
|
||||
* that the resulting "seed" will always generate a <i>good</i> (i.e.
|
||||
* sufficiently uniformly random for the intended purpose) sequence of
|
||||
* numbers, even if the generator is good!
|
||||
* The only way to ensure that the selected seed will make the generator
|
||||
* produce a good sequence is to submit that sequence to a series of
|
||||
* stringent tests, as provided by tools such as
|
||||
* <a href="http://www.phy.duke.edu/~rgb/General/dieharder.php">dieharder</a>
|
||||
* or <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">TestU01</a>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The current implementations have no provision for producing non-overlapping
|
||||
* sequences.
|
||||
* For parallel applications, a possible workaround is that each thread uses
|
||||
* a generator of a different type (see {@link #TWO_CMRES_SELECT}).
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>Note:</b>
|
||||
* Seeding is not equivalent to restoring the internal state of an
|
||||
* <i>already initialized</i> generator.
|
||||
* Indeed, generators can have a state that is more complex than the
|
||||
* seed, and seeding is thus a transformation (from seed to state).
|
||||
* Implementations do not provide the inverse transformation (from
|
||||
* state to seed), hence it is not generally possible to know the seed
|
||||
* that would initialize a new generator instance to the current state
|
||||
* of another instance.
|
||||
* Reseeding is also inefficient if the purpose is to continue the
|
||||
* same sequence where another instance left off, as it would require
|
||||
* to "replay" all the calls performed by that other instance (and it
|
||||
* would require to know the number of calls to the primary source of
|
||||
* randomness, which is also not usually accessible).
|
||||
* <br>
|
||||
* This factory thus provides a method for
|
||||
* {@link #saveState(UniformRandomProvider) saving} the internal
|
||||
* state of a generator.
|
||||
* The state is encapsulated in an {@link State "opaque object"} to be
|
||||
* used for {@link #restoreState(UniformRandomProvider,State) restoring}
|
||||
* a generator (of the same type) to an identical state (e.g. to allow
|
||||
* persistent storage, or to continue a sequence from where the original
|
||||
* instance left off).
|
||||
* </p>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public enum RandomSource {
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.JDKRandom}.
|
||||
* Native seed type: {@code Long}.
|
||||
*/
|
||||
JDK(ProviderBuilder.RandomSourceInternal.JDK),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well512a}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_512_A(ProviderBuilder.RandomSourceInternal.WELL_512_A),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well1024a}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_1024_A(ProviderBuilder.RandomSourceInternal.WELL_1024_A),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well19937a}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_19937_A(ProviderBuilder.RandomSourceInternal.WELL_19937_A),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well19937c}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_19937_C(ProviderBuilder.RandomSourceInternal.WELL_19937_C),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well44497a}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_44497_A(ProviderBuilder.RandomSourceInternal.WELL_44497_A),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.Well44497b}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
WELL_44497_B(ProviderBuilder.RandomSourceInternal.WELL_44497_B),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.MersenneTwister}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
MT(ProviderBuilder.RandomSourceInternal.MT),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source32.ISAACRandom}.
|
||||
* Native seed type: {@code int[]}.
|
||||
*/
|
||||
ISAAC(ProviderBuilder.RandomSourceInternal.ISAAC),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source64.SplitMix64}.
|
||||
* Native seed type: {@code Long}.
|
||||
*/
|
||||
SPLIT_MIX_64(ProviderBuilder.RandomSourceInternal.SPLIT_MIX_64),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source64.XorShift1024Star}.
|
||||
* Native seed type: {@code long[]}.
|
||||
*/
|
||||
XOR_SHIFT_1024_S(ProviderBuilder.RandomSourceInternal.XOR_SHIFT_1024_S),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source64.TwoCmres}.
|
||||
* Native seed type: {@code Integer}.
|
||||
*/
|
||||
TWO_CMRES(ProviderBuilder.RandomSourceInternal.TWO_CMRES),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source64.TwoCmres},
|
||||
* with explicit selection of the two subcycle generators.
|
||||
* Native seed type: {@code Integer}.
|
||||
*/
|
||||
TWO_CMRES_SELECT(ProviderBuilder.RandomSourceInternal.TWO_CMRES_SELECT),
|
||||
/**
|
||||
* Source of randomness is {@link org.apache.commons.math4.rng.internal.source64.MersenneTwister64}.
|
||||
* Native seed type: {@code long[]}.
|
||||
*/
|
||||
MT_64(ProviderBuilder.RandomSourceInternal.MT_64);
|
||||
|
||||
/** Internal identifier. */
|
||||
private final ProviderBuilder.RandomSourceInternal internalIdentifier;
|
||||
|
||||
/**
|
||||
* @param id Internal identifier.
|
||||
*/
|
||||
RandomSource(ProviderBuilder.RandomSourceInternal id) {
|
||||
internalIdentifier = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the internal identifier.
|
||||
*/
|
||||
ProviderBuilder.RandomSourceInternal getInternalIdentifier() {
|
||||
return internalIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the type of given {@code seed} is the native type
|
||||
* of the implementation.
|
||||
*
|
||||
* @param seed Seed value.
|
||||
* @return {@code true} if the seed can be passed to the builder
|
||||
* for this RNG type.
|
||||
*/
|
||||
public boolean isNativeSeed(Object seed) {
|
||||
return internalIdentifier.isNativeSeed(seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marker interface used to define the "save" and "restore"
|
||||
* functionality of the generators.
|
||||
*/
|
||||
public interface State {}
|
||||
|
||||
/**
|
||||
* Creates a random number generator with a random seed.
|
||||
*
|
||||
* <p>
|
||||
* Example of usage:
|
||||
* <pre><code>
|
||||
* UniformRandomProvider rng = RandomSource.create(RandomSource.MT);
|
||||
* </code></pre>
|
||||
* </p>
|
||||
*
|
||||
* @param source RNG type.
|
||||
* @return the RNG.
|
||||
*/
|
||||
public static UniformRandomProvider create(RandomSource source) {
|
||||
return create(source, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a random number generator with the given {@code seed}.
|
||||
*
|
||||
* <p>
|
||||
* Example of usage:
|
||||
* <pre><code>
|
||||
* UniformRandomProvider rng = RandomSource.create(RandomSource.TWO_CMRES_SELECT, 26219, 6, 9);
|
||||
* </code></pre>
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Valid types for the {@code seed} are:
|
||||
* <ul>
|
||||
* <li>{@code Integer} (or {@code int})</li>
|
||||
* <li>{@code Long} (or {@code long})</li>
|
||||
* <li>{@code int[]}</li>
|
||||
* <li>{@code long[]}</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Notes:
|
||||
* <ul>
|
||||
* <li>
|
||||
* When the seed type passed as argument is more complex (i.e. more
|
||||
* bits can be independently chosen) than the generator's
|
||||
* {@link #isNativeSeed(Object) native type}, the conversion of a
|
||||
* set of different seeds will necessarily result in the same value
|
||||
* of the native seed type.
|
||||
* </li>
|
||||
* <li>
|
||||
* When the native seed type is an array, the same remark applies
|
||||
* when the array contains more bits than the state of the generator.
|
||||
* </li>
|
||||
* <li>
|
||||
* When the native seed type is an array and the {@code seed} is
|
||||
* {@code null}, the size of the generated array will be 128.
|
||||
* </li>
|
||||
* </p>
|
||||
*
|
||||
* @param source RNG type.
|
||||
* @param seed Seed value. It can be {@code null} (in which case a
|
||||
* random value will be used).
|
||||
* @param data Additional arguments to the implementation's constructor.
|
||||
* Please refer to the documentation of each specific implementation.
|
||||
* @return the RNG.
|
||||
* @throws MathUnsupportedOperationException if the type of the
|
||||
* {@code seed} is invalid.
|
||||
* @throws org.apache.commons.math4.exception.InsufficientDataException
|
||||
* if data is missing to initialize the generator implemented by the
|
||||
* given {@code source}.
|
||||
*/
|
||||
public static UniformRandomProvider create(RandomSource source,
|
||||
Object seed,
|
||||
Object ... data) {
|
||||
return ProviderBuilder.create(source.getInternalIdentifier(), seed, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of elements of the set of "subcycle" generators from
|
||||
* which two can be selected in order to create a {@link TwoCmres} RNG.
|
||||
*
|
||||
* @return the number of implemented subcycle generators.
|
||||
*/
|
||||
public static int numberOfCmresGenerators() {
|
||||
return TwoCmres.numberOfSubcycleGenerators();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the state of a RNG.
|
||||
*
|
||||
* @param provider Provider.
|
||||
* @return the current state of the given {@code provider}.
|
||||
* @throws MathUnsupportedOperationException if the {@code provider} is
|
||||
* not an object created by this factory or the underlying source of
|
||||
* randomness does not support this functionality.
|
||||
*
|
||||
* @see #restoreState(UniformRandomProvider,RandomSource.State)
|
||||
*/
|
||||
public static State saveState(UniformRandomProvider provider) {
|
||||
if (!(provider instanceof BaseProvider)) {
|
||||
throw new MathUnsupportedOperationException();
|
||||
} else {
|
||||
return ((BaseProvider) provider).getState();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the state of a RNG.
|
||||
*
|
||||
* @param provider Provider.
|
||||
* @param state State which the {@code provider} will be set to.
|
||||
* This parameter must have been obtained by a call to
|
||||
* {@link #saveState(UniformRandomProvider) saveState(rng)}
|
||||
* where {@code rng} is either the same object as {@code provider},
|
||||
* or an object of the exact same class.
|
||||
* @throws MathUnsupportedOperationException if the {@code provider} is
|
||||
* not an object created by this factory or the underlying source of
|
||||
* randomness does not support this functionality.
|
||||
* @throws org.apache.commons.math4.exception.InsufficientDataException
|
||||
* if it was detected that the {@code state} is incompatible with the
|
||||
* given {@code provider}.
|
||||
*
|
||||
* @see #saveState(UniformRandomProvider)
|
||||
*/
|
||||
public static void restoreState(UniformRandomProvider provider,
|
||||
State state) {
|
||||
if (!(provider instanceof BaseProvider)) {
|
||||
throw new MathUnsupportedOperationException();
|
||||
} else {
|
||||
((BaseProvider) provider).setState(state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number for use as a seed.
|
||||
*
|
||||
* @return a random number.
|
||||
*/
|
||||
public static int createInt() {
|
||||
return SeedFactory.createInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number for use as a seed.
|
||||
*
|
||||
* @return a random number.
|
||||
*/
|
||||
public static long createLong() {
|
||||
return SeedFactory.createLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
public static int[] createIntArray(int n) {
|
||||
return SeedFactory.createIntArray(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
public static long[] createLongArray(int n) {
|
||||
return SeedFactory.createLongArray(n);
|
||||
}
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng;
|
||||
|
||||
/**
|
||||
* Applies to generators of random number sequences that follow a uniform
|
||||
* distribution.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface UniformRandomProvider {
|
||||
/**
|
||||
* Generates {@code byte} values and places them into a user-supplied array.
|
||||
* <p>
|
||||
* The number of random bytes produced is equal to the length of the
|
||||
* the byte array.
|
||||
* </p>
|
||||
*
|
||||
* @param bytes Byte array in which to put the random bytes.
|
||||
* Cannot be {@code null}.
|
||||
*/
|
||||
void nextBytes(byte[] bytes);
|
||||
|
||||
/**
|
||||
* Generates {@code byte} values and places them into a user-supplied array.
|
||||
*
|
||||
* <p>
|
||||
* The array is filled with bytes extracted from random integers.
|
||||
* This implies that the number of random bytes generated may be larger than
|
||||
* the length of the byte array.
|
||||
* </p>
|
||||
*
|
||||
* @param bytes Array in which to put the generated bytes.
|
||||
* Cannot be {@code null}.
|
||||
* @param start Index at which to start inserting the generated bytes.
|
||||
* @param len Number of bytes to insert.
|
||||
* @throws org.apache.commons.math4.exception.OutOfRangeException
|
||||
* if {@code start < 0} or {@code start >= bytes.length}.
|
||||
* @throws org.apache.commons.math4.exception.OutOfRangeException
|
||||
* if {@code len < 0} or {@code len > bytes.length - start}.
|
||||
*/
|
||||
void nextBytes(byte[] bytes,
|
||||
int start,
|
||||
int len);
|
||||
|
||||
/**
|
||||
* Generates an {@code int} value.
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
int nextInt();
|
||||
|
||||
/**
|
||||
* Generates an {@code int} value between 0 (inclusive) and the
|
||||
* specified value (exclusive).
|
||||
*
|
||||
* @param n Bound on the random number to be returned. Must be positive.
|
||||
* @return a random {@code int} value between 0 (inclusive) and n
|
||||
* (exclusive).
|
||||
* @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
|
||||
* if {@code n} is not positive.
|
||||
*/
|
||||
int nextInt(int n);
|
||||
|
||||
/**
|
||||
* Generates a {@code long} value.
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
long nextLong();
|
||||
|
||||
/**
|
||||
* Generates a {@code long} value between 0 (inclusive) and the specified
|
||||
* value (exclusive).
|
||||
*
|
||||
* @param n Bound on the random number to be returned. Must be positive.
|
||||
* @return a random {@code long} value between 0 (inclusive) and n
|
||||
* (exclusive).
|
||||
* @throws org.apache.commons.math4.exception.NotStrictlyPositiveException
|
||||
* if {@code n} is not positive.
|
||||
*/
|
||||
long nextLong(long n);
|
||||
|
||||
/**
|
||||
* Generates a {@code boolean} value.
|
||||
*
|
||||
* @return the next random value.
|
||||
*/
|
||||
boolean nextBoolean();
|
||||
|
||||
/**
|
||||
* Generates a {@code float} value between 0 and 1.
|
||||
*
|
||||
* @return the next random value between 0 and 1.
|
||||
*/
|
||||
float nextFloat();
|
||||
|
||||
/**
|
||||
* Generates a {@code double} value between 0 and 1.
|
||||
*
|
||||
* @return the next random value between 0 and 1.
|
||||
*/
|
||||
double nextDouble();
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.io.Serializable;
|
||||
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
|
||||
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
|
||||
/**
|
||||
* Base class with default implementation for common methods.
|
||||
*/
|
||||
public abstract class BaseProvider
|
||||
implements UniformRandomProvider,
|
||||
StateSettable {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int nextInt(int n) throws IllegalArgumentException {
|
||||
if (n > 0) {
|
||||
if ((n & -n) == n) {
|
||||
return (int) ((n * (long) (nextInt() >>> 1)) >> 31);
|
||||
}
|
||||
int bits;
|
||||
int val;
|
||||
do {
|
||||
bits = nextInt() >>> 1;
|
||||
val = bits % n;
|
||||
} while (bits - val + (n - 1) < 0);
|
||||
return val;
|
||||
}
|
||||
|
||||
throw new NotStrictlyPositiveException(n);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long nextLong(long n) {
|
||||
if (n > 0) {
|
||||
long bits;
|
||||
long val;
|
||||
do {
|
||||
bits = nextLong() >>> 1;
|
||||
val = bits % n;
|
||||
} while (bits - val + (n - 1) < 0);
|
||||
return val;
|
||||
}
|
||||
|
||||
throw new NotStrictlyPositiveException(n);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RandomSource.State getState() {
|
||||
return new State(getStateInternal());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void setState(RandomSource.State state) {
|
||||
// Cast will intentionally fail if the argument is not one we created.
|
||||
final State s = (State) state;
|
||||
setStateInternal(s.getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a snapshot of the RNG state.
|
||||
*
|
||||
* @return the internal state.
|
||||
* @throws MathUnsupportedOperationException if not implemented.
|
||||
*/
|
||||
protected byte[] getStateInternal() {
|
||||
throw new MathUnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the RNG to the given {@code state}.
|
||||
*
|
||||
* @param state State (previously obtained by a call to
|
||||
* {@link #getStateInternal()}).
|
||||
* @throws MathUnsupportedOperationException if not implemented.
|
||||
*/
|
||||
protected void setStateInternal(byte[] state) {
|
||||
throw new MathUnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* "Black-box" state.
|
||||
* Its sole 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 to modify the data contained in
|
||||
* instances of this class.
|
||||
*/
|
||||
private static class State
|
||||
implements RandomSource.State,
|
||||
Serializable {
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = 4720160226L;
|
||||
/** Internal state. */
|
||||
private byte[] state;
|
||||
|
||||
/**
|
||||
* @param state Mapping of all the data which a subclass of
|
||||
* {@link BaseProvider} needs in order to reset its internal
|
||||
* state.
|
||||
*/
|
||||
State(byte[] state) {
|
||||
this.state = Arrays.copyOf(state, state.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the internal state.
|
||||
*/
|
||||
byte[] getState() {
|
||||
return Arrays.copyOf(state, state.length);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,346 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.apache.commons.math4.exception.MathUnsupportedOperationException;
|
||||
import org.apache.commons.math4.exception.MathInternalError;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.internal.util.SeedFactory;
|
||||
import org.apache.commons.math4.rng.internal.util.NoOpConverter;
|
||||
import org.apache.commons.math4.rng.internal.util.Int2Long;
|
||||
import org.apache.commons.math4.rng.internal.util.Long2Int;
|
||||
import org.apache.commons.math4.rng.internal.util.Long2IntArray;
|
||||
import org.apache.commons.math4.rng.internal.util.Long2LongArray;
|
||||
import org.apache.commons.math4.rng.internal.util.IntArray2LongArray;
|
||||
import org.apache.commons.math4.rng.internal.util.LongArray2IntArray;
|
||||
import org.apache.commons.math4.rng.internal.util.LongArray2Long;
|
||||
import org.apache.commons.math4.rng.internal.util.IntArray2Int;
|
||||
import org.apache.commons.math4.rng.internal.util.SeedConverter;
|
||||
import org.apache.commons.math4.rng.internal.util.SeedConverterComposer;
|
||||
import org.apache.commons.math4.rng.internal.source32.JDKRandom;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well512a;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well1024a;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well19937a;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well19937c;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well44497a;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well44497b;
|
||||
import org.apache.commons.math4.rng.internal.source32.ISAACRandom;
|
||||
import org.apache.commons.math4.rng.internal.source32.MersenneTwister;
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
import org.apache.commons.math4.rng.internal.source64.XorShift1024Star;
|
||||
import org.apache.commons.math4.rng.internal.source64.TwoCmres;
|
||||
import org.apache.commons.math4.rng.internal.source64.MersenneTwister64;
|
||||
|
||||
/**
|
||||
* RNG builder.
|
||||
* <p>
|
||||
* It uses reflection to find the factory method of the RNG implementation,
|
||||
* and performs seed type conversions.
|
||||
* </p>
|
||||
*/
|
||||
public class ProviderBuilder {
|
||||
/** Length of the seed array (for random seed). */
|
||||
private static final int RANDOM_SEED_ARRAY_SIZE = 128;
|
||||
/** Seed converter. */
|
||||
private static final Long2Int LONG_TO_INT = new Long2Int();
|
||||
/** Seed converter. */
|
||||
private static final Int2Long INT_TO_LONG = new Int2Long();
|
||||
/** Seed converter. */
|
||||
private static final Long2IntArray LONG_TO_INT_ARRAY = new Long2IntArray(RANDOM_SEED_ARRAY_SIZE);
|
||||
/** Seed converter. */
|
||||
private static final Long2LongArray LONG_TO_LONG_ARRAY = new Long2LongArray(RANDOM_SEED_ARRAY_SIZE);
|
||||
/** Seed converter. */
|
||||
private static final LongArray2Long LONG_ARRAY_TO_LONG = new LongArray2Long();
|
||||
/** Seed converter. */
|
||||
private static final IntArray2Int INT_ARRAY_TO_INT = new IntArray2Int();
|
||||
/** Seed converter. */
|
||||
private static final LongArray2IntArray LONG_ARRAY_TO_INT_ARRAY = new LongArray2IntArray();
|
||||
/** Seed converter. */
|
||||
private static final IntArray2LongArray INT_ARRAY_TO_LONG_ARRAY = new IntArray2LongArray();
|
||||
/** Map to convert "Integer" seeds. */
|
||||
private static final Map<Class<?>, SeedConverter<Integer,?>> CONV_INT = new HashMap<>();
|
||||
/** Map to convert "int[]" seeds. */
|
||||
private static final Map<Class<?>, SeedConverter<int[],?>> CONV_INT_ARRAY = new HashMap<>();
|
||||
/** Map to convert "Long" seeds. */
|
||||
private static final Map<Class<?>, SeedConverter<Long,?>> CONV_LONG = new HashMap<>();
|
||||
/** Map to convert "long[]" seeds. */
|
||||
private static final Map<Class<?>, SeedConverter<long[],?>> CONV_LONG_ARRAY = new HashMap<>();
|
||||
|
||||
static {
|
||||
// Input seed type is "Long".
|
||||
// Key is the implementation's "native" seed type.
|
||||
CONV_LONG.put(Integer.class, LONG_TO_INT);
|
||||
CONV_LONG.put(Long.class, new NoOpConverter<Long>());
|
||||
CONV_LONG.put(int[].class, LONG_TO_INT_ARRAY);
|
||||
CONV_LONG.put(long[].class, LONG_TO_LONG_ARRAY);
|
||||
|
||||
// Input seed type is "Integer".
|
||||
// Key is the implementation's "native" seed type.
|
||||
CONV_INT.put(Integer.class, new NoOpConverter<Integer>());
|
||||
CONV_INT.put(Long.class, INT_TO_LONG);
|
||||
CONV_INT.put(int[].class, new SeedConverterComposer<Integer,Long,int[]>(INT_TO_LONG, LONG_TO_INT_ARRAY));
|
||||
CONV_INT.put(long[].class, new SeedConverterComposer<Integer,Long,long[]>(INT_TO_LONG, LONG_TO_LONG_ARRAY));
|
||||
|
||||
// Input seed type is "int[]".
|
||||
// Key is the implementation's "native" seed type.
|
||||
CONV_INT_ARRAY.put(Integer.class, INT_ARRAY_TO_INT);
|
||||
CONV_INT_ARRAY.put(Long.class, new SeedConverterComposer<int[],Integer,Long>(INT_ARRAY_TO_INT, INT_TO_LONG));
|
||||
CONV_INT_ARRAY.put(int[].class, new NoOpConverter<int[]>());
|
||||
CONV_INT_ARRAY.put(long[].class, INT_ARRAY_TO_LONG_ARRAY);
|
||||
|
||||
// Input seed type is "long[]".
|
||||
// Key is the implementation's "native" seed type.
|
||||
CONV_LONG_ARRAY.put(Integer.class, new SeedConverterComposer<long[],Long,Integer>(LONG_ARRAY_TO_LONG, LONG_TO_INT));
|
||||
CONV_LONG_ARRAY.put(Long.class, LONG_ARRAY_TO_LONG);
|
||||
CONV_LONG_ARRAY.put(int[].class, LONG_ARRAY_TO_INT_ARRAY);
|
||||
CONV_LONG_ARRAY.put(long[].class, new NoOpConverter<long[]>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Class only contains static method.
|
||||
*/
|
||||
private ProviderBuilder() {}
|
||||
|
||||
/**
|
||||
* Creates a RNG instance.
|
||||
*
|
||||
* @param source RNG specification.
|
||||
* @param seed Seed value. It can be {@code null} (in which case a
|
||||
* random value will be used).
|
||||
* @param args Additional arguments to the implementation's constructor.
|
||||
* @return a new RNG instance.
|
||||
* @throws MathUnsupportedOperationException if the seed type is
|
||||
* invalid.
|
||||
*/
|
||||
public static UniformRandomProvider create(RandomSourceInternal source,
|
||||
Object seed,
|
||||
Object[] args) {
|
||||
// Convert seed to native type.
|
||||
final Object nativeSeed = createSeed(source, seed);
|
||||
|
||||
// Build a single array with all the arguments to be passed
|
||||
// (in the right order) to the constructor.
|
||||
final List<Object> all = new ArrayList<>();
|
||||
all.add(nativeSeed);
|
||||
if (args != null) {
|
||||
all.addAll(Arrays.asList(args));
|
||||
}
|
||||
|
||||
// Instantiate.
|
||||
return create(createConstructor(source), all.toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a native seed from any of the supported seed types.
|
||||
*
|
||||
* @param source Source.
|
||||
* @param seed Input seed.
|
||||
* @return the native seed.
|
||||
* @throw MathUnsupportedOperationException if the {@code seed} type
|
||||
* is invalid.
|
||||
*/
|
||||
private static Object createSeed(RandomSourceInternal source,
|
||||
Object seed) {
|
||||
Object nativeSeed = null;
|
||||
|
||||
if (seed == null) {
|
||||
// Create a random seed of the appropriate native type.
|
||||
|
||||
if (source.getSeed().equals(Integer.class)) {
|
||||
nativeSeed = SeedFactory.createInt();
|
||||
} else if (source.getSeed().equals(Long.class)) {
|
||||
nativeSeed = SeedFactory.createLong();
|
||||
} else if (source.getSeed().equals(int[].class)) {
|
||||
nativeSeed = SeedFactory.createIntArray(RANDOM_SEED_ARRAY_SIZE);
|
||||
} else if (source.getSeed().equals(long[].class)) {
|
||||
nativeSeed = SeedFactory.createLongArray(RANDOM_SEED_ARRAY_SIZE);
|
||||
}
|
||||
} else {
|
||||
// Convert to native type.
|
||||
|
||||
if (seed instanceof Integer) {
|
||||
nativeSeed = CONV_INT.get(source.getSeed()).convert((Integer) seed);
|
||||
} else if (seed instanceof Long) {
|
||||
nativeSeed = CONV_LONG.get(source.getSeed()).convert((Long) seed);
|
||||
} else if (seed instanceof int[]) {
|
||||
nativeSeed = CONV_INT_ARRAY.get(source.getSeed()).convert((int[]) seed);
|
||||
} else if (seed instanceof long[]) {
|
||||
nativeSeed = CONV_LONG_ARRAY.get(source.getSeed()).convert((long[]) seed);
|
||||
}
|
||||
|
||||
if (nativeSeed == null) {
|
||||
// Since the input seed was not null, getting here means that
|
||||
// no suitable converter is present in the maps.
|
||||
throw new MathUnsupportedOperationException();
|
||||
}
|
||||
|
||||
if (!source.isNativeSeed(nativeSeed)) {
|
||||
// Conversion setup is wrong.
|
||||
throw new MathInternalError();
|
||||
}
|
||||
}
|
||||
|
||||
return nativeSeed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a constructor.
|
||||
*
|
||||
* @param source RNG specification.
|
||||
* @return a RNG constructor.
|
||||
*/
|
||||
private static Constructor<?> createConstructor(RandomSourceInternal source) {
|
||||
try {
|
||||
return source.getRng().getConstructor(source.getArgs());
|
||||
} catch (NoSuchMethodException e) {
|
||||
// Info in "RandomSourceInternal" is inconsistent with the
|
||||
// constructor of the implementation.
|
||||
throw new MathInternalError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a RNG.
|
||||
*
|
||||
* @param rng RNG specification.
|
||||
* @param args Arguments to the implementation's constructor.
|
||||
* @return a new RNG instance.
|
||||
*/
|
||||
private static UniformRandomProvider create(Constructor<?> rng,
|
||||
Object[] args) {
|
||||
try {
|
||||
return (UniformRandomProvider) rng.newInstance(args);
|
||||
} catch (InvocationTargetException |
|
||||
InstantiationException |
|
||||
IllegalArgumentException |
|
||||
IllegalAccessException e) {
|
||||
throw new MathInternalError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifiers of the generators.
|
||||
*/
|
||||
public enum RandomSourceInternal {
|
||||
/** Source of randomness is {@link JDKRandom}. */
|
||||
JDK(JDKRandom.class,
|
||||
Long.class),
|
||||
/** Source of randomness is {@link Well512a}. */
|
||||
WELL_512_A(Well512a.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link Well1024a}. */
|
||||
WELL_1024_A(Well1024a.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link Well19937a}. */
|
||||
WELL_19937_A(Well19937a.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link Well19937c}. */
|
||||
WELL_19937_C(Well19937c.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link Well44497a}. */
|
||||
WELL_44497_A(Well44497a.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link Well44497b}. */
|
||||
WELL_44497_B(Well44497b.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link MersenneTwister}. */
|
||||
MT(MersenneTwister.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link ISAACRandom}. */
|
||||
ISAAC(ISAACRandom.class,
|
||||
int[].class),
|
||||
/** Source of randomness is {@link SplitMix64}. */
|
||||
SPLIT_MIX_64(SplitMix64.class,
|
||||
Long.class),
|
||||
/** Source of randomness is {@link XorShift1024Star}. */
|
||||
XOR_SHIFT_1024_S(XorShift1024Star.class,
|
||||
long[].class),
|
||||
/** Source of randomness is {@link TwoCmres}. */
|
||||
TWO_CMRES(TwoCmres.class,
|
||||
Integer.class),
|
||||
/**
|
||||
* Source of randomness is {@link TwoCmres} with explicit selection
|
||||
* of the two subcycle generators.
|
||||
*/
|
||||
TWO_CMRES_SELECT(TwoCmres.class,
|
||||
Integer.class,
|
||||
Integer.TYPE,
|
||||
Integer.TYPE),
|
||||
/** Source of randomness is {@link MersenneTwister64}. */
|
||||
MT_64(MersenneTwister64.class,
|
||||
long[].class);
|
||||
|
||||
/** Source type. */
|
||||
private final Class<? extends UniformRandomProvider> rng;
|
||||
/** Data needed to build the generator. */
|
||||
private final Class<?>[] args;
|
||||
|
||||
/**
|
||||
* @param rng Source type.
|
||||
* @param args Data needed to create a generator instance.
|
||||
* The first element must be the native seed type.
|
||||
*/
|
||||
RandomSourceInternal(Class<? extends UniformRandomProvider> rng,
|
||||
Class<?> ... args) {
|
||||
this.rng = rng;
|
||||
this.args = Arrays.copyOf(args, args.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the source type.
|
||||
*/
|
||||
public Class<?> getRng() {
|
||||
return rng;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the seed type.
|
||||
*/
|
||||
Class<?> getSeed() {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the data needed to build the generator.
|
||||
*/
|
||||
Class<?>[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the type of given {@code seed} is the native type
|
||||
* of the implementation.
|
||||
*
|
||||
* @param <SEED> Seed type.
|
||||
*
|
||||
* @param seed Seed value.
|
||||
* @return {@code true} if the seed can be passed to the builder
|
||||
* for this RNG type.
|
||||
*/
|
||||
public <SEED> boolean isNativeSeed(SEED seed) {
|
||||
return getSeed().equals(seed.getClass());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal;
|
||||
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
|
||||
/**
|
||||
* Indicates that the state of the instance can be saved and restored.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface StateSettable {
|
||||
/**
|
||||
* Sets the instance's state.
|
||||
*
|
||||
* @param state State. The given argument must have been retrieved
|
||||
* by a call to {@link #getState()}.
|
||||
*
|
||||
* @throws org.apache.commons.math4.exception.MathUnsupportedOperationException
|
||||
* if not implemented.
|
||||
*/
|
||||
void setState(RandomSource.State state);
|
||||
|
||||
/**
|
||||
* Gets the instance's state.
|
||||
*
|
||||
* @return the current state. The given argument can then be passed
|
||||
* to {@link #setState(RandomSource.State)} in order to recover the
|
||||
* current state.
|
||||
*
|
||||
* @throws org.apache.commons.math4.exception.MathUnsupportedOperationException
|
||||
* if not implemented.
|
||||
*/
|
||||
RandomSource.State getState();
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <h3>Base classes for the {@link org.apache.commons.math4.rng.UniformRandomProvider
|
||||
* generation of uniformly distributed random numbers}.
|
||||
* </h3>
|
||||
*
|
||||
* <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.math4.rng.internal.source32.RandomIntSource int}
|
||||
* values are defined in the
|
||||
* {@link org.apache.commons.math4.rng.internal.source32 source32} package.
|
||||
* <br>
|
||||
* Implementations that produce
|
||||
* {@link org.apache.commons.math4.rng.internal.source64.RandomLongSource long}
|
||||
* values are defined in the
|
||||
* {@link org.apache.commons.math4.rng.internal.source64 source64} package.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Each implementation must have an identifier in
|
||||
* {@link org.apache.commons.math4.rng.internal.ProviderBuilder.RandomSourceInternal}
|
||||
* which must be referred to from the {@link org.apache.commons.math4.rng.RandomSource public API}.
|
||||
* </p>
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.rng.internal;
|
|
@ -1,208 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* This abstract class implements the WELL class of pseudo-random number
|
||||
* generator from François Panneton, Pierre L'Ecuyer and Makoto
|
||||
* Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public abstract class AbstractWell extends IntProvider {
|
||||
/** Current index in the bytes pool. */
|
||||
protected int index;
|
||||
/** Bytes pool. */
|
||||
protected final int[] v;
|
||||
|
||||
/**
|
||||
* Creates a new random number generator using an int array seed.
|
||||
*
|
||||
* @param k Number of bits in the pool (not necessarily a multiple of 32).
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
protected AbstractWell(final int k,
|
||||
final int[] seed) {
|
||||
final int r = calculateBlockCount(k);
|
||||
v = new int[r];
|
||||
index = 0;
|
||||
|
||||
// Initialize the pool content.
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
final int[] s = Arrays.copyOf(v, v.length + 1);
|
||||
s[v.length] = index;
|
||||
|
||||
return NumberFactory.makeByteArray(s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != (v.length + 1) * 4) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final int[] tmp = NumberFactory.makeIntArray(s);
|
||||
|
||||
System.arraycopy(tmp, 0, v, 0, v.length);
|
||||
index = tmp[v.length];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reinitialize the generator as if just built with the given int array seed.
|
||||
*
|
||||
* <p>The state of the generator is exactly the same as a new generator built
|
||||
* with the same seed.</p>
|
||||
*
|
||||
* @param seed Seed. Cannot be null.
|
||||
*/
|
||||
private void setSeedInternal(final int[] seed) {
|
||||
System.arraycopy(seed, 0, v, 0, Math.min(seed.length, v.length));
|
||||
|
||||
if (seed.length < v.length) {
|
||||
for (int i = seed.length; i < v.length; ++i) {
|
||||
final long current = v[i - seed.length];
|
||||
v[i] = (int) ((1812433253L * (current ^ (current >> 30)) + i) & 0xffffffffL);
|
||||
}
|
||||
}
|
||||
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the number of 32-bits blocks.
|
||||
*
|
||||
* @param k Number of bits in the pool (not necessarily a multiple of 32).
|
||||
* @return the number of 32-bits blocks.
|
||||
*/
|
||||
private static int calculateBlockCount(final int k) {
|
||||
// the bits pool contains k bits, k = r w - p where r is the number
|
||||
// of w bits blocks, w is the block size (always 32 in the original paper)
|
||||
// and p is the number of unused bits in the last block
|
||||
final int w = 32;
|
||||
final int r = (k + w - 1) / w;
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inner class used to store the indirection index table which is fixed for a given
|
||||
* type of WELL class of pseudo-random number generator.
|
||||
*/
|
||||
protected static final class IndexTable {
|
||||
/** Index indirection table giving for each index its predecessor taking table size into account. */
|
||||
private final int[] iRm1;
|
||||
/** Index indirection table giving for each index its second predecessor taking table size into account. */
|
||||
private final int[] iRm2;
|
||||
/** Index indirection table giving for each index the value index + m1 taking table size into account. */
|
||||
private final int[] i1;
|
||||
/** Index indirection table giving for each index the value index + m2 taking table size into account. */
|
||||
private final int[] i2;
|
||||
/** Index indirection table giving for each index the value index + m3 taking table size into account. */
|
||||
private final int[] i3;
|
||||
|
||||
/** Creates a new pre-calculated indirection index table.
|
||||
* @param k number of bits in the pool (not necessarily a multiple of 32)
|
||||
* @param m1 first parameter of the algorithm
|
||||
* @param m2 second parameter of the algorithm
|
||||
* @param m3 third parameter of the algorithm
|
||||
*/
|
||||
public IndexTable(final int k, final int m1, final int m2, final int m3) {
|
||||
|
||||
final int r = calculateBlockCount(k);
|
||||
|
||||
// precompute indirection index tables. These tables are used for optimizing access
|
||||
// they allow saving computations like "(j + r - 2) % r" with costly modulo operations
|
||||
iRm1 = new int[r];
|
||||
iRm2 = new int[r];
|
||||
i1 = new int[r];
|
||||
i2 = new int[r];
|
||||
i3 = new int[r];
|
||||
for (int j = 0; j < r; ++j) {
|
||||
iRm1[j] = (j + r - 1) % r;
|
||||
iRm2[j] = (j + r - 2) % r;
|
||||
i1[j] = (j + m1) % r;
|
||||
i2[j] = (j + m2) % r;
|
||||
i3[j] = (j + m3) % r;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the predecessor of the given index modulo the table size.
|
||||
* @param index the index to look at
|
||||
* @return (index - 1) % table size
|
||||
*/
|
||||
public int getIndexPred(final int index) {
|
||||
return iRm1[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the second predecessor of the given index modulo the table size.
|
||||
* @param index the index to look at
|
||||
* @return (index - 2) % table size
|
||||
*/
|
||||
public int getIndexPred2(final int index) {
|
||||
return iRm2[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index + M1 modulo the table size.
|
||||
* @param index the index to look at
|
||||
* @return (index + M1) % table size
|
||||
*/
|
||||
public int getIndexM1(final int index) {
|
||||
return i1[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index + M2 modulo the table size.
|
||||
* @param index the index to look at
|
||||
* @return (index + M2) % table size
|
||||
*/
|
||||
public int getIndexM2(final int index) {
|
||||
return i2[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index + M3 modulo the table size.
|
||||
* @param index the index to look at
|
||||
* @return (index + M3) % table size
|
||||
*/
|
||||
public int getIndexM3(final int index) {
|
||||
return i3[index];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,270 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* A fast cryptographic pseudo-random number generator.
|
||||
* <p>
|
||||
* ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit
|
||||
* random numbers.
|
||||
* ISAAC has been designed to be cryptographically secure and is inspired
|
||||
* by RC4.
|
||||
* Cycles are guaranteed to be at least 2<sup>40</sup> values long, and they
|
||||
* are 2<sup>8295</sup> values long on average.
|
||||
* The results are uniformly distributed, unbiased, and unpredictable unless
|
||||
* you know the seed.
|
||||
* <p>
|
||||
* This code is based (with minor changes and improvements) on the original
|
||||
* implementation of the algorithm by Bob Jenkins.
|
||||
*
|
||||
* @see <a href="http://burtleburtle.net/bob/rand/isaacafa.html">
|
||||
* ISAAC: a fast cryptographic pseudo-random number generator</a>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ISAACRandom extends IntProvider {
|
||||
/** Log of size of rsl[] and mem[] */
|
||||
private static final int SIZE_L = 8;
|
||||
/** Size of rsl[] and mem[] */
|
||||
private static final int SIZE = 1 << SIZE_L;
|
||||
/** Half-size of rsl[] and mem[] */
|
||||
private static final int H_SIZE = SIZE >> 1;
|
||||
/** For pseudo-random lookup */
|
||||
private static final int MASK = SIZE - 1 << 2;
|
||||
/** The golden ratio */
|
||||
private static final int GLD_RATIO = 0x9e3779b9;
|
||||
/** The results given to the user */
|
||||
private final int[] rsl = new int[SIZE];
|
||||
/** The internal state */
|
||||
private final int[] mem = new int[SIZE];
|
||||
/** Count through the results in rsl[] */
|
||||
private int count;
|
||||
/** Accumulator */
|
||||
private int isaacA;
|
||||
/** The last result */
|
||||
private int isaacB;
|
||||
/** Counter, guarantees cycle is at least 2^40 */
|
||||
private int isaacC;
|
||||
/** Service variable. */
|
||||
private final int[] arr = new int[8];
|
||||
/** Service variable. */
|
||||
private int isaacX;
|
||||
/** Service variable. */
|
||||
private int isaacI;
|
||||
/** Service variable. */
|
||||
private int isaacJ;
|
||||
|
||||
/**
|
||||
* Creates a new ISAAC random number generator.
|
||||
*
|
||||
* @param seed Initial seed
|
||||
*/
|
||||
public ISAACRandom(int[] seed) {
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
final int[] sRsl = Arrays.copyOf(rsl, SIZE);
|
||||
final int[] sMem = Arrays.copyOf(mem, SIZE);
|
||||
final int[] sRem = Arrays.copyOf(new int[] { count, isaacA, isaacB, isaacC }, 4);
|
||||
|
||||
final int[] s = new int[2 * SIZE + sRem.length];
|
||||
System.arraycopy(sRsl, 0, s, 0, SIZE);
|
||||
System.arraycopy(sMem, 0, s, SIZE, SIZE);
|
||||
System.arraycopy(sRem, 0, s, 2 * SIZE, sRem.length);
|
||||
|
||||
return NumberFactory.makeByteArray(s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != (2 * SIZE + 4) * 4) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final int[] tmp = NumberFactory.makeIntArray(s);
|
||||
|
||||
System.arraycopy(tmp, 0, rsl, 0, SIZE);
|
||||
System.arraycopy(tmp, SIZE, mem, 0, SIZE);
|
||||
final int offset = 2 * SIZE;
|
||||
count = tmp[offset];
|
||||
isaacA = tmp[offset + 1];
|
||||
isaacB = tmp[offset + 2];
|
||||
isaacC = tmp[offset + 3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reseeds the RNG.
|
||||
*
|
||||
* @param seed Seed. Cannot be null.
|
||||
*/
|
||||
private void setSeedInternal(int[] seed) {
|
||||
final int seedLen = seed.length;
|
||||
final int rslLen = rsl.length;
|
||||
System.arraycopy(seed, 0, rsl, 0, Math.min(seedLen, rslLen));
|
||||
if (seedLen < rslLen) {
|
||||
for (int j = seedLen; j < rslLen; j++) {
|
||||
long k = rsl[j - seedLen];
|
||||
rsl[j] = (int) (0x6c078965L * (k ^ k >> 30) + j & 0xffffffffL);
|
||||
}
|
||||
}
|
||||
initState();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
if (count < 0) {
|
||||
isaac();
|
||||
count = SIZE - 1;
|
||||
}
|
||||
return rsl[count--];
|
||||
}
|
||||
|
||||
/** Generate 256 results */
|
||||
private void isaac() {
|
||||
isaacI = 0;
|
||||
isaacJ = H_SIZE;
|
||||
isaacB += ++isaacC;
|
||||
while (isaacI < H_SIZE) {
|
||||
isaac2();
|
||||
}
|
||||
isaacJ = 0;
|
||||
while (isaacJ < H_SIZE) {
|
||||
isaac2();
|
||||
}
|
||||
}
|
||||
|
||||
/** Intermediate internal loop. */
|
||||
private void isaac2() {
|
||||
isaacX = mem[isaacI];
|
||||
isaacA ^= isaacA << 13;
|
||||
isaacA += mem[isaacJ++];
|
||||
isaac3();
|
||||
isaacX = mem[isaacI];
|
||||
isaacA ^= isaacA >>> 6;
|
||||
isaacA += mem[isaacJ++];
|
||||
isaac3();
|
||||
isaacX = mem[isaacI];
|
||||
isaacA ^= isaacA << 2;
|
||||
isaacA += mem[isaacJ++];
|
||||
isaac3();
|
||||
isaacX = mem[isaacI];
|
||||
isaacA ^= isaacA >>> 16;
|
||||
isaacA += mem[isaacJ++];
|
||||
isaac3();
|
||||
}
|
||||
|
||||
/** Lowest level internal loop. */
|
||||
private void isaac3() {
|
||||
mem[isaacI] = mem[(isaacX & MASK) >> 2] + isaacA + isaacB;
|
||||
isaacB = mem[(mem[isaacI] >> SIZE_L & MASK) >> 2] + isaacX;
|
||||
rsl[isaacI++] = isaacB;
|
||||
}
|
||||
|
||||
/** Initialize, or reinitialize, this instance of rand. */
|
||||
private void initState() {
|
||||
isaacA = 0;
|
||||
isaacB = 0;
|
||||
isaacC = 0;
|
||||
for (int j = 0; j < arr.length; j++) {
|
||||
arr[j] = GLD_RATIO;
|
||||
}
|
||||
for (int j = 0; j < 4; j++) {
|
||||
shuffle();
|
||||
}
|
||||
// fill in mem[] with messy stuff
|
||||
for (int j = 0; j < SIZE; j += 8) {
|
||||
arr[0] += rsl[j];
|
||||
arr[1] += rsl[j + 1];
|
||||
arr[2] += rsl[j + 2];
|
||||
arr[3] += rsl[j + 3];
|
||||
arr[4] += rsl[j + 4];
|
||||
arr[5] += rsl[j + 5];
|
||||
arr[6] += rsl[j + 6];
|
||||
arr[7] += rsl[j + 7];
|
||||
shuffle();
|
||||
setState(j);
|
||||
}
|
||||
// second pass makes all of seed affect all of mem
|
||||
for (int j = 0; j < SIZE; j += 8) {
|
||||
arr[0] += mem[j];
|
||||
arr[1] += mem[j + 1];
|
||||
arr[2] += mem[j + 2];
|
||||
arr[3] += mem[j + 3];
|
||||
arr[4] += mem[j + 4];
|
||||
arr[5] += mem[j + 5];
|
||||
arr[6] += mem[j + 6];
|
||||
arr[7] += mem[j + 7];
|
||||
shuffle();
|
||||
setState(j);
|
||||
}
|
||||
isaac();
|
||||
count = SIZE - 1;
|
||||
}
|
||||
|
||||
/** Shuffle array. */
|
||||
private void shuffle() {
|
||||
arr[0] ^= arr[1] << 11;
|
||||
arr[3] += arr[0];
|
||||
arr[1] += arr[2];
|
||||
arr[1] ^= arr[2] >>> 2;
|
||||
arr[4] += arr[1];
|
||||
arr[2] += arr[3];
|
||||
arr[2] ^= arr[3] << 8;
|
||||
arr[5] += arr[2];
|
||||
arr[3] += arr[4];
|
||||
arr[3] ^= arr[4] >>> 16;
|
||||
arr[6] += arr[3];
|
||||
arr[4] += arr[5];
|
||||
arr[4] ^= arr[5] << 10;
|
||||
arr[7] += arr[4];
|
||||
arr[5] += arr[6];
|
||||
arr[5] ^= arr[6] >>> 4;
|
||||
arr[0] += arr[5];
|
||||
arr[6] += arr[7];
|
||||
arr[6] ^= arr[7] << 8;
|
||||
arr[1] += arr[6];
|
||||
arr[7] += arr[0];
|
||||
arr[7] ^= arr[0] >>> 9;
|
||||
arr[2] += arr[7];
|
||||
arr[0] += arr[1];
|
||||
}
|
||||
|
||||
/** Set the state by copying the internal arrays.
|
||||
*
|
||||
* @param start First index into {@link #mem} array.
|
||||
*/
|
||||
private void setState(int start) {
|
||||
mem[start] = arr[0];
|
||||
mem[start + 1] = arr[1];
|
||||
mem[start + 2] = arr[2];
|
||||
mem[start + 3] = arr[3];
|
||||
mem[start + 4] = arr[4];
|
||||
mem[start + 5] = arr[5];
|
||||
mem[start + 6] = arr[6];
|
||||
mem[start + 7] = arr[7];
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
import org.apache.commons.math4.rng.internal.BaseProvider;
|
||||
|
||||
/**
|
||||
* Base class for all implementations that provide an {@code int}-based
|
||||
* source randomness.
|
||||
*/
|
||||
public abstract class IntProvider
|
||||
extends BaseProvider
|
||||
implements RandomIntSource {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public abstract int next();
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int nextInt() {
|
||||
return next();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean nextBoolean() {
|
||||
return NumberFactory.makeBoolean(nextInt());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double nextDouble() {
|
||||
return NumberFactory.makeDouble(nextInt(), nextInt());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public float nextFloat() {
|
||||
return NumberFactory.makeFloat(nextInt());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long nextLong() {
|
||||
return NumberFactory.makeLong(nextInt(), nextInt());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void nextBytes(byte[] bytes) {
|
||||
nextBytesFill(this, bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void nextBytes(byte[] bytes,
|
||||
int start,
|
||||
int len) {
|
||||
if (start < 0 ||
|
||||
start >= bytes.length) {
|
||||
throw new OutOfRangeException(start, 0, bytes.length);
|
||||
}
|
||||
if (len < 0 ||
|
||||
len > bytes.length - start) {
|
||||
throw new OutOfRangeException(len, 0, bytes.length - start);
|
||||
}
|
||||
|
||||
nextBytesFill(this, bytes, start, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates random bytes and places them into a user-supplied array.
|
||||
*
|
||||
* <p>
|
||||
* The array is filled with bytes extracted from random {@code int} values.
|
||||
* This implies that the number of random bytes generated may be larger than
|
||||
* the length of the byte array.
|
||||
* </p>
|
||||
*
|
||||
* @param source Source of randomness.
|
||||
* @param bytes Array in which to put the generated bytes. Cannot be null.
|
||||
* @param start Index at which to start inserting the generated bytes.
|
||||
* @param len Number of bytes to insert.
|
||||
*/
|
||||
static void nextBytesFill(RandomIntSource source,
|
||||
byte[] bytes,
|
||||
int start,
|
||||
int len) {
|
||||
int index = start; // Index of first insertion.
|
||||
|
||||
// Index of first insertion plus multiple of 4 part of length
|
||||
// (i.e. length with 2 least significant bits unset).
|
||||
final int indexLoopLimit = index + (len & 0x7ffffffc);
|
||||
|
||||
// Start filling in the byte array, 4 bytes at a time.
|
||||
while (index < indexLoopLimit) {
|
||||
final int random = source.next();
|
||||
bytes[index++] = (byte) random;
|
||||
bytes[index++] = (byte) (random >>> 8);
|
||||
bytes[index++] = (byte) (random >>> 16);
|
||||
bytes[index++] = (byte) (random >>> 24);
|
||||
}
|
||||
|
||||
final int indexLimit = start + len; // Index of last insertion + 1.
|
||||
|
||||
// Fill in the remaining bytes.
|
||||
if (index < indexLimit) {
|
||||
int random = source.next();
|
||||
while (true) {
|
||||
bytes[index++] = (byte) random;
|
||||
if (index < indexLimit) {
|
||||
random >>>= 8;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
import java.util.Random;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
/**
|
||||
* A provider that uses the {@link Random#nextInt()} method of the JDK's
|
||||
* {@code Random} class as the source of randomness.
|
||||
*
|
||||
* <p>
|
||||
* <b>Caveat:</b> All the other calls will be redirected to the methods
|
||||
* implemented within this library.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The state of this source of randomness is saved and restored through
|
||||
* the serialization of the {@link Random} instance.
|
||||
* </p>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class JDKRandom extends IntProvider {
|
||||
/** Delegate. Cannot be "final" (to allow serialization). */
|
||||
private Random delegate;
|
||||
|
||||
/**
|
||||
* Creates an instance with the given seed.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public JDKRandom(Long seed) {
|
||||
delegate = new Random(seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see Random#nextInt()
|
||||
*/
|
||||
@Override
|
||||
public int next() {
|
||||
return delegate.nextInt();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
try {
|
||||
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
|
||||
// Serialize the "delegate".
|
||||
oos.writeObject(delegate);
|
||||
|
||||
return bos.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// Workaround checked exception.
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
try {
|
||||
final ByteArrayInputStream bis = new ByteArrayInputStream(s);
|
||||
final ObjectInputStream ois = new ObjectInputStream(bis);
|
||||
|
||||
delegate = (Random) ois.readObject();
|
||||
} catch (ClassNotFoundException|IOException e) {
|
||||
// Workaround checked exception.
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,230 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* This class implements a powerful pseudo-random number generator
|
||||
* developed by Makoto Matsumoto and Takuji Nishimura during
|
||||
* 1996-1997.
|
||||
*
|
||||
* <p>
|
||||
* This generator features an extremely long period
|
||||
* (2<sup>19937</sup>-1) and 623-dimensional equidistribution up to
|
||||
* 32 bits accuracy. The home page for this generator is located at
|
||||
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">
|
||||
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html</a>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This generator is described in a paper by Makoto Matsumoto and
|
||||
* Takuji Nishimura in 1998:
|
||||
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf">
|
||||
* Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random
|
||||
* Number Generator</a>,
|
||||
* ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1,
|
||||
* January 1998, pp 3--30
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This class is mainly a Java port of the
|
||||
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html">
|
||||
* 2002-01-26 version of the generator</a> written in C by Makoto Matsumoto
|
||||
* and Takuji Nishimura. Here is their original copyright:
|
||||
* </p>
|
||||
*
|
||||
* <table border="0" width="80%" cellpadding="10" align="center" bgcolor="#E0E0E0">
|
||||
* <tr><td>Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
||||
* All rights reserved.</td></tr>
|
||||
*
|
||||
* <tr><td>Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* <ol>
|
||||
* <li>Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.</li>
|
||||
* <li>Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.</li>
|
||||
* <li>The names of its contributors may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.</li>
|
||||
* </ol></td></tr>
|
||||
*
|
||||
* <tr><td><strong>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.</strong></td></tr>
|
||||
* </table>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class MersenneTwister extends IntProvider {
|
||||
/** Mask 32 most significant bits. */
|
||||
private static final long INT_MASK_LONG = 0xffffffffL;
|
||||
/** Most significant w-r bits. */
|
||||
private static final long UPPER_MASK_LONG = 0x80000000L;
|
||||
/** Least significant r bits */
|
||||
private static final long LOWER_MASK_LONG = 0x7fffffffL;
|
||||
/** Most significant w-r bits. */
|
||||
private static final int UPPER_MASK = 0x80000000;
|
||||
/** Least significant r bits */
|
||||
private static final int LOWER_MASK = 0x7fffffff;
|
||||
/** Size of the bytes pool. */
|
||||
private static final int N = 624;
|
||||
/** Period second parameter. */
|
||||
private static final int M = 397;
|
||||
/** X * MATRIX_A for X = {0, 1}. */
|
||||
private static final int[] MAG01 = { 0x0, 0x9908b0df };
|
||||
/** Bytes pool. */
|
||||
private int[] mt = new int[N];
|
||||
/** Current index in the bytes pool. */
|
||||
private int mti;
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public MersenneTwister(int[] seed) {
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
final int[] s = Arrays.copyOf(mt, N + 1);
|
||||
s[N] = mti;
|
||||
|
||||
return NumberFactory.makeByteArray(s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != (N + 1) * 4) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final int[] tmp = NumberFactory.makeIntArray(s);
|
||||
|
||||
System.arraycopy(tmp, 0, mt, 0, N);
|
||||
mti = tmp[N];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reinitializes the generator as if just built with the given seed.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
private void setSeedInternal(int[] seed) {
|
||||
initState(19650218);
|
||||
int i = 1;
|
||||
int j = 0;
|
||||
|
||||
for (int k = Math.max(N, seed.length); k != 0; k--) {
|
||||
final long l0 = (mt[i] & LOWER_MASK_LONG) | ((mt[i] < 0) ? UPPER_MASK_LONG : 0);
|
||||
final long l1 = (mt[i-1] & LOWER_MASK_LONG) | ((mt[i-1] < 0) ? UPPER_MASK_LONG : 0);
|
||||
final long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1664525l)) + seed[j] + j; // non linear
|
||||
mt[i] = (int) (l & INT_MASK_LONG);
|
||||
i++; j++;
|
||||
if (i >= N) {
|
||||
mt[0] = mt[N - 1];
|
||||
i = 1;
|
||||
}
|
||||
if (j >= seed.length) {
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = N - 1; k != 0; k--) {
|
||||
final long l0 = (mt[i] & LOWER_MASK_LONG) | ((mt[i] < 0) ? UPPER_MASK_LONG : 0);
|
||||
final long l1 = (mt[i-1] & LOWER_MASK_LONG) | ((mt[i-1] < 0) ? UPPER_MASK_LONG : 0);
|
||||
final long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1566083941l)) - i; // non linear
|
||||
mt[i] = (int) (l & INT_MASK_LONG);
|
||||
i++;
|
||||
if (i >= N) {
|
||||
mt[0] = mt[N - 1];
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
mt[0] = UPPER_MASK; // MSB is 1; assuring non-zero initial array
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the internal state of this instance.
|
||||
*
|
||||
* @param seed Seed.
|
||||
*/
|
||||
private void initState(int seed) {
|
||||
long longMT = seed & INT_MASK_LONG;
|
||||
mt[0]= (int) longMT;
|
||||
for (mti = 1; mti < N; ++mti) {
|
||||
longMT = (1812433253L * (longMT ^ (longMT >> 30)) + mti) & INT_MASK_LONG;
|
||||
mt[mti]= (int) longMT;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
int y;
|
||||
|
||||
if (mti >= N) { // Generate N words at one time.
|
||||
int mtNext = mt[0];
|
||||
for (int k = 0; k < N - M; ++k) {
|
||||
int mtCurr = mtNext;
|
||||
mtNext = mt[k + 1];
|
||||
y = (mtCurr & UPPER_MASK) | (mtNext & LOWER_MASK);
|
||||
mt[k] = mt[k + M] ^ (y >>> 1) ^ MAG01[y & 1];
|
||||
}
|
||||
for (int k = N - M; k < N - 1; ++k) {
|
||||
int mtCurr = mtNext;
|
||||
mtNext = mt[k + 1];
|
||||
y = (mtCurr & UPPER_MASK) | (mtNext & LOWER_MASK);
|
||||
mt[k] = mt[k + (M - N)] ^ (y >>> 1) ^ MAG01[y & 1];
|
||||
}
|
||||
y = (mtNext & UPPER_MASK) | (mt[0] & LOWER_MASK);
|
||||
mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAG01[y & 1];
|
||||
|
||||
mti = 0;
|
||||
}
|
||||
|
||||
y = mt[mti++];
|
||||
|
||||
// Tempering.
|
||||
y ^= y >>> 11;
|
||||
y ^= (y << 7) & 0x9d2c5680;
|
||||
y ^= (y << 15) & 0xefc60000;
|
||||
y ^= y >>> 18;
|
||||
|
||||
return y;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* Source of randomness that generate values of type {@code int}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface RandomIntSource {
|
||||
/**
|
||||
* @return the next random value.
|
||||
*/
|
||||
int next();
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL1024a pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Well1024a extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 1024;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 3;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 24;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 10;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well1024a(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
|
||||
final int v0 = v[index];
|
||||
final int vM1 = v[TABLE.getIndexM1(index)];
|
||||
final int vM2 = v[TABLE.getIndexM2(index)];
|
||||
final int vM3 = v[TABLE.getIndexM3(index)];
|
||||
|
||||
final int z0 = v[indexRm1];
|
||||
final int z1 = v0 ^ (vM1 ^ (vM1 >>> 8));
|
||||
final int z2 = (vM2 ^ (vM2 << 19)) ^ (vM3 ^ (vM3 << 14));
|
||||
final int z3 = z1 ^ z2;
|
||||
final int z4 = (z0 ^ (z0 << 11)) ^ (z1 ^ (z1 << 7)) ^ (z2 ^ (z2 << 13));
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
index = indexRm1;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL19937a pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Well19937a extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 19937;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 70;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 179;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 449;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well19937a(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
final int indexRm2 = TABLE.getIndexPred2(index);
|
||||
|
||||
final int v0 = v[index];
|
||||
final int vM1 = v[TABLE.getIndexM1(index)];
|
||||
final int vM2 = v[TABLE.getIndexM2(index)];
|
||||
final int vM3 = v[TABLE.getIndexM3(index)];
|
||||
|
||||
final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]);
|
||||
final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27));
|
||||
final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1));
|
||||
final int z3 = z1 ^ z2;
|
||||
final int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21));
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
v[indexRm2] &= 0x80000000;
|
||||
index = indexRm1;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL19937c pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 2.2
|
||||
*/
|
||||
public class Well19937c extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 19937;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 70;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 179;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 449;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well19937c(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
final int indexRm2 = TABLE.getIndexPred2(index);
|
||||
|
||||
final int v0 = v[index];
|
||||
final int vM1 = v[TABLE.getIndexM1(index)];
|
||||
final int vM2 = v[TABLE.getIndexM2(index)];
|
||||
final int vM3 = v[TABLE.getIndexM3(index)];
|
||||
|
||||
final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]);
|
||||
final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27));
|
||||
final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1));
|
||||
final int z3 = z1 ^ z2;
|
||||
int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21));
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
v[indexRm2] &= 0x80000000;
|
||||
index = indexRm1;
|
||||
|
||||
// add Matsumoto-Kurita tempering
|
||||
// to get a maximally-equidistributed generator
|
||||
z4 ^= (z4 << 7) & 0xe46e1700;
|
||||
z4 ^= (z4 << 15) & 0x9b868000;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL44497a pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Well44497a extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 44497;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 23;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 481;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 229;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well44497a(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
final int indexRm2 = TABLE.getIndexPred2(index);
|
||||
|
||||
final int v0 = v[index];
|
||||
final int vM1 = v[TABLE.getIndexM1(index)];
|
||||
final int vM2 = v[TABLE.getIndexM2(index)];
|
||||
final int vM3 = v[TABLE.getIndexM3(index)];
|
||||
|
||||
// the values below include the errata of the original article
|
||||
final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
|
||||
final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30));
|
||||
final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
|
||||
final int z3 = z1 ^ z2;
|
||||
final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
|
||||
final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime;
|
||||
final int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
v[indexRm2] &= 0xFFFF8000;
|
||||
index = indexRm1;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL44497b pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Well44497b extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 44497;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 23;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 481;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 229;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well44497b(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
// compute raw value given by WELL44497a generator
|
||||
// which is NOT maximally-equidistributed
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
final int indexRm2 = TABLE.getIndexPred2(index);
|
||||
|
||||
final int v0 = v[index];
|
||||
final int vM1 = v[TABLE.getIndexM1(index)];
|
||||
final int vM2 = v[TABLE.getIndexM2(index)];
|
||||
final int vM3 = v[TABLE.getIndexM3(index)];
|
||||
|
||||
// the values below include the errata of the original article
|
||||
final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]);
|
||||
final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30));
|
||||
final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26);
|
||||
final int z3 = z1 ^ z2;
|
||||
final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff;
|
||||
final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime;
|
||||
int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3;
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
v[indexRm2] &= 0xFFFF8000;
|
||||
index = indexRm1;
|
||||
|
||||
// add Matsumoto-Kurita tempering
|
||||
// to get a maximally-equidistributed generator
|
||||
z4 ^= (z4 << 7) & 0x93dd1400;
|
||||
z4 ^= (z4 << 15) & 0xfa118000;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source32;
|
||||
|
||||
/**
|
||||
* This class implements the WELL512a pseudo-random number generator
|
||||
* from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
|
||||
* <p>
|
||||
* This generator is described in a paper by François Panneton,
|
||||
* Pierre L'Ecuyer and Makoto Matsumoto
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">
|
||||
* Improved Long-Period Generators Based on Linear Recurrences Modulo 2</a>
|
||||
* ACM Transactions on Mathematical Software, 32, 1 (2006).
|
||||
* The errata for the paper are in
|
||||
* <a href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="http://www.iro.umontreal.ca/~panneton/WELLRNG.html">WELL Random number generator</a>
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Well512a extends AbstractWell {
|
||||
/** Number of bits in the pool. */
|
||||
private static final int K = 512;
|
||||
/** First parameter of the algorithm. */
|
||||
private static final int M1 = 13;
|
||||
/** Second parameter of the algorithm. */
|
||||
private static final int M2 = 9;
|
||||
/** Third parameter of the algorithm. */
|
||||
private static final int M3 = 5;
|
||||
/** The indirection index table. */
|
||||
private static final IndexTable TABLE = new IndexTable(K, M1, M2, M3);
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public Well512a(int[] seed) {
|
||||
super(K, seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int next() {
|
||||
final int indexRm1 = TABLE.getIndexPred(index);
|
||||
|
||||
final int vi = v[index];
|
||||
final int vi1 = v[TABLE.getIndexM1(index)];
|
||||
final int vi2 = v[TABLE.getIndexM2(index)];
|
||||
final int z0 = v[indexRm1];
|
||||
|
||||
// the values below include the errata of the original article
|
||||
final int z1 = (vi ^ (vi << 16)) ^ (vi1 ^ (vi1 << 15));
|
||||
final int z2 = vi2 ^ (vi2 >>> 11);
|
||||
final int z3 = z1 ^ z2;
|
||||
final int z4 = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28) ^ (z3 ^ ((z3 << 5) & 0xda442d24));
|
||||
|
||||
v[index] = z3;
|
||||
v[indexRm1] = z4;
|
||||
index = indexRm1;
|
||||
|
||||
return z4;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <h3>
|
||||
* Concrete algorithms for {@code int}-based sources of randomness
|
||||
* </h3>
|
||||
*
|
||||
* <p>
|
||||
* <b>For internal use only:</b> Direct access to classes in this package
|
||||
* is discouraged, as they could be modified without notice.
|
||||
* </p>
|
||||
*
|
||||
* <p><b>Notes for developers</b></p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>
|
||||
* A source of randomness must inherit from
|
||||
* {@link org.apache.commons.math4.rng.internal.source32.IntProvider}
|
||||
* </li>
|
||||
* <li>
|
||||
* The "provider" must specify <em>one</em> way for setting the seed.
|
||||
* For a given seed, the generated sequence must always be the same.
|
||||
* </li>
|
||||
* <li>
|
||||
* The "provider" must implement methods {@code getStateInternal} and
|
||||
* {@code setStateInternal} in order to save and restore the state of an
|
||||
* instance (cf. {@link org.apache.commons.math4.rng.internal.BaseProvider}).
|
||||
* </li>
|
||||
* <li>
|
||||
* When a new class is implemented here, user-access to it must be provided
|
||||
* through associated {@link org.apache.commons.math4.rng.RandomSource
|
||||
* factory methods}.
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.rng.internal.source32;
|
|
@ -1,141 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
import org.apache.commons.math4.rng.internal.BaseProvider;
|
||||
|
||||
/**
|
||||
* Base class for all implementations that provide a {@code long}-based
|
||||
* source randomness.
|
||||
*/
|
||||
public abstract class LongProvider
|
||||
extends BaseProvider
|
||||
implements RandomLongSource {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public abstract long next();
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long nextLong() {
|
||||
return next();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int nextInt() {
|
||||
return NumberFactory.makeInt(nextLong());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public double nextDouble() {
|
||||
return NumberFactory.makeDouble(nextLong());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean nextBoolean() {
|
||||
return NumberFactory.makeBoolean(nextLong());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public float nextFloat() {
|
||||
return NumberFactory.makeFloat(nextInt());
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void nextBytes(byte[] bytes) {
|
||||
nextBytesFill(this, bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void nextBytes(byte[] bytes,
|
||||
int start,
|
||||
int len) {
|
||||
if (start < 0 ||
|
||||
start >= bytes.length) {
|
||||
throw new OutOfRangeException(start, 0, bytes.length);
|
||||
}
|
||||
if (len < 0 ||
|
||||
len > bytes.length - start) {
|
||||
throw new OutOfRangeException(len, 0, bytes.length - start);
|
||||
}
|
||||
|
||||
nextBytesFill(this, bytes, start, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates random bytes and places them into a user-supplied array.
|
||||
*
|
||||
* <p>
|
||||
* The array is filled with bytes extracted from random {@code long} values.
|
||||
* This implies that the number of random bytes generated may be larger than
|
||||
* the length of the byte array.
|
||||
* </p>
|
||||
*
|
||||
* @param source Source of randomness.
|
||||
* @param bytes Array in which to put the generated bytes. Cannot be null.
|
||||
* @param start Index at which to start inserting the generated bytes.
|
||||
* @param len Number of bytes to insert.
|
||||
*/
|
||||
static void nextBytesFill(RandomLongSource source,
|
||||
byte[] bytes,
|
||||
int start,
|
||||
int len) {
|
||||
int index = start; // Index of first insertion.
|
||||
|
||||
// Index of first insertion plus multiple of 8 part of length
|
||||
// (i.e. length with 3 least significant bits unset).
|
||||
final int indexLoopLimit = index + (len & 0x7ffffff8);
|
||||
|
||||
// Start filling in the byte array, 8 bytes at a time.
|
||||
while (index < indexLoopLimit) {
|
||||
final long random = source.next();
|
||||
bytes[index++] = (byte) random;
|
||||
bytes[index++] = (byte) (random >>> 8);
|
||||
bytes[index++] = (byte) (random >>> 16);
|
||||
bytes[index++] = (byte) (random >>> 24);
|
||||
bytes[index++] = (byte) (random >>> 32);
|
||||
bytes[index++] = (byte) (random >>> 40);
|
||||
bytes[index++] = (byte) (random >>> 48);
|
||||
bytes[index++] = (byte) (random >>> 56);
|
||||
}
|
||||
|
||||
final int indexLimit = start + len; // Index of last insertion + 1.
|
||||
|
||||
// Fill in the remaining bytes.
|
||||
if (index < indexLimit) {
|
||||
long random = source.next();
|
||||
while (true) {
|
||||
bytes[index++] = (byte) random;
|
||||
if (index < indexLimit) {
|
||||
random >>>= 8;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,201 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* This class provides the 64-bits version of the originally 32-bits
|
||||
* {@link org.apache.commons.math4.rng.internal.source32.MersenneTwister
|
||||
* Mersenne Twister}.
|
||||
*
|
||||
* <p>
|
||||
* This class is mainly a Java port of
|
||||
* <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt64.html">
|
||||
* the 2014/2/23 version of the generator
|
||||
* </a> written in C by Takuji Nishimura and Makoto Matsumoto.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Here is their original copyright:
|
||||
* </p>
|
||||
*
|
||||
* <table border="0" width="80%" cellpadding="10" align="center" bgcolor="#E0E0E0">
|
||||
* <tr><td>Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
|
||||
* All rights reserved.</td></tr>
|
||||
*
|
||||
* <tr><td>Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* <ol>
|
||||
* <li>Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.</li>
|
||||
* <li>Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.</li>
|
||||
* <li>The names of its contributors may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.</li>
|
||||
* </ol></td></tr>
|
||||
*
|
||||
* <tr><td><strong>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.</strong></td></tr>
|
||||
* </table>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class MersenneTwister64 extends LongProvider {
|
||||
/** Size of the bytes pool. */
|
||||
private static final int NN = 312;
|
||||
/** Period second parameter. */
|
||||
private static final int MM = 156;
|
||||
/** X * MATRIX_A for X = {0, 1}. */
|
||||
private static final long[] MAG01 = { 0x0, 0xb5026f5aa96619e9L };
|
||||
/** Most significant 33 bits. */
|
||||
private static final long UM = 0xffffffff80000000L;
|
||||
/** Least significant 31 bits. */
|
||||
private static final long LM = 0x7fffffffL;
|
||||
/** Bytes pool. */
|
||||
private long[] mt = new long[NN];
|
||||
/** Current index in the bytes pool. */
|
||||
private int mti;
|
||||
|
||||
/**
|
||||
* Creates a new random number generator.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public MersenneTwister64(long[] seed) {
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
final long[] s = Arrays.copyOf(mt, NN + 1);
|
||||
s[NN] = mti;
|
||||
|
||||
return NumberFactory.makeByteArray(s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != (NN + 1) * 8) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final long[] tmp = NumberFactory.makeLongArray(s);
|
||||
|
||||
System.arraycopy(tmp, 0, mt, 0, NN);
|
||||
mti = (int) tmp[NN];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reinitializes the generator as if just built with the given seed.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
private void setSeedInternal(long[] seed) {
|
||||
initState(19650218L);
|
||||
|
||||
int i = 1;
|
||||
int j = 0;
|
||||
|
||||
for (int k = Math.max(NN, seed.length); k != 0; k--) {
|
||||
final long mm1 = mt[i - 1];
|
||||
mt[i] = (mt[i] ^ ((mm1 ^ (mm1 >>> 62)) * 0x369dea0f31a53f85L)) + seed[j] + j; // non linear
|
||||
i++;
|
||||
j++;
|
||||
if (i >= NN) {
|
||||
mt[0] = mt[NN - 1];
|
||||
i = 1;
|
||||
}
|
||||
if (j >= seed.length) {
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
for (int k = NN - 1; k != 0; k--) {
|
||||
final long mm1 = mt[i - 1];
|
||||
mt[i] = (mt[i] ^ ((mm1 ^ (mm1 >>> 62)) * 0x27bb2ee687b0b0fdL)) - i; // non linear
|
||||
i++;
|
||||
if (i >= NN) {
|
||||
mt[0] = mt[NN - 1];
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
mt[0] = 0x8000000000000000L; // MSB is 1; assuring non-zero initial array
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the internal state of this instance.
|
||||
*
|
||||
* @param seed Seed.
|
||||
*/
|
||||
private void initState(long seed) {
|
||||
mt[0] = seed;
|
||||
for (mti = 1; mti < NN; mti++) {
|
||||
final long mm1 = mt[mti - 1];
|
||||
mt[mti] = 0x5851f42d4c957f2dL * (mm1 ^ (mm1 >>> 62)) + mti;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long next() {
|
||||
long x;
|
||||
|
||||
if (mti >= NN) { // generate NN words at one time
|
||||
for (int i = 0; i < NN - MM; i++) {
|
||||
x = (mt[i] & UM) | (mt[i + 1] & LM);
|
||||
mt[i] = mt[i + MM] ^ (x >>> 1) ^ MAG01[(int)(x & 0x1L)];
|
||||
}
|
||||
for (int i = NN - MM; i < NN - 1; i++) {
|
||||
x = (mt[i] & UM) | (mt[i + 1] & LM);
|
||||
mt[i] = mt[ i + (MM - NN)] ^ (x >>> 1) ^ MAG01[(int)(x & 0x1L)];
|
||||
}
|
||||
|
||||
x = (mt[NN - 1] & UM) | (mt[0] & LM);
|
||||
mt[NN - 1] = mt[MM - 1] ^ (x >>> 1) ^ MAG01[(int)(x & 0x1L)];
|
||||
|
||||
mti = 0;
|
||||
}
|
||||
|
||||
x = mt[mti++];
|
||||
|
||||
x ^= (x >>> 29) & 0x5555555555555555L;
|
||||
x ^= (x << 17) & 0x71d67fffeda60000L;
|
||||
x ^= (x << 37) & 0xfff7eee000000000L;
|
||||
x ^= x >>> 43;
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
/**
|
||||
* Source of randomness that generate values of type {@code long}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface RandomLongSource {
|
||||
/**
|
||||
* @return the next random value.
|
||||
*/
|
||||
long next();
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* A fast RNG, with 64 bits of state, that can be used to initialize the
|
||||
* state of other generators.
|
||||
*
|
||||
* @see <a href="http://xorshift.di.unimi.it/splitmix64.c">
|
||||
* Original source code</a>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class SplitMix64 extends LongProvider {
|
||||
/** State. */
|
||||
private long state;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
*/
|
||||
public SplitMix64(Long seed) {
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the RNG.
|
||||
*
|
||||
* @param seed Seed.
|
||||
*/
|
||||
private void setSeedInternal(Long seed) {
|
||||
state = seed;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long next() {
|
||||
long z = state += 0x9e3779b97f4a7c15L;
|
||||
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
|
||||
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
|
||||
return z ^ (z >>> 31);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
return NumberFactory.makeByteArray(state);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != 8) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
state = NumberFactory.makeLong(s);
|
||||
}
|
||||
}
|
|
@ -1,310 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import org.apache.commons.math4.exception.MathInternalError;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* Random number generator designed by Mark D. Overton.
|
||||
* <p>
|
||||
* It is one of the many generators described by the author in the following article series:
|
||||
* <ul>
|
||||
* <li><a href="http://www.drdobbs.com/tools/fast-high-quality-parallel-random-number/229625477">Part one</a></li>
|
||||
* <li><a href="http://www.drdobbs.com/tools/fast-high-quality-parallel-random-number/231000484">Part two</a></li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class TwoCmres extends LongProvider {
|
||||
/** A small positive integer. */
|
||||
private static final byte SEED_GUARD = 9;
|
||||
/** Factory of instances of this class. Singleton. */
|
||||
private static final Cmres.Factory FACTORY = new Cmres.Factory();
|
||||
/** First subcycle generator. */
|
||||
private final Cmres x;
|
||||
/** Second subcycle generator. */
|
||||
private final Cmres y;
|
||||
/** State of first subcycle generator. */
|
||||
private long xx;
|
||||
/** State of second subcycle generator. */
|
||||
private long yy;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
* @param x First subcycle generator.
|
||||
* @param y Second subcycle generator.
|
||||
* @throws InsufficientDataException if {@code x == y}.
|
||||
*/
|
||||
private TwoCmres(int seed,
|
||||
Cmres x,
|
||||
Cmres y) {
|
||||
if (x == y) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param seed Seed.
|
||||
*/
|
||||
public TwoCmres(Integer seed) {
|
||||
this(seed, 0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param seed Seed.
|
||||
* @param i Table entry for first subcycle generator.
|
||||
* @param j Table entry for second subcycle generator.
|
||||
* @throws InsufficientDataException if {@code i == j}.
|
||||
* @throws OutOfRangeException if {@code i < 0} or
|
||||
* {@code i >= numberOfSubcycleGenerators()}.
|
||||
* @throws OutOfRangeException if {@code j < 0} or
|
||||
* {@code j >= numberOfSubcycleGenerators()}.
|
||||
*/
|
||||
public TwoCmres(Integer seed,
|
||||
int i,
|
||||
int j) {
|
||||
this(seed, FACTORY.get(i), FACTORY.get(j));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long next() {
|
||||
xx = x.transform(xx);
|
||||
yy = y.transform(yy);
|
||||
|
||||
return xx + yy;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + " (" + x + " + " + y + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the number of subcycle generators.
|
||||
*/
|
||||
public static int numberOfSubcycleGenerators() {
|
||||
return FACTORY.numberOfSubcycleGenerators();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
return NumberFactory.makeByteArray(new long[] { xx, yy });
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != 16) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final long[] state = NumberFactory.makeLongArray(s);
|
||||
xx = state[0];
|
||||
yy = state[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param seed Seed.
|
||||
*/
|
||||
private void setSeedInternal(int seed) {
|
||||
// The seeding procedure consists in going away from some
|
||||
// point known to be in the cycle.
|
||||
// The total number of calls to the "transform" method will
|
||||
// not exceed about 130,000 (which is negligible as seeding
|
||||
// will not occur more than once in normal usage).
|
||||
|
||||
// Make two positive 16-bits integers.
|
||||
final long s = NumberFactory.makeLong(0, seed); // s >= 0
|
||||
final int xMax = (int) (s & 0xffff + SEED_GUARD);
|
||||
final int yMax = (int) ((s >> 16) + SEED_GUARD);
|
||||
|
||||
if (xMax < 0 ||
|
||||
yMax < 0) {
|
||||
throw new MathInternalError();
|
||||
}
|
||||
|
||||
xx = x.getStart();
|
||||
for (int i = xMax; i > 0; i--) {
|
||||
xx = x.transform(xx);
|
||||
}
|
||||
|
||||
yy = y.getStart();
|
||||
for (int i = yMax; i > 0; i--) {
|
||||
yy = y.transform(yy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcycle generator.
|
||||
* Class is immutable.
|
||||
*/
|
||||
static class Cmres {
|
||||
/** Cycle start. */
|
||||
private final int start;
|
||||
/** Multiplier. */
|
||||
private final long multiply;
|
||||
/** Rotation. */
|
||||
private final int rotate;
|
||||
|
||||
/**
|
||||
* @param multiply Multiplier.
|
||||
* @param rotate Positive number. Must be in {@code [0, 64]}.
|
||||
* @param start Cycle start.
|
||||
*/
|
||||
Cmres(long multiply,
|
||||
int rotate,
|
||||
int start) {
|
||||
this.multiply = multiply;
|
||||
this.rotate = rotate;
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
final String sep = ", ";
|
||||
// Use hexadecimal for "multiplier" field.
|
||||
final String m = String.format((java.util.Locale) null, "0x%016xL", multiply);
|
||||
return "Cmres: [" + m + sep + rotate + sep + start + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the multiplier.
|
||||
*/
|
||||
public long getMultiply() {
|
||||
return multiply;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the cycle start.
|
||||
*/
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param state Current state.
|
||||
* @return the new state.
|
||||
*/
|
||||
long transform(long state) {
|
||||
long s = state;
|
||||
s *= multiply;
|
||||
s = rotl(s);
|
||||
s -= state;
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param state State.
|
||||
* @return the rotated state.
|
||||
*/
|
||||
private long rotl(long state) {
|
||||
return (state << rotate) | (state >>> (64 - rotate));
|
||||
}
|
||||
|
||||
/** Factory. */
|
||||
static class Factory {
|
||||
/** List of good "Cmres" subcycle generators. */
|
||||
private static final List<Cmres> TABLE = new ArrayList<Cmres>();
|
||||
|
||||
/**
|
||||
* Populates the table.
|
||||
* It lists parameters known to be good (provided in
|
||||
* the article referred to above).
|
||||
* To maintain compatibility, new entries must be added
|
||||
* only at the end of the table.
|
||||
*/
|
||||
static {
|
||||
add(0xedce446814d3b3d9L, 33, 0x13b572e7);
|
||||
add(0xc5b3cf786c806df7L, 33, 0x13c8e18a);
|
||||
add(0xdd91bbb8ab9e0e65L, 31, 0x06dd03a6);
|
||||
add(0x7b69342c0790221dL, 31, 0x1646bb8b);
|
||||
add(0x0c72c0d18614c32bL, 33, 0x06014a3d);
|
||||
add(0xd8d98c13bebe26c9L, 33, 0x014e8475);
|
||||
add(0xcb039dc328bbc40fL, 31, 0x008684bd);
|
||||
add(0x858c5ef3c021ed2fL, 32, 0x0dc8d622);
|
||||
add(0x4c8be96bfc23b127L, 33, 0x0b6b20cc);
|
||||
add(0x11eab77f808cf641L, 32, 0x06534421);
|
||||
add(0xbc9bd78810fd28fdL, 31, 0x1d9ba40d);
|
||||
add(0x0f1505c780688cb5L, 33, 0x0b7b7b67);
|
||||
add(0xadc174babc2053afL, 31, 0x267f4197);
|
||||
add(0x900b6b82b31686d9L, 31, 0x023c6985);
|
||||
// Add new entries here.
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the number of subcycle generators.
|
||||
*/
|
||||
int numberOfSubcycleGenerators() {
|
||||
return TABLE.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param index Index into the list of available generators.
|
||||
* @return the subcycle generator entry at index {@code index}.
|
||||
*/
|
||||
Cmres get(int index) {
|
||||
if (index < 0 ||
|
||||
index >= TABLE.size()) {
|
||||
throw new OutOfRangeException(index, 0, TABLE.size());
|
||||
}
|
||||
|
||||
return TABLE.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an entry to the {@link Factory#TABLE}.
|
||||
*
|
||||
* @param multiply Multiplier.
|
||||
* @param rotate Rotate.
|
||||
* @param start Cycle start.
|
||||
*/
|
||||
private static void add(long multiply,
|
||||
int rotate,
|
||||
int start) {
|
||||
// Sanity check: if there are duplicates, the class initialization
|
||||
// will fail (and the JVM will report "NoClassDefFoundError").
|
||||
for (Cmres sg : TABLE) {
|
||||
if (multiply == sg.getMultiply()) {
|
||||
throw new MathInternalError();
|
||||
}
|
||||
}
|
||||
|
||||
TABLE.add(new Cmres(multiply, rotate, start));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.source64;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.rng.internal.util.NumberFactory;
|
||||
|
||||
/**
|
||||
* A fast RNG.
|
||||
*
|
||||
* @see <a href="http://xorshift.di.unimi.it/xorshift1024star.c">
|
||||
* Original source code</a>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class XorShift1024Star extends LongProvider {
|
||||
/** Size of the state vector. */
|
||||
private static final int SEED_SIZE = 16;
|
||||
/** State. */
|
||||
private final long[] state = new long[SEED_SIZE];
|
||||
/** Index in "state" array. */
|
||||
private int index;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param seed Initial seed.
|
||||
* If the length is larger than 16, only the first 16 elements will
|
||||
* be used; if smaller, the remaining elements will be automatically
|
||||
* set.
|
||||
*/
|
||||
public XorShift1024Star(long[] seed) {
|
||||
setSeedInternal(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected byte[] getStateInternal() {
|
||||
final long[] s = Arrays.copyOf(state, SEED_SIZE + 1);
|
||||
s[SEED_SIZE] = index;
|
||||
|
||||
return NumberFactory.makeByteArray(s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected void setStateInternal(byte[] s) {
|
||||
if (s.length != (SEED_SIZE + 1) * 8) {
|
||||
throw new InsufficientDataException();
|
||||
}
|
||||
|
||||
final long[] tmp = NumberFactory.makeLongArray(s);
|
||||
|
||||
System.arraycopy(tmp, 0, state, 0, SEED_SIZE);
|
||||
index = (int) tmp[SEED_SIZE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the RNG.
|
||||
*
|
||||
* @param seed Seed.
|
||||
*/
|
||||
private void setSeedInternal(long[] seed) {
|
||||
// Reset the whole state of this RNG (i.e. "state" and "index").
|
||||
// Seeding procedure is not part of the reference code.
|
||||
|
||||
System.arraycopy(seed, 0, state, 0, Math.min(seed.length, state.length));
|
||||
|
||||
if (seed.length < SEED_SIZE) {
|
||||
for (int i = seed.length; i < SEED_SIZE; i++) {
|
||||
state[i] = 26021969L * i;
|
||||
}
|
||||
for (int i = SEED_SIZE - 1; i > seed.length; i--) {
|
||||
state[i] ^= state[SEED_SIZE - i - 1];
|
||||
}
|
||||
|
||||
state[seed.length] = 0x8000000000000000L; // Ensuring non-zero initial array.
|
||||
}
|
||||
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long next() {
|
||||
final long s0 = state[index];
|
||||
long s1 = state[index = (index + 1) & 15];
|
||||
s1 ^= s1 << 31; // a
|
||||
state[index] = s1 ^ s0 ^ (s1 >>> 11) ^ (s0 >>> 30); // b,c
|
||||
return state[index] * 1181783497276652981L;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <h3>
|
||||
* Concrete algorithms for {@code long}-based sources of randomness
|
||||
* </h3>
|
||||
*
|
||||
* <p>
|
||||
* <b>For internal use only:</b> Direct access to classes in this package
|
||||
* is discouraged, as they could be modified without notice.
|
||||
* </p>
|
||||
*
|
||||
* <p><b>Notes for developers</b></p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>
|
||||
* A source of randomness must inherit from
|
||||
* {@link org.apache.commons.math4.rng.internal.source64.LongProvider}
|
||||
* </li>
|
||||
* <li>
|
||||
* The "provider" must specify <em>one</em> way for setting the seed.
|
||||
* For a given seed, the generated sequence must always be the same.
|
||||
* </li>
|
||||
* <li>
|
||||
* The "provider" must implement methods {@code getStateInternal} and
|
||||
* {@code setStateInternal} in order to save and restore the state of an
|
||||
* instance (cf. {@link org.apache.commons.math4.rng.internal.BaseProvider}).
|
||||
* </li>
|
||||
* <li>
|
||||
* When a new class is implemented here, user-access to it must be provided
|
||||
* through associated {@link org.apache.commons.math4.rng.RandomSource
|
||||
* factory methods}.
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.rng.internal.source64;
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Converts a {@code Integer} to an {@code Long}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Int2Long implements SeedConverter<Integer, Long> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Long convert(Integer seed) {
|
||||
final int s = seed;
|
||||
return NumberFactory.makeLong(s, ~s);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Creates a single value by "xor" of all the values in the input array.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class IntArray2Int implements SeedConverter<int[], Integer> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Integer convert(int[] seed) {
|
||||
int out = 0;
|
||||
for (int i = 0; i < seed.length; i++) {
|
||||
out ^= seed[i];
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Creates a {@code long[]} from an {@code int[]}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class IntArray2LongArray implements SeedConverter<int[], long[]> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long[] convert(int[] seed) {
|
||||
final int outSize = (seed.length + 1) / 2;
|
||||
final long[] out = new long[outSize];
|
||||
for (int i = 0; i < outSize; i++) {
|
||||
final int lo = seed[i];
|
||||
final int hi = outSize + i < seed.length ? seed[outSize + i] : 0;
|
||||
out[i] = NumberFactory.makeLong(hi, lo) ;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Converts a {@code Long} to an {@code Integer}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Long2Int implements SeedConverter<Long, Integer> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Integer convert(Long seed) {
|
||||
return NumberFactory.makeInt(seed);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
|
||||
/**
|
||||
* Uses a {@code long} value to seed a {@link SplitMix64} RNG and
|
||||
* create a {@code int[]} with the requested number of random
|
||||
* values.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Long2IntArray implements SeedConverter<Long, int[]> {
|
||||
/** Size of the output array. */
|
||||
private final int size;
|
||||
|
||||
/**
|
||||
* @param size Size of the output array.
|
||||
*/
|
||||
public Long2IntArray(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int[] convert(Long seed) {
|
||||
return SeedFactory.createIntArray(size, new SplitMix64(seed));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(size=" + size + ")";
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
|
||||
/**
|
||||
* Uses a {@code Long} value to seed a {@link SplitMix64} RNG and
|
||||
* create a {@code long[]} with the requested number of random
|
||||
* values.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class Long2LongArray implements SeedConverter<Long, long[]> {
|
||||
/** Size of the output array. */
|
||||
private final int size;
|
||||
|
||||
/**
|
||||
* @param size Size of the output array.
|
||||
*/
|
||||
public Long2LongArray(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long[] convert(Long seed) {
|
||||
final long[] out = new long[size];
|
||||
final SplitMix64 rng = new SplitMix64(seed);
|
||||
for (int i = 0; i < size; i++) {
|
||||
out[i] = rng.nextLong();
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(size: " + size + ")";
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Creates an {@code int[]} from a {@code long[]}.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LongArray2IntArray implements SeedConverter<long[], int[]> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int[] convert(long[] seed) {
|
||||
final int[] out = new int[seed.length * 2];
|
||||
for (int i = 0; i < seed.length; i++) {
|
||||
final long current = seed[i];
|
||||
out[i] = NumberFactory.extractLo(current);
|
||||
out[seed.length + i] = NumberFactory.extractHi(current);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Creates a single value by "xor" of all the values in the input array.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LongArray2Long implements SeedConverter<long[], Long> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Long convert(long[] seed) {
|
||||
long out = 0;
|
||||
for (int i = 0; i < seed.length; i++) {
|
||||
out ^= seed[i];
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
|
||||
/**
|
||||
* Uses a {@code long} value to seed a {@link SplitMix64} RNG and
|
||||
* create an {@code int[]} with the requested number of random
|
||||
* values.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LongMixInt implements SeedConverter<Long, int[]> {
|
||||
/** Size of the output array. */
|
||||
private final int size;
|
||||
|
||||
/**
|
||||
* @param size Size of the output array.
|
||||
*/
|
||||
public LongMixInt(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int[] convert(Long seed) {
|
||||
return SeedFactory.createIntArray(size, new SplitMix64(seed));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(size=" + size + ")";
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
|
||||
/**
|
||||
* Uses a {@code long} value to seed a {@link SplitMix64} RNG and
|
||||
* create a {@code long[]} with the requested number of random
|
||||
* values.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class LongMixLong implements SeedConverter<Long, long[]> {
|
||||
/** Size of the output array. */
|
||||
private final int size;
|
||||
|
||||
/**
|
||||
* @param size Size of the output array.
|
||||
*/
|
||||
public LongMixLong(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public long[] convert(Long seed) {
|
||||
final long[] out = new long[size];
|
||||
final SplitMix64 rng = new SplitMix64(seed);
|
||||
for (int i = 0; i < size; i++) {
|
||||
out[i] = rng.nextLong();
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(size: " + size + ")";
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
|
||||
/**
|
||||
* Dummy converter that simply passes on its input.
|
||||
* It can be useful to avoid "unchecked" compiler warnings.
|
||||
*
|
||||
* @param <SEED> Seed type.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class NoOpConverter<SEED> implements SeedConverter<SEED, SEED> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public SEED convert(SEED seed) {
|
||||
return seed;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pass-through";
|
||||
}
|
||||
}
|
|
@ -1,327 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
/** See {@link #makeDouble(long)} */
|
||||
private static final long DOUBLE_HIGH_BITS = 0x3ffL << 52;
|
||||
/** See {@link #makeFloat(int)} */
|
||||
private static final float FLOAT_MULTIPLIER = 0x1.0p-23f;
|
||||
/** See {@link #makeDouble(int, int)} */
|
||||
private static final double DOUBLE_MULTIPLIER = 0x1.0p-52d;
|
||||
/** 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() {}
|
||||
|
||||
/**
|
||||
* @param v Number.
|
||||
* @return a boolean.
|
||||
*/
|
||||
public static boolean makeBoolean(int v) {
|
||||
return (v >>> 31) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param v Number.
|
||||
* @return a boolean.
|
||||
*/
|
||||
public static boolean makeBoolean(long v) {
|
||||
return (v >>> 63) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param v Number.
|
||||
* @return a {@code double} value in the interval {@code [0, 1]}.
|
||||
*/
|
||||
public static double makeDouble(long v) {
|
||||
// http://xorshift.di.unimi.it
|
||||
return Double.longBitsToDouble(DOUBLE_HIGH_BITS | v >>> 12) - 1d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
final long high = ((long) (v >>> 6)) << 26;
|
||||
final int low = w >>> 6;
|
||||
return (high | low) * DOUBLE_MULTIPLIER;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param v Number.
|
||||
* @return a {@code float} value in the interval {@code [0, 1]}.
|
||||
*/
|
||||
public static float makeFloat(int v) {
|
||||
return (v >>> 9) * FLOAT_MULTIPLIER;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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}.
|
||||
*/
|
||||
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
|
||||
* <pre><code>
|
||||
* vL = extractLo(v);
|
||||
* vH = extractHi(v);
|
||||
* </code></pre>
|
||||
* 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
|
||||
* <pre><code>
|
||||
* vL = extractLo(v);
|
||||
* vH = extractHi(v);
|
||||
* </code></pre>
|
||||
* 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];
|
||||
|
||||
for (int i = 0; i < LONG_SIZE; i++) {
|
||||
final int shift = i * 8;
|
||||
b[i] = (byte) ((v >>> shift) & LONG_LOWEST_BYTE_MASK);
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code long} from 8 bytes.
|
||||
*
|
||||
* @param input Input.
|
||||
* @return the value that correspond to the given bytes assuming
|
||||
* that the is ordered in increasing byte significance (i.e. the
|
||||
* first byte in the array is the least-siginficant).
|
||||
* @throws DimensionMismatchException if {@code input.length != 8}.
|
||||
*/
|
||||
public static long makeLong(byte[] input) {
|
||||
if (input.length != LONG_SIZE) {
|
||||
throw new DimensionMismatchException(input.length, LONG_SIZE);
|
||||
}
|
||||
|
||||
long v = 0;
|
||||
for (int i = 0; i < LONG_SIZE; i++) {
|
||||
final int shift = i * 8;
|
||||
v |= (((long) input[i]) & LONG_LOWEST_BYTE_MASK) << shift;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++) {
|
||||
final byte[] current = makeByteArray(input[i]);
|
||||
System.arraycopy(current, 0, b, i * LONG_SIZE, 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 DimensionMismatchException 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;
|
||||
if (num * LONG_SIZE != size) {
|
||||
throw new DimensionMismatchException(size, num * LONG_SIZE);
|
||||
}
|
||||
|
||||
final long[] output = new long[num];
|
||||
for (int i = 0; i < num; i++) {
|
||||
final int from = i * LONG_SIZE;
|
||||
final byte[] current = Arrays.copyOfRange(input, from, from + LONG_SIZE);
|
||||
output[i] = makeLong(current);
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
for (int i = 0; i < INT_SIZE; i++) {
|
||||
final int shift = i * 8;
|
||||
b[i] = (byte) ((v >>> shift) & INT_LOWEST_BYTE_MASK);
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an {@code int} from 4 bytes.
|
||||
*
|
||||
* @param input Input.
|
||||
* @return the value that correspond to the given bytes assuming
|
||||
* that the is ordered in increasing byte significance (i.e. the
|
||||
* first byte in the array is the least-siginficant).
|
||||
* @throws DimensionMismatchException if {@code input.length != 4}.
|
||||
*/
|
||||
public static int makeInt(byte[] input) {
|
||||
if (input.length != INT_SIZE) {
|
||||
throw new DimensionMismatchException(input.length, INT_SIZE);
|
||||
}
|
||||
|
||||
int v = 0;
|
||||
for (int i = 0; i < INT_SIZE; i++) {
|
||||
final int shift = i * 8;
|
||||
v |= (((int) input[i]) & INT_LOWEST_BYTE_MASK) << shift;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++) {
|
||||
final byte[] current = makeByteArray(input[i]);
|
||||
System.arraycopy(current, 0, b, i * INT_SIZE, 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 DimensionMismatchException 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;
|
||||
if (num * INT_SIZE != size) {
|
||||
throw new DimensionMismatchException(size, num * INT_SIZE);
|
||||
}
|
||||
|
||||
final int[] output = new int[num];
|
||||
for (int i = 0; i < num; i++) {
|
||||
final int from = i * INT_SIZE;
|
||||
final byte[] current = Arrays.copyOfRange(input, from, from + INT_SIZE);
|
||||
output[i] = makeInt(current);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Seed converter.
|
||||
*
|
||||
* @param <IN> Input seed type.
|
||||
* @param <OUT> Output seed type.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface SeedConverter<IN, OUT> {
|
||||
/**
|
||||
* Converts seed from input type to output type.
|
||||
*
|
||||
* @param seed Original seed value.
|
||||
* @return the converted seed value.
|
||||
*/
|
||||
OUT convert(IN seed);
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
/**
|
||||
* Composes two {@link SeedConverter converters}.
|
||||
*
|
||||
* @param <IN> Input seed type.
|
||||
* @param <TRANS> Transitional seed type.
|
||||
* @param <OUT> Output seed type.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class SeedConverterComposer<IN, TRANS, OUT> implements SeedConverter<IN, OUT> {
|
||||
/** First conversion. */
|
||||
private SeedConverter<IN, TRANS> first;
|
||||
/** Second conversion. */
|
||||
private SeedConverter<TRANS, OUT> second;
|
||||
|
||||
/**
|
||||
* @param first First conversion.
|
||||
* @param second second conversion.
|
||||
*/
|
||||
public SeedConverterComposer(SeedConverter<IN, TRANS> first,
|
||||
SeedConverter<TRANS, OUT> second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public OUT convert(IN seed) {
|
||||
final TRANS trans = first.convert(seed);
|
||||
return second.convert(trans);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " (" + second + " o " + first + ")";
|
||||
}
|
||||
}
|
|
@ -1,262 +0,0 @@
|
|||
/*
|
||||
* 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.math4.rng.internal.util;
|
||||
|
||||
import org.apache.commons.math4.rng.internal.source32.RandomIntSource;
|
||||
import org.apache.commons.math4.rng.internal.source32.Well44497b;
|
||||
import org.apache.commons.math4.rng.internal.source64.RandomLongSource;
|
||||
import org.apache.commons.math4.rng.internal.source64.SplitMix64;
|
||||
|
||||
/**
|
||||
* Utilities related to seeding.
|
||||
*
|
||||
* <p>
|
||||
* This class provides methods to generate random seeds (single values
|
||||
* or arrays of values, of {@code int} or {@code long} types) that can
|
||||
* be passed to the {@link org.apache.commons.math4.rng.RandomSource
|
||||
* methods that create a generator instance}.
|
||||
* <br>
|
||||
* Although the seed-generating methods defined in this class will likely
|
||||
* return different values for all calls, there is no guarantee that the
|
||||
* produced seed will result always in a "good" sequence of numbers (even
|
||||
* if the generator initialized with that seed is good).
|
||||
* <br>
|
||||
* There is <i>no guarantee</i> that sequences will not overlap.
|
||||
* </p>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class SeedFactory {
|
||||
/** Generator with a long period. */
|
||||
private static final RandomIntSource SEED_GENERATOR;
|
||||
|
||||
static {
|
||||
// Another RNG for initializing the "SEED_GENERATOR".
|
||||
final long t = System.currentTimeMillis();
|
||||
final int h = System.identityHashCode(Runtime.getRuntime());
|
||||
final SplitMix64 rng = new SplitMix64(t ^ NumberFactory.makeLong(h, ~h));
|
||||
|
||||
final int blockCount = 1391; // Size of the state array of "Well44497b".
|
||||
SEED_GENERATOR = new Well44497b(createIntArray(blockCount, rng));
|
||||
}
|
||||
|
||||
/**
|
||||
* Class contains only static methods.
|
||||
*/
|
||||
private SeedFactory() {}
|
||||
|
||||
/**
|
||||
* Creates a number for use as a seed.
|
||||
*
|
||||
* @return a random number.
|
||||
*/
|
||||
public static int createInt() {
|
||||
return createInt(SEED_GENERATOR, System.identityHashCode(new Object()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number for use as a seed.
|
||||
*
|
||||
* @return a random number.
|
||||
*/
|
||||
public static long createLong() {
|
||||
return createLong(SEED_GENERATOR, System.identityHashCode(new Object()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
public static int[] createIntArray(int n) {
|
||||
return createIntArray(n, SEED_GENERATOR, new Object());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
public static long[] createLongArray(int n) {
|
||||
return createLongArray(n, SEED_GENERATOR, new Object());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @return an array of {@code n} random numbers drawn from the
|
||||
* {@code source}.
|
||||
*/
|
||||
static long[] createLongArray(int n,
|
||||
RandomIntSource source) {
|
||||
return createLongArray(n, source, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @return an array of {@code n} random numbers drawn from the
|
||||
* {@code source}.
|
||||
*/
|
||||
static int[] createIntArray(int n,
|
||||
RandomLongSource source) {
|
||||
return createIntArray(n, source, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @return an array of {@code n} random numbers drawn from the
|
||||
* {@code source}.
|
||||
*/
|
||||
static int[] createIntArray(int n,
|
||||
RandomIntSource source) {
|
||||
return createIntArray(n, source, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @param h Arbitrary object whose {@link System#identityHashCode(Object)
|
||||
* hash code} will be combined with the next number drawn from
|
||||
* the {@code source}.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
private static long[] createLongArray(int n,
|
||||
RandomIntSource source,
|
||||
Object h) {
|
||||
final long[] array = new long[n];
|
||||
|
||||
final int hash = System.identityHashCode(h);
|
||||
for (int i = 0; i < n; i++) {
|
||||
array[i] = createLong(source, hash);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @param h Arbitrary object whose {@link System#identityHashCode(Object)
|
||||
* hash code} will be combined with the next number drawn from
|
||||
* the {@code source}.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
private static int[] createIntArray(int n,
|
||||
RandomLongSource source,
|
||||
Object h) {
|
||||
final int[] array = new int[n];
|
||||
|
||||
final int hash = System.identityHashCode(h);
|
||||
for (int i = 0; i < n; i += 2) {
|
||||
final long v = createLong(source, hash);
|
||||
|
||||
array[i] = NumberFactory.extractHi(v);
|
||||
|
||||
if (i + 1 < n) {
|
||||
array[i + 1] = NumberFactory.extractLo(v);
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of numbers for use as a seed.
|
||||
*
|
||||
* @param n Size of the array to create.
|
||||
* @param source Source of randomness.
|
||||
* @param h Arbitrary object whose {@link System#identityHashCode(Object)
|
||||
* hash code} will be combined with the next number drawn from
|
||||
* the {@code source}.
|
||||
* @return an array of {@code n} random numbers.
|
||||
*/
|
||||
private static int[] createIntArray(int n,
|
||||
RandomIntSource source,
|
||||
Object h) {
|
||||
final int[] array = new int[n];
|
||||
|
||||
final int hash = System.identityHashCode(h);
|
||||
for (int i = 0; i < n; i++) {
|
||||
array[i] = createInt(source, hash);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a random number by performing an "xor" between the
|
||||
* next value in the sequence of the {@code source} and the
|
||||
* given {@code number}.
|
||||
*
|
||||
* @param source Source of randomness.
|
||||
* @param number Arbitrary number.
|
||||
* @return a random number.
|
||||
*/
|
||||
private static long createLong(RandomLongSource source,
|
||||
int number) {
|
||||
synchronized (source) {
|
||||
return source.next() ^ NumberFactory.makeLong(number, number);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a random number by performing an "xor" between the
|
||||
* the next value in the sequence of the {@code source} and the
|
||||
* given {@code number}.
|
||||
*
|
||||
* @param source Source of randomness.
|
||||
* @param number Arbitrary number.
|
||||
* @return a random number.
|
||||
*/
|
||||
private static long createLong(RandomIntSource source,
|
||||
int number) {
|
||||
synchronized (source) {
|
||||
return NumberFactory.makeLong(source.next() ^ number,
|
||||
source.next() ^ number);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a random number by performing an "xor" between the
|
||||
* next value in the sequence of the {@code source} and the
|
||||
* given {@code number}.
|
||||
*
|
||||
* @param source Source of randomness.
|
||||
* @param number Arbitrary number.
|
||||
* @return a random number.
|
||||
*/
|
||||
private static int createInt(RandomIntSource source,
|
||||
int number) {
|
||||
synchronized (source) {
|
||||
return source.next() ^ number;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utilities for seed conversion.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.rng.internal.util;
|
|
@ -1,95 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <h3>Randomness Providers</h3>
|
||||
*
|
||||
* <p>
|
||||
* This package contains the public API for generating sequences of
|
||||
* pseudo-random numbers that are <i>uniformly distributed</i> in a
|
||||
* specified range.
|
||||
* <br>
|
||||
* All implemented generators can be instantiated through
|
||||
* {@link org.apache.commons.math4.rng.RandomSource factory methods}.
|
||||
* The low-level classes, that define how the randomness is produced,
|
||||
* are implemented in package {@link org.apache.commons.math4.rng.internal}
|
||||
* and its sub-packages, but should not be used directly.
|
||||
* <br>
|
||||
* The generators are <i>not</i> thread-safe: Parallel applications must
|
||||
* use different generator instances in different threads.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* In the case of pseudo-random generators, the source of randomness is
|
||||
* usually a set of numbers whose bits representation are scrambled in such
|
||||
* a way as to produce a random-looking sequence.
|
||||
* <br>
|
||||
* The main property of the sequence is that the numbers must be uniformly
|
||||
* distributed within their allowed range.
|
||||
* <br>
|
||||
* Classes in this package do not provide any further processing of the
|
||||
* number generation such as to match other types of distribution.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Which source of randomness to choose may depend on which properties
|
||||
* are more important.
|
||||
* Considerations can include speed of generation, memory usage, period
|
||||
* size, equidistribution, correlation, etc.
|
||||
* <br>
|
||||
* For some of the generators, interesting properties (of the reference
|
||||
* implementations) are proven in scientific papers.
|
||||
* Some generators can also suffer from potential weaknesses.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* For simple sampling, any of the generators implemented in this library
|
||||
* may be sufficient.
|
||||
* <br>
|
||||
* For Monte-Carlo simulations that require generating high-dimensional
|
||||
* vectors), equidistribution and non-correlation are crucial.
|
||||
* The <i>Mersenne Twister</i> and <i>Well</i> generators have
|
||||
* equidistribution properties proven according to their bits pool size
|
||||
* which is directly related to their period (all of them have maximal
|
||||
* period, i.e. a generator with size {@code n} pool has a period
|
||||
* <code>2<sup>n</sup>-1</code>).
|
||||
* They also have equidistribution properties for 32 bits blocks up to
|
||||
* {@code s/32} dimension where {@code s} is their pool size.
|
||||
* <br>
|
||||
* For example, {@code Well19937c} is equidistributed up to dimension 623
|
||||
* (i.e. 19937 divided by 32).
|
||||
* It means that a Monte-Carlo simulation generating vectors of {@code n}
|
||||
* (32-bits integer) variables at each iteration has some guarantee on the
|
||||
* properties of its components as long as {@code n < 623}.
|
||||
* Note that if the variables are of type {@code double}, the limit is
|
||||
* divided by two (since 64 bits are needed to create a {@code double}).
|
||||
* <br>
|
||||
* Reference to the relevant publications are listed in the specific
|
||||
* documentation of each class.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Memory usage can vary a lot between providers.
|
||||
* The state of {@code MersenneTwister} is composed of 624 integers,
|
||||
* using about 2.5 kB.
|
||||
* The <i>Well</i> generators use 6 integer arrays, the length of each
|
||||
* being equal to the pool size; thus, for example, {@code Well44497b}
|
||||
* uses about 33 kB.
|
||||
* </p>
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.rng;
|
|
@ -40,8 +40,8 @@ import org.apache.commons.math4.linear.Array2DRowFieldMatrix;
|
|||
import org.apache.commons.math4.linear.FieldMatrix;
|
||||
import org.apache.commons.math4.linear.MatrixUtils;
|
||||
import org.apache.commons.math4.linear.RealMatrix;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.CombinatoricsUtils;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
|
|
|
@ -24,8 +24,8 @@ import java.util.List;
|
|||
|
||||
import org.apache.commons.math4.exception.MathInternalError;
|
||||
import org.apache.commons.math4.exception.NotANumberException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.random.RandomUtils;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
||||
|
|
|
@ -39,8 +39,8 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
|
|||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
|
||||
/**
|
||||
* Arrays utilities.
|
||||
|
|
|
@ -21,8 +21,8 @@ import java.io.IOException;
|
|||
import java.io.ObjectOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
|
||||
/**
|
||||
* A strategy of selecting random index between begin and end indices.
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
package org.apache.commons.math4;
|
||||
|
||||
import org.apache.commons.math4.RealFieldElement;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
import org.junit.Assert;
|
||||
|
|
|
@ -22,8 +22,8 @@ import java.util.regex.MatchResult;
|
|||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.apache.commons.math4.util.MathArrays;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.exception.MathIllegalStateException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
|
|
|
@ -26,8 +26,8 @@ import org.apache.commons.math4.analysis.differentiation.DerivativeStructure;
|
|||
import org.apache.commons.math4.analysis.polynomials.PolynomialFunction;
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooLargeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.ArithmeticUtils;
|
||||
import org.apache.commons.math4.util.CombinatoricsUtils;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
|
|
|
@ -22,8 +22,8 @@ import java.util.List;
|
|||
import org.apache.commons.math4.ExtendedFieldElementAbstractTest;
|
||||
import org.apache.commons.math4.TestUtils;
|
||||
import org.apache.commons.math4.analysis.polynomials.PolynomialFunction;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
|
|
@ -26,8 +26,8 @@ import org.apache.commons.math4.analysis.function.Sigmoid;
|
|||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
|
|
@ -26,8 +26,8 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
|
|||
import org.apache.commons.math4.exception.NonMonotonicSequenceException;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.NumberIsTooSmallException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.Precision;
|
||||
import org.junit.Assert;
|
||||
|
|
|
@ -22,8 +22,8 @@ import org.apache.commons.math4.distribution.UniformRealDistribution;
|
|||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.exception.OutOfRangeException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.Precision;
|
||||
import org.junit.Assert;
|
||||
|
|
|
@ -21,8 +21,8 @@ import org.apache.commons.math4.distribution.RealDistribution;
|
|||
import org.apache.commons.math4.distribution.UniformRealDistribution;
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
|
|||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.exception.NonMonotonicSequenceException;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.Precision;
|
||||
import org.junit.Assert;
|
||||
|
|
|
@ -23,8 +23,8 @@ import org.apache.commons.math4.exception.DimensionMismatchException;
|
|||
import org.apache.commons.math4.exception.InsufficientDataException;
|
||||
import org.apache.commons.math4.exception.NonMonotonicSequenceException;
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ import org.apache.commons.math4.distribution.RealDistribution;
|
|||
import org.apache.commons.math4.distribution.UniformRealDistribution;
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.apache.commons.math4.util.Precision;
|
||||
import org.junit.Assert;
|
||||
|
|
|
@ -18,8 +18,8 @@ package org.apache.commons.math4.distribution;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math4.rng.RandomSource;
|
||||
import org.apache.commons.math4.rng.UniformRandomProvider;
|
||||
import org.apache.commons.rng.RandomSource;
|
||||
import org.apache.commons.rng.UniformRandomProvider;
|
||||
import org.apache.commons.math4.stat.StatUtils;
|
||||
import org.apache.commons.math4.stat.inference.KolmogorovSmirnovTest;
|
||||
import org.apache.commons.math4.stat.inference.InferenceTestUtils;
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue