001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.0).
003 * Copyright (c) 2007-2020 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.ext.internal;
021
022 import static java.lang.String.format;
023
024 import java.math.BigInteger;
025 import java.util.Random;
026
027 /**
028 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
029 * @version 3.5
030 * @since 3.5
031 */
032 public class random {
033 private random() {}
034
035 /**
036 * Returns a pseudo-random, uniformly distributed int value between 0
037 * (inclusive) and the specified value (exclusive), drawn from the given
038 * random number generator's sequence.
039 *
040 * @param n the bound on the random number to be returned. Must be
041 * positive.
042 * @param random the random engine used for creating the random number.
043 * @return the next pseudo-random, uniformly distributed int value
044 * between 0 (inclusive) and n (exclusive) from the given random
045 * number generator's sequence
046 * @throws IllegalArgumentException if n is smaller than 1.
047 * @throws NullPointerException if the given {@code random}
048 * engine is {@code null}.
049 */
050 public static long nextLong(final long n, final Random random) {
051 if (n <= 0) {
052 throw new IllegalArgumentException(format(
053 "n is smaller than one: %d", n
054 ));
055 }
056
057 long bits;
058 long result;
059 do {
060 bits = random.nextLong() & 0x7fffffffffffffffL;
061 result = bits%n;
062 } while (bits - result + (n - 1) < 0);
063
064 return result;
065 }
066
067 /**
068 * Returns a pseudo-random, uniformly distributed int value between 0
069 * (inclusive) and the specified value (exclusive), drawn from the given
070 * random number generator's sequence.
071 *
072 * @param n the bound on the random number to be returned. Must be
073 * positive.
074 * @param random the random engine used for creating the random number.
075 * @return the next pseudo-random, uniformly distributed int value
076 * between 0 (inclusive) and n (exclusive) from the given random
077 * number generator's sequence
078 * @throws IllegalArgumentException if n is smaller than 1.
079 * @throws NullPointerException if the given {@code random}
080 * engine of the maximal value {@code n} is {@code null}.
081 */
082 public static BigInteger nextBigInteger(
083 final BigInteger n,
084 final Random random
085 ) {
086 if (n.compareTo(BigInteger.ONE) < 0) {
087 throw new IllegalArgumentException(format(
088 "n is smaller than one: %d", n
089 ));
090 }
091
092 BigInteger result = null;
093 if (n.bitLength() <= Integer.SIZE - 1) {
094 result = BigInteger.valueOf(random.nextInt(n.intValue()));
095 } else if (n.bitLength() <= Long.SIZE - 1) {
096 result = BigInteger.valueOf(nextLong(n.longValue(), random));
097 } else {
098 do {
099 result = new BigInteger(n.bitLength(), random).mod(n);
100 } while (result.compareTo(n) > 0);
101 }
102
103 return result;
104 }
105
106 /**
107 * Returns a pseudo-random, uniformly distributed int value between min
108 * and max (min and max included).
109 *
110 * @param min lower bound for generated long integer (inclusively)
111 * @param max upper bound for generated long integer (inclusively)
112 * @param random the random engine to use for calculating the random
113 * long value
114 * @return a random long integer greater than or equal to {@code min}
115 * and less than or equal to {@code max}
116 * @throws IllegalArgumentException if {@code min >= max}
117 * @throws NullPointerException if one of the given parameters
118 * are {@code null}.
119 */
120 public static BigInteger nextBigInteger(
121 final BigInteger min,
122 final BigInteger max,
123 final Random random
124 ) {
125 if (min.compareTo(max) >= 0) {
126 throw new IllegalArgumentException(format(
127 "min >= max: %d >= %d.", min, max
128 ));
129 }
130
131 final BigInteger n = max.subtract(min).add(BigInteger.ONE);
132 return nextBigInteger(n, random).add(min);
133 }
134
135 public static BigInteger nextBigInteger(final Random random) {
136 return new BigInteger(100, random);
137 }
138
139 }
|