001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 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 */
020package io.jenetics.ext.internal.util;
021
022import static java.lang.String.format;
023
024import java.math.BigInteger;
025import java.util.random.RandomGenerator;
026
027import io.jenetics.util.RandomAdapter;
028
029/**
030 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
031 * @version 3.5
032 * @since 3.5
033 */
034public class Randoms {
035        private Randoms() {}
036
037        /**
038         * Returns a pseudo-random, uniformly distributed int value between 0
039         * (inclusive) and the specified value (exclusive), drawn from the given
040         * random number generator's sequence.
041         *
042         * @param n the bound on the random number to be returned. Must be
043         *        positive.
044         * @param random the random engine used for creating the random number.
045         * @return the next pseudo-random, uniformly distributed int value
046         *         between 0 (inclusive) and n (exclusive) from the given random
047         *         number generator's sequence
048         * @throws IllegalArgumentException if n is smaller than 1.
049         * @throws NullPointerException if the given {@code random}
050         *         engine of the maximal value {@code n} is {@code null}.
051         */
052        public static BigInteger nextBigInteger(
053                final BigInteger n,
054                final RandomGenerator random
055        ) {
056                if (n.compareTo(BigInteger.ONE) < 0) {
057                        throw new IllegalArgumentException(format(
058                                "n is smaller than one: %d", n
059                        ));
060                }
061
062                BigInteger result = null;
063                if (n.bitLength() <= Integer.SIZE - 1) {
064                        result = BigInteger.valueOf(random.nextInt(n.intValue()));
065                } else if (n.bitLength() <= Long.SIZE - 1) {
066                        result = BigInteger.valueOf(random.nextLong(n.longValue()));
067                } else {
068                        final var rnd = RandomAdapter.of(random);
069                        do {
070                                result = new BigInteger(n.bitLength(), rnd).mod(n);
071                        } while (result.compareTo(n) > 0);
072                }
073
074                return result;
075        }
076
077        /**
078         * Returns a pseudo-random, uniformly distributed int value between min
079         * and max (min and max included).
080         *
081         * @param min lower bound for generated long integer (inclusively)
082         * @param max upper bound for generated long integer (exclusively)
083         * @param random the random engine to use for calculating the random
084         *        long value
085         * @return a random long integer greater than or equal to {@code min}
086         *         and less than or equal to {@code max}
087         * @throws IllegalArgumentException if {@code min >= max}
088         * @throws NullPointerException if one of the given parameters
089         *         are {@code null}.
090         */
091        public static BigInteger nextBigInteger(
092                final BigInteger min,
093                final BigInteger max,
094                final RandomGenerator random
095        ) {
096                if (min.compareTo(max) > 0) {
097                        throw new IllegalArgumentException(format(
098                                "min >= max: %d > %d.", min, max
099                        ));
100                }
101
102                final BigInteger n = max.subtract(min);
103                return nextBigInteger(n, random).add(min);
104        }
105
106}