From 55f64675b42ac8d3c557cc850f78664bee006f6f Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Sat, 28 Jan 2023 19:32:44 -0800 Subject: [ci skip] spotlessApply with the new settings --- .../api/objects/random/CSPRNG_DO_NOT_USE.java | 125 +++++++----------- .../api/objects/random/UUIDGenerator.java | 120 ++++++++--------- .../java/gtPlusPlus/api/objects/random/XSTR.java | 142 +++++++++------------ 3 files changed, 165 insertions(+), 222 deletions(-) (limited to 'src/main/java/gtPlusPlus/api/objects/random') diff --git a/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java b/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java index 86ea4b4ad6..07c8c3609b 100644 --- a/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java +++ b/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java @@ -1,84 +1,62 @@ /* - * Copyright 2005, Nick Galbreath -- nickg [at] modp [dot] com - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 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. - * - * Neither the name of the modp.com nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * 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. - * - * This is the standard "new" BSD license: + * Copyright 2005, Nick Galbreath -- nickg [at] modp [dot] com All rights reserved. Redistribution and use in source and + * binary forms, with or without modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. 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. Neither the name of + * the modp.com nor the names of its contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. 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. This is the standard "new" BSD license: * http://www.opensource.org/licenses/bsd-license.php */ package gtPlusPlus.api.objects.random; -import gtPlusPlus.api.interfaces.IRandomGenerator; -import gtPlusPlus.core.util.Utils; import java.math.BigInteger; import java.security.SecureRandom; import java.util.Random; +import gtPlusPlus.api.interfaces.IRandomGenerator; +import gtPlusPlus.core.util.Utils; + /** * The Blum-Blum-Shub random number generator. * *

- * The Blum-Blum-Shub is a "cryptographically secure" random number - * generator. It has been proven that predicting the ouput - * is equivalent to factoring n, a large integer generated - * from two prime numbers. + * The Blum-Blum-Shub is a "cryptographically secure" random number generator. It has been proven that predicting the + * ouput is equivalent to factoring n, a large integer generated from two prime numbers. *

* *

* The Algorithm: *

*
    - *
  1. - * (setup) generate two secret prime numbers p, q such that - * pq, p ≡ 3 mod 4, q ≡ 3 mod 4. - *
  2. - *
  3. (setup) compute n = pq. n can be re-used, but - * p, and q are secret and should be disposed of.
  4. - *
  5. Generate a (secure) random seed s in the range [1, n -1] - * such that gcd(s, n) = 1. - *
  6. Compute x = s2 mod n
  7. - *
  8. Compute a single random bit with: - *
      - *
    1. x = x2 mod n
    2. - *
    3. return Least-Significant-Bit(x) (i.e. x & 1)
    4. - *
    - * Repeat as necessary. - *
  9. + *
  10. (setup) generate two secret prime numbers p, q such that pq, p ≡ 3 + * mod 4, q ≡ 3 mod 4.
  11. + *
  12. (setup) compute n = pq. n can be re-used, but p, and q are secret and should + * be disposed of.
  13. + *
  14. Generate a (secure) random seed s in the range [1, n -1] such that gcd(s, n) = 1. + *
  15. Compute x = s2 mod n
  16. + *
  17. Compute a single random bit with: + *
      + *
    1. x = x2 mod n
    2. + *
    3. return Least-Significant-Bit(x) (i.e. x & 1)
    4. + *
    + * Repeat as necessary.
  18. *
* *

- * The code originally appeared in Cryptography for - * Internet and Database Applications , Chapter 4, pages 174-177 + * The code originally appeared in Cryptography for Internet and Database + * Applications , Chapter 4, pages 174-177 *

*

- * More details are in the Handbook of Applied Cryptography, + * More details are in the Handbook of Applied Cryptography, * Section 5.5.2 *

* @@ -105,8 +83,7 @@ public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { /** * Generate appropriate prime number for use in Blum-Blum-Shub. * - * This generates the appropriate primes (p = 3 mod 4) needed to compute the - * "n-value" for Blum-Blum-Shub. + * This generates the appropriate primes (p = 3 mod 4) needed to compute the "n-value" for Blum-Blum-Shub. * * @param bits Number of bits in prime * @param rand A source of randomness @@ -121,13 +98,11 @@ public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { } /** - * This generates the "n value" -- the multiplication of two equally sized - * random prime numbers -- for use in the Blum-Blum-Shub algorithm. + * This generates the "n value" -- the multiplication of two equally sized random prime numbers -- for use in the + * Blum-Blum-Shub algorithm. * - * @param bits - * The number of bits of security - * @param rand - * A random instance to aid in generating primes + * @param bits The number of bits of security + * @param rand A random instance to aid in generating primes * @return A BigInteger, the n. */ public static BigInteger generateN(int bits, Random rand) { @@ -161,25 +136,20 @@ public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { } /** - * A constructor to specify the "n-value" to the Blum-Blum-Shub algorithm. - * The inital seed is computed using Java's internal "true" random number - * generator. + * A constructor to specify the "n-value" to the Blum-Blum-Shub algorithm. The inital seed is computed using Java's + * internal "true" random number generator. * - * @param n - * The n-value. + * @param n The n-value. */ public CSPRNG_DO_NOT_USE(BigInteger n) { this(n, SecureRandom.getSeed(n.bitLength() / 8)); } /** - * A constructor to specify both the n-value and the seed to the - * Blum-Blum-Shub algorithm. + * A constructor to specify both the n-value and the seed to the Blum-Blum-Shub algorithm. * - * @param n - * The n-value using a BigInteger - * @param seed - * The seed value using a byte[] array. + * @param n The n-value using a BigInteger + * @param seed The seed value using a byte[] array. */ public CSPRNG_DO_NOT_USE(BigInteger n, byte[] seed) { this.n = n; @@ -189,8 +159,7 @@ public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { /** * Sets or resets the seed value and internal state * - * @param seedBytes - * The new seed. + * @param seedBytes The new seed. */ public void setSeed(byte[] seedBytes) { // ADD: use hardwired default for n @@ -206,7 +175,7 @@ public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { @Override public int next(int numBits) { // TODO: find out how many LSB one can extract per cycle. - // it is more than one. + // it is more than one. int result = 0; for (int i = numBits; i != 0; --i) { state = state.modPow(two, n); diff --git a/src/main/java/gtPlusPlus/api/objects/random/UUIDGenerator.java b/src/main/java/gtPlusPlus/api/objects/random/UUIDGenerator.java index 8b70955e63..bfed0ce346 100644 --- a/src/main/java/gtPlusPlus/api/objects/random/UUIDGenerator.java +++ b/src/main/java/gtPlusPlus/api/objects/random/UUIDGenerator.java @@ -7,9 +7,8 @@ import java.util.UUID; /** * - * Implement modified version of Apache's OpenJPA UUID generator. - * This UUID generator is paired with a Blum-Blum-Shub random number generator - * which in itself is seeded by custom SecureRandom. + * Implement modified version of Apache's OpenJPA UUID generator. This UUID generator is paired with a Blum-Blum-Shub + * random number generator which in itself is seeded by custom SecureRandom. * * The UUID generator class has been converted from a static factory to an instanced factory. * @@ -18,36 +17,24 @@ import java.util.UUID; // ========================================= APACHE BLOCK ========================================= /* - * 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. + * 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. */ /** - * UUID value generator. Type 1 generator is based on the time-based generator - * in the Apache Commons Id project: http://jakarta.apache.org/commons/sandbox - * /id/uuid.html The type 4 generator uses the standard Java UUID generator. + * UUID value generator. Type 1 generator is based on the time-based generator in the Apache Commons Id project: + * http://jakarta.apache.org/commons/sandbox /id/uuid.html The type 4 generator uses the standard Java UUID generator. * - * The type 1 code has been vastly simplified and modified to replace the - * ethernet address of the host machine with the IP, since we do not want to - * require native libs and Java cannot access the MAC address directly. + * The type 1 code has been vastly simplified and modified to replace the ethernet address of the host machine with the + * IP, since we do not want to require native libs and Java cannot access the MAC address directly. * * In spirit, implements the IETF UUID draft specification, found here:
- * http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/draft-leach-uuids-guids-01 - * .txt + * http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/draft-leach-uuids-guids-01 .txt * * @author Abe White, Kevin Sutter * @since 0.3.3 @@ -95,9 +82,9 @@ public class UUIDGenerator { private static final int MAX_14BIT = 0x3FFF; private short _seq = 0; private boolean type1Initialized = false; /* - * Initializer for type 1 UUIDs. Creates random generator and genenerates - * the node portion of the UUID using the IP address. - */ + * Initializer for type 1 UUIDs. Creates random generator and genenerates + * the node portion of the UUID using the IP address. + */ private synchronized void initializeForType1() { if (type1Initialized == true) { @@ -123,6 +110,7 @@ public class UUIDGenerator { } type1Initialized = true; } + /** * Return a unique UUID value. */ @@ -132,6 +120,7 @@ public class UUIDGenerator { } return createType1(); } + /* * Creates a type 1 UUID */ @@ -142,11 +131,11 @@ public class UUIDGenerator { // set ip addr byte[] uuid = new byte[16]; System.arraycopy(IP, 0, uuid, 10, IP.length); - // Set time info. Have to do this processing within a synchronized + // Set time info. Have to do this processing within a synchronized // block because of the statics... long now = 0; synchronized (UUIDGenerator.class) { - // Get the time to use for this uuid. This method has the side + // Get the time to use for this uuid. This method has the side // effect of modifying the clock sequence, as well. now = getTime(); // Insert the resulting clock sequence into the uuid @@ -166,6 +155,7 @@ public class UUIDGenerator { uuid[IDX_TYPE] |= TYPE_TIME_BASED; // 0001 0000 return uuid; } + /* * Creates a type 4 UUID */ @@ -176,9 +166,9 @@ public class UUIDGenerator { longToBytes(type4.getLeastSignificantBits(), uuid, 8); return uuid; } + /* - * Converts a long to byte values, setting them in a byte array - * at a given starting position. + * Converts a long to byte values, setting them in a byte array at a given starting position. */ private void longToBytes(long longVal, byte[] buf, int sPos) { sPos += 7; @@ -196,15 +186,16 @@ public class UUIDGenerator { return new String(bytes); } } + /** * Return the next unique uuid value as a 32-character hex string. */ public String nextHex(int type) { return Base16Encoder.encode(next(type)); } + /** - * Get the timestamp to be used for this uuid. Must be called from - * a synchronized block. + * Get the timestamp to be used for this uuid. Must be called from a synchronized block. * * @return long timestamp */ @@ -219,12 +210,11 @@ public class UUIDGenerator { _lastMillis = newTime; return newTime; } + /** - * Gets the appropriately modified timestamep for the UUID. Must be called - * from a synchronized block. + * Gets the appropriately modified timestamep for the UUID. Must be called from a synchronized block. * - * @return long timestamp in 100ns intervals since the Gregorian change - * offset + * @return long timestamp in 100ns intervals since the Gregorian change offset */ private long getUUIDTime() { if (_currentMillis != System.currentTimeMillis()) { @@ -233,7 +223,7 @@ public class UUIDGenerator { } // check to see if we have created too many uuid's for this timestamp if (_counter + 1 >= MILLI_MULT) { - // Original algorithm threw exception. Seemed like overkill. + // Original algorithm threw exception. Seemed like overkill. // Let's just increment the timestamp instead and start over... _currentMillis++; _counter = 0; @@ -243,9 +233,9 @@ public class UUIDGenerator { // return the uuid time plus the artificial tick counter incremented return currentTime + _counter++; } + /** - * Increments the clock sequence for this uuid. Must be called from a - * synchronized block. + * Increments the clock sequence for this uuid. Must be called from a synchronized block. */ private void incrementSequence() { // increment, but if it's greater than its 14-bits, reset it @@ -257,17 +247,22 @@ public class UUIDGenerator { // Add Dependant classes internally /** - * This class came from the Apache Commons Id sandbox project in support - * of the UUIDGenerator implementation. + * This class came from the Apache Commons Id sandbox project in support of the UUIDGenerator implementation. * - *

Static methods for managing byte arrays (all methods follow Big - * Endian order where most significant bits are in front).

+ *

+ * Static methods for managing byte arrays (all methods follow Big Endian order where most significant bits are in + * front). + *

*/ public static final class Bytes { + /** - *

Hide constructor in utility class.

+ *

+ * Hide constructor in utility class. + *

*/ private Bytes() {} + /** * Appends two bytes array into one. * @@ -281,6 +276,7 @@ public class UUIDGenerator { System.arraycopy(b, 0, z, a.length, b.length); return z; } + /** * Returns a 8-byte array built from a long. * @@ -290,9 +286,9 @@ public class UUIDGenerator { public static byte[] toBytes(long n) { return toBytes(n, new byte[8]); } + /** - * Build a 8-byte array from a long. No check is performed on the - * array length. + * Build a 8-byte array from a long. No check is performed on the array length. * * @param n The number to convert. * @param b The array to fill. @@ -317,6 +313,7 @@ public class UUIDGenerator { return b; } + /** * Build a long from first 8 bytes of the array. * @@ -324,8 +321,7 @@ public class UUIDGenerator { * @return A long. */ public static long toLong(byte[] b) { - return ((((long) b[7]) & 0xFF) - + ((((long) b[6]) & 0xFF) << 8) + return ((((long) b[7]) & 0xFF) + ((((long) b[6]) & 0xFF) << 8) + ((((long) b[5]) & 0xFF) << 16) + ((((long) b[4]) & 0xFF) << 24) + ((((long) b[3]) & 0xFF) << 32) @@ -333,6 +329,7 @@ public class UUIDGenerator { + ((((long) b[1]) & 0xFF) << 48) + ((((long) b[0]) & 0xFF) << 56)); } + /** * Compares two byte arrays for equality. * @@ -352,14 +349,15 @@ public class UUIDGenerator { } return true; } + /** - *

Compares two byte arrays as specified by Comparable. + *

+ * Compares two byte arrays as specified by Comparable. * * @param lhs - left hand value in the comparison operation. * @param rhs - right hand value in the comparison operation. - * @return a negative integer, zero, or a positive integer as - * lhs is less than, equal to, or greater than - * rhs. + * @return a negative integer, zero, or a positive integer as lhs is less than, equal to, or + * greater than rhs. */ public static int compareTo(byte[] lhs, byte[] rhs) { if (lhs == rhs) { @@ -383,6 +381,7 @@ public class UUIDGenerator { } return 0; } + /** * Build a short from first 2 bytes of the array. * @@ -393,6 +392,7 @@ public class UUIDGenerator { return (short) ((b[1] & 0xFF) + ((b[0] & 0xFF) << 8)); } } + /** * Base 16 encoder. * @@ -400,10 +400,9 @@ public class UUIDGenerator { */ public static final class Base16Encoder { - private static final char[] HEX = new char[] { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' - }; + private static final char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', + 'D', 'E', 'F' }; + /** * Convert bytes to a base16 string. */ @@ -413,6 +412,7 @@ public class UUIDGenerator { for (int j = 1; j >= 0; j--) hexBuffer.append(HEX[(byteArray[i] >> (j * 4)) & 0xF]); return hexBuffer.toString(); } + /** * Convert a base16 string into a byte array. */ diff --git a/src/main/java/gtPlusPlus/api/objects/random/XSTR.java b/src/main/java/gtPlusPlus/api/objects/random/XSTR.java index aaaa7a4d1d..087f9535ce 100644 --- a/src/main/java/gtPlusPlus/api/objects/random/XSTR.java +++ b/src/main/java/gtPlusPlus/api/objects/random/XSTR.java @@ -1,36 +1,28 @@ package gtPlusPlus.api.objects.random; + /** - * A subclass of java.util.random that implements the Xorshift random number - * generator + * A subclass of java.util.random that implements the Xorshift random number generator * - * - it is 30% faster than the generator from Java's library - it produces - * random sequences of higher quality than java.util.Random - this class also - * provides a clone() function + * - it is 30% faster than the generator from Java's library - it produces random sequences of higher quality than + * java.util.Random - this class also provides a clone() function * - * Usage: XSRandom rand = new XSRandom(); //Instantiation x = rand.nextInt(); - * //pull a random number + * Usage: XSRandom rand = new XSRandom(); //Instantiation x = rand.nextInt(); //pull a random number * - * To use the class in legacy code, you may also instantiate an XSRandom object - * and assign it to a java.util.Random object: java.util.Random rand = new - * XSRandom(); + * To use the class in legacy code, you may also instantiate an XSRandom object and assign it to a java.util.Random + * object: java.util.Random rand = new XSRandom(); * - * for an explanation of the algorithm, see - * http://demesos.blogspot.com/2011/09/pseudo-random-number-generators.html + * for an explanation of the algorithm, see http://demesos.blogspot.com/2011/09/pseudo-random-number-generators.html * - * @author Wilfried Elmenreich University of Klagenfurt/Lakeside Labs - * http://www.elmenreich.tk + * @author Wilfried Elmenreich University of Klagenfurt/Lakeside Labs http://www.elmenreich.tk * - * This code is released under the GNU Lesser General Public License Version 3 - * http://www.gnu.org/licenses/lgpl-3.0.txt + * This code is released under the GNU Lesser General Public License Version 3 + * http://www.gnu.org/licenses/lgpl-3.0.txt */ import java.util.Random; import java.util.concurrent.atomic.AtomicLong; /** - * XSTR - Xorshift ThermiteRandom - * Modified by Bogdan-G - * 03.06.2016 - * version 0.0.4 + * XSTR - Xorshift ThermiteRandom Modified by Bogdan-G 03.06.2016 version 0.0.4 */ public class XSTR extends Random implements Cloneable { @@ -40,16 +32,14 @@ public class XSTR extends Random implements Cloneable { private static final long GAMMA = 0x9e3779b97f4a7c15L; private static final int PROBE_INCREMENT = 0x9e3779b9; private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL; - private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) + private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24) /* - MODIFIED BY: Robotia - Modification: Implemented Random class seed generator - */ + * MODIFIED BY: Robotia Modification: Implemented Random class seed generator + */ /** - * Creates a new pseudo random number generator. The seed is initialized to - * the current time, as if by + * Creates a new pseudo random number generator. The seed is initialized to the current time, as if by * setSeed(System.currentTimeMillis());. */ public XSTR() { @@ -61,7 +51,7 @@ public class XSTR extends Random implements Cloneable { private static long seedUniquifier() { // L'Ecuyer, "Tables of Linear Congruential Generators of // Different Sizes and Good Lattice Structure", 1999 - for (; ; ) { + for (;;) { final long current = seedUniquifier.get(); final long next = current * 181783497276652981L; if (seedUniquifier.compareAndSet(current, next)) { @@ -71,8 +61,8 @@ public class XSTR extends Random implements Cloneable { } /** - * Creates a new pseudo random number generator, starting with the specified - * seed, using setSeed(seed);. + * Creates a new pseudo random number generator, starting with the specified seed, using + * setSeed(seed);. * * @param seed the initial seed */ @@ -89,6 +79,7 @@ public class XSTR extends Random implements Cloneable { public double nextDouble() { return (((long) (this.next(26)) << 27) + this.next(27)) * DOUBLE_UNIT; } + /** * Returns the current state of the seed, can be used to clone the object * @@ -99,9 +90,8 @@ public class XSTR extends Random implements Cloneable { } /** - * Sets the seed for this pseudo random number generator. As described - * above, two instances of the same random class, starting with the same - * seed, produce the same results, if the same methods are called. + * Sets the seed for this pseudo random number generator. As described above, two instances of the same random + * class, starting with the same seed, produce the same results, if the same methods are called. * * @param seed the new seed */ @@ -125,9 +115,8 @@ public class XSTR extends Random implements Cloneable { } /** - * Implementation of George Marsaglia's elegant Xorshift random generator - * 30% faster and better quality than the built-in java.util.random see also - * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml + * Implementation of George Marsaglia's elegant Xorshift random generator 30% faster and better quality than the + * built-in java.util.random see also see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml * * @param nbits * @return @@ -164,16 +153,16 @@ public class XSTR extends Random implements Cloneable { this.haveNextNextGaussian = true; return v1 * multiplier; } + /** - * Returns a pseudorandom, uniformly distributed {@code int} value between 0 - * (inclusive) and the specified value (exclusive), drawn from this random - * number generator's sequence. The general contract of {@code nextInt} is - * that one {@code int} value in the specified range is pseudorandomly - * generated and returned. All {@code bound} possible {@code int} values are - * produced with (approximately) equal probability. The method - * {@code nextInt(int bound)} is implemented by class {@code Random} as if - * by: - *

 {@code
+     * Returns a pseudorandom, uniformly distributed {@code int} value between 0 (inclusive) and the specified value
+     * (exclusive), drawn from this random number generator's sequence. The general contract of {@code nextInt} is that
+     * one {@code int} value in the specified range is pseudorandomly generated and returned. All {@code bound} possible
+     * {@code int} values are produced with (approximately) equal probability. The method {@code nextInt(int bound)} is
+     * implemented by class {@code Random} as if by:
+     * 
+     * 
+     *  {@code
      * public int nextInt(int bound) {
      *   if (bound <= 0)
      *     throw new IllegalArgumentException("bound must be positive");
@@ -187,35 +176,29 @@ public class XSTR extends Random implements Cloneable {
      *       val = bits % bound;
      *   } while (bits - val + (bound-1) < 0);
      *   return val;
-     * }}
+ * }} + *
* - *

The hedge "approx - * imately" is used in the foregoing description only because the next - * method is only approximately an unbiased source of independently chosen - * bits. If it were a perfect source of randomly chosen bits, then the - * algorithm shown would choose {@code int} values from the stated range - * with perfect uniformity. *

- * The algorithm is slightly tricky. It rejects values that would result in - * an uneven distribution (due to the fact that 2^31 is not divisible by n). - * The probability of a value being rejected depends on n. The worst case is - * n=2^30+1, for which the probability of a reject is 1/2, and the expected - * number of iterations before the loop terminates is 2. + * The hedge "approx imately" is used in the foregoing description only because the next method is only + * approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen + * bits, then the algorithm shown would choose {@code int} values from the stated range with perfect uniformity. *

- * The algorithm treats the case where n is a power of two specially: it - * returns the correct number of high-order bits from the underlying - * pseudo-random number generator. In the absence of special treatment, the - * correct number of low-order bits would be returned. Linear - * congruential pseudo-random number generators such as the one implemented - * by this class are known to have short periods in the sequence of values - * of their low-order bits. Thus, this special case greatly increases the - * length of the sequence of values returned by successive calls to this - * method if n is a small power of two. + * The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact + * that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is + * n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop + * terminates is 2. + *

+ * The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order + * bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number + * of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one + * implemented by this class are known to have short periods in the sequence of values of their low-order bits. + * Thus, this special case greatly increases the length of the sequence of values returned by successive calls to + * this method if n is a small power of two. * * @param bound the upper bound (exclusive). Must be positive. - * @return the next pseudorandom, uniformly distributed {@code int} value - * between zero (inclusive) and {@code bound} (exclusive) from this random - * number generator's sequence + * @return the next pseudorandom, uniformly distributed {@code int} value between zero (inclusive) and {@code bound} + * (exclusive) from this random number generator's sequence * @throws IllegalArgumentException if bound is not positive * @since 1.2 */ @@ -229,18 +212,10 @@ public class XSTR extends Random implements Cloneable { newBound = bound; } - /*int r = next(31); - int m = bound - 1; - if ((bound & m) == 0) // i.e., bound is a power of 2 - { - r = (int) ((bound * (long) r) >> 31); - } else { - for (int u = r; - u - (r = u % bound) + m < 0; - u = next(31)) - ; - } - return r;*/ + /* + * int r = next(31); int m = bound - 1; if ((bound & m) == 0) // i.e., bound is a power of 2 { r = (int) ((bound + * * (long) r) >> 31); } else { for (int u = r; u - (r = u % bound) + m < 0; u = next(31)) ; } return r; + */ // speedup, new nextInt ~+40% this.last = this.seed ^ (this.seed << 21); this.last ^= (this.last >>> 35); @@ -268,10 +243,9 @@ public class XSTR extends Random implements Cloneable { @Override public void nextBytes(final byte[] bytes_arr) { - for (int iba = 0, lenba = bytes_arr.length; iba < lenba; ) { - for (int rndba = this.nextInt(), nba = Math.min(lenba - iba, Integer.SIZE / Byte.SIZE); - nba-- > 0; - rndba >>= Byte.SIZE) { + for (int iba = 0, lenba = bytes_arr.length; iba < lenba;) { + for (int rndba = this.nextInt(), nba = Math.min(lenba - iba, Integer.SIZE / Byte.SIZE); nba-- + > 0; rndba >>= Byte.SIZE) { bytes_arr[iba++] = (byte) rndba; } } -- cgit