001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.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.util;
021
022import static java.util.Objects.requireNonNull;
023
024import java.io.Serial;
025import java.util.Random;
026import java.util.random.RandomGenerator;
027import java.util.stream.DoubleStream;
028import java.util.stream.IntStream;
029import java.util.stream.LongStream;
030
031/**
032 * Some places in the Java API still require a {@link Random} object instead of
033 * the new {@link RandomGenerator}. This class can be used by using this adapter
034 * class.
035 * {@snippet lang="java":
036 * final var random = RandomGenerator.getDefault();
037 * final var bi = new BigInteger(100, RandomAdapter.of(random));
038 * }
039 *
040 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
041 * @since 7.0
042 * @version 7.0
043 */
044public final class RandomAdapter extends Random {
045
046        @Serial
047        private static final long serialVersionUID = 1;
048
049        private final RandomGenerator _random;
050
051        private RandomAdapter(final RandomGenerator random) {
052                _random = requireNonNull(random);
053        }
054
055        @Override
056        public boolean isDeprecated() {
057                return _random.isDeprecated();
058        }
059
060        @Override
061        public DoubleStream doubles() {
062                return _random.doubles();
063        }
064
065        @Override
066        public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
067                return _random.doubles(randomNumberOrigin, randomNumberBound);
068        }
069
070        @Override
071        public DoubleStream doubles(long streamSize) {
072                return _random.doubles(streamSize);
073        }
074
075        @Override
076        public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) {
077                return _random.doubles(streamSize, randomNumberOrigin, randomNumberBound);
078        }
079
080        @Override
081        public IntStream ints() {
082                return _random.ints();
083        }
084
085        @Override
086        public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
087                return _random.ints(randomNumberOrigin, randomNumberBound);
088        }
089
090        @Override
091        public IntStream ints(long streamSize) {
092                return _random.ints(streamSize);
093        }
094
095        @Override
096        public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) {
097                return _random.ints(streamSize, randomNumberOrigin, randomNumberBound);
098        }
099
100        @Override
101        public LongStream longs() {
102                return _random.longs();
103        }
104
105        @Override
106        public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
107                return _random.longs(randomNumberOrigin, randomNumberBound);
108        }
109
110        @Override
111        public LongStream longs(long streamSize) {
112                return _random.longs(streamSize);
113        }
114
115        @Override
116        public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) {
117                return _random.longs(streamSize, randomNumberOrigin, randomNumberBound);
118        }
119
120        @Override
121        public boolean nextBoolean() {
122                return _random.nextBoolean();
123        }
124
125        @Override
126        public void nextBytes(byte[] bytes) {
127                _random.nextBytes(bytes);
128        }
129
130        @Override
131        public float nextFloat() {
132                return _random.nextFloat();
133        }
134
135        @Override
136        public float nextFloat(float bound) {
137                return _random.nextFloat(bound);
138        }
139
140        @Override
141        public float nextFloat(float origin, float bound) {
142                return _random.nextFloat(origin, bound);
143        }
144
145        @Override
146        public double nextDouble() {
147                return _random.nextDouble();
148        }
149
150        @Override
151        public double nextDouble(double bound) {
152                return _random.nextDouble(bound);
153        }
154
155        @Override
156        public double nextDouble(double origin, double bound) {
157                return _random.nextDouble(origin, bound);
158        }
159
160        @Override
161        public int nextInt() {
162                return _random.nextInt();
163        }
164
165        @Override
166        public int nextInt(int bound) {
167                return _random.nextInt(bound);
168        }
169
170        @Override
171        public int nextInt(int origin, int bound) {
172                return _random.nextInt(origin, bound);
173        }
174
175        @Override
176        public long nextLong() {
177                return _random.nextLong();
178        }
179
180        @Override
181        public long nextLong(long bound) {
182                return _random.nextLong(bound);
183        }
184
185        @Override
186        public long nextLong(long origin, long bound) {
187                return _random.nextLong(origin, bound);
188        }
189
190        @Override
191        public double nextGaussian() {
192                return _random.nextGaussian();
193        }
194
195        @Override
196        public double nextGaussian(double mean, double stddev) {
197                return _random.nextGaussian(mean, stddev);
198        }
199
200        @Override
201        public double nextExponential() {
202                return _random.nextExponential();
203        }
204
205        /**
206         * Create a new {@link Random} object from the given {@code random} generator.
207         *
208         * @param random the random generator to adapt
209         * @return the adapted random generator
210         */
211        public static Random of(final RandomGenerator random) {
212                return new RandomAdapter(random);
213        }
214}