Repository: commons-math Updated Branches: refs/heads/master 564345179 -> c7f7da754
MATH-1301 Using composition rather than inheritance. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/c7f7da75 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/c7f7da75 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/c7f7da75 Branch: refs/heads/master Commit: c7f7da754a911302dcaa6935be6fded84e1ac348 Parents: 5643451 Author: Gilles <er...@apache.org> Authored: Sun Dec 27 23:17:29 2015 +0100 Committer: Gilles <er...@apache.org> Committed: Sun Dec 27 23:17:29 2015 +0100 ---------------------------------------------------------------------- .../math4/random/JDKRandomGenerator.java | 76 ++++++++++++++++---- 1 file changed, 63 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/c7f7da75/src/main/java/org/apache/commons/math4/random/JDKRandomGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/JDKRandomGenerator.java b/src/main/java/org/apache/commons/math4/random/JDKRandomGenerator.java index 18f7466..ddd399c 100644 --- a/src/main/java/org/apache/commons/math4/random/JDKRandomGenerator.java +++ b/src/main/java/org/apache/commons/math4/random/JDKRandomGenerator.java @@ -20,50 +20,100 @@ import java.util.Random; import org.apache.commons.math4.exception.NotStrictlyPositiveException; /** - * Extension of <code>java.util.Random</code> to implement - * {@link RandomGenerator}. + * A {@link RandomGenerator} adapter that delegates the random number + * generation to the standard {@link java.util.Random} class. * * @since 1.1 */ -public class JDKRandomGenerator extends Random implements RandomGenerator { - +public class JDKRandomGenerator + implements RandomGenerator { /** Serializable version identifier. */ - private static final long serialVersionUID = -7745277476784028798L; + private static final long serialVersionUID = 20151227L; + /** JDK's RNG. */ + private final Random delegate; /** - * Create a new JDKRandomGenerator with a default seed. + * Creates an instance with an arbitrary seed. */ public JDKRandomGenerator() { - super(); + delegate = new Random(); } /** - * Create a new JDKRandomGenerator with the given seed. + * Creates an instance with the given seed. * - * @param seed initial seed + * @param seed Initial seed. * @since 3.6 */ public JDKRandomGenerator(long seed) { - setSeed(seed); + delegate = new Random(seed); } /** {@inheritDoc} */ @Override public void setSeed(int seed) { - setSeed((long) seed); + delegate.setSeed((long) seed); + } + + /** {@inheritDoc} */ + @Override + public void setSeed(long seed) { + delegate.setSeed( seed); } /** {@inheritDoc} */ @Override public void setSeed(int[] seed) { - setSeed(RandomGeneratorFactory.convertToLong(seed)); + delegate.setSeed(RandomGeneratorFactory.convertToLong(seed)); + } + + /** {@inheritDoc} */ + @Override + public void nextBytes(byte[] bytes) { + delegate.nextBytes(bytes); + } + + /** {@inheritDoc} */ + @Override + public int nextInt() { + return delegate.nextInt(); + } + + /** {@inheritDoc} */ + @Override + public long nextLong() { + return delegate.nextLong(); + } + + /** {@inheritDoc} */ + @Override + public boolean nextBoolean() { + return delegate.nextBoolean(); + } + + /** {@inheritDoc} */ + @Override + public float nextFloat() { + return delegate.nextFloat(); + } + + /** {@inheritDoc} */ + @Override + public double nextDouble() { + return delegate.nextDouble(); + } + + /** {@inheritDoc} */ + @Override + public double nextGaussian() { + return delegate.nextGaussian(); } /** {@inheritDoc} */ @Override public int nextInt(int n) { try { - return super.nextInt(n); + return delegate.nextInt(n); } catch (IllegalArgumentException e) { throw new NotStrictlyPositiveException(n); }