Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 047D210F15 for ; Thu, 1 Aug 2013 13:58:23 +0000 (UTC) Received: (qmail 78569 invoked by uid 500); 1 Aug 2013 13:58:22 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 78514 invoked by uid 500); 1 Aug 2013 13:58:22 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 78507 invoked by uid 99); 1 Aug 2013 13:58:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Aug 2013 13:58:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Aug 2013 13:58:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 022962388831; Thu, 1 Aug 2013 13:57:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1509237 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java Date: Thu, 01 Aug 2013 13:57:57 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130801135758.022962388831@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erans Date: Thu Aug 1 13:57:57 2013 New Revision: 1509237 URL: http://svn.apache.org/r1509237 Log: MATH-1012 Removed duplicate code. Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java?rev=1509237&r1=1509236&r2=1509237&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java Thu Aug 1 13:57:57 2013 @@ -118,7 +118,7 @@ public class RandomDataGenerator impleme private RandomGenerator rand = null; /** underlying secure random number generator */ - private SecureRandom secRand = null; + private RandomGenerator secRand = null; /** * Construct a RandomDataGenerator, using a default random generator as the source @@ -278,7 +278,7 @@ public class RandomDataGenerator impleme } // Get SecureRandom and setup Digest provider - SecureRandom secRan = getSecRan(); + final RandomGenerator secRan = getSecRan(); MessageDigest alg = null; try { alg = MessageDigest.getInstance("SHA-1"); @@ -323,25 +323,7 @@ public class RandomDataGenerator impleme /** {@inheritDoc} */ public int nextSecureInt(final int lower, final int upper) throws NumberIsTooLargeException { - if (lower >= upper) { - throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, - lower, upper, false); - } - final int max = (upper - lower) + 1; - if (max <= 0) { - // the range is too wide to fit in a positive int (larger than 2^31); as it covers - // more than half the integer range, we use directly a simple rejection method - final SecureRandom rng = getSecRan(); - while (true) { - final int r = rng.nextInt(); - if (r >= lower && r <= upper) { - return r; - } - } - } else { - // we can shift the range and generate directly a positive int - return lower + getSecRan().nextInt(max); - } + return new UniformIntegerDistribution(getSecRan(), lower, upper).sample(); } /** {@inheritDoc} */ @@ -350,11 +332,11 @@ public class RandomDataGenerator impleme throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } + final RandomGenerator rng = getSecRan(); final long max = (upper - lower) + 1; if (max <= 0) { // the range is too wide to fit in a positive long (larger than 2^63); as it covers // more than half the long range, we use directly a simple rejection method - final SecureRandom rng = getSecRan(); while (true) { final long r = rng.nextLong(); if (r >= lower && r <= upper) { @@ -363,45 +345,14 @@ public class RandomDataGenerator impleme } } else if (max < Integer.MAX_VALUE){ // we can shift the range and generate directly a positive int - return lower + getSecRan().nextInt((int) max); + return lower + rng.nextInt((int) max); } else { // we can shift the range and generate directly a positive long - return lower + nextLong(getSecRan(), max); + return lower + nextLong(rng, max); } } /** - * Returns a pseudorandom, uniformly distributed long value - * between 0 (inclusive) and the specified value (exclusive), drawn from - * this random number generator's sequence. - * - * @param rng random generator to use - * @param n the bound on the random number to be returned. Must be - * positive. - * @return a pseudorandom, uniformly distributed long - * value between 0 (inclusive) and n (exclusive). - * @throws IllegalArgumentException if n is not positive. - */ - private static long nextLong(final SecureRandom rng, final long n) throws IllegalArgumentException { - if (n > 0) { - final byte[] byteArray = new byte[8]; - long bits; - long val; - do { - rng.nextBytes(byteArray); - bits = 0; - for (final byte b : byteArray) { - bits = (bits << 8) | (((long) b) & 0xffL); - } - bits = bits & 0x7fffffffffffffffL; - val = bits % n; - } while (bits - val + (n - 1) < 0); - return val; - } - throw new NotStrictlyPositiveException(n); - } - - /** * {@inheritDoc} *

* Algorithm Description: @@ -793,7 +744,7 @@ public class RandomDataGenerator impleme */ public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { - secRand = SecureRandom.getInstance(algorithm, provider); + secRand = RandomGeneratorFactory.createRandomGenerator(SecureRandom.getInstance(algorithm, provider)); } /** @@ -829,11 +780,12 @@ public class RandomDataGenerator impleme * {@code System.currentTimeMillis() + System.identityHashCode(this)} as the default seed. *

* - * @return the SecureRandom used to generate secure random data + * @return the SecureRandom used to generate secure random data, wrapped in a + * {@link RandomGenerator}. */ - private SecureRandom getSecRan() { + private RandomGenerator getSecRan() { if (secRand == null) { - secRand = new SecureRandom(); + secRand = RandomGeneratorFactory.createRandomGenerator(new SecureRandom()); secRand.setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } return secRand;