001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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@gmx.at)
019 */
020 package org.jenetics.util;
021
022 import static java.lang.Math.min;
023
024 import java.util.Objects;
025 import java.util.Random;
026 import java.util.function.LongSupplier;
027
028 import org.jenetics.internal.math.random;
029
030 /**
031 * An abstract base class which eases the implementation of {@code Random}
032 * objects which natively creates random {@code long} values. All other
033 * {@code Random} functions are optimized using this {@code long} values.
034 *
035 * <pre>{@code
036 * public class MyRandom64 extends Random64 {
037 * \@Override
038 * public long nextLong() {
039 * // Only this method must be implemented.
040 * ...
041 * }
042 * }
043 * }</pre>
044 *
045 * @deprecated This random class implementation has been moved to a separate
046 * module. Use the implementation of the
047 * <a href="https://github.com/jenetics/prngine">PRNGine</a> instead.
048 *
049 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
050 * @since 1.1
051 * @version 2.0
052 */
053 @Deprecated
054 public abstract class Random64 extends PRNG {
055
056 private static final long serialVersionUID = 1L;
057
058 protected Random64(final long seed) {
059 super(seed);
060 }
061
062 protected Random64() {
063 this(random.seed());
064 }
065
066 /**
067 * Force to explicitly override the Random.nextLong() method. All other
068 * methods of this class are implemented by calling this method.
069 */
070 @Override
071 public abstract long nextLong();
072
073
074 @Override
075 public boolean nextBoolean() {
076 return (nextLong() & 0x8000000000000000L) != 0L;
077 }
078
079 @Override
080 public int nextInt() {
081 return (int)(nextLong() >>> Integer.SIZE);
082 }
083
084 @Override
085 protected int next(final int bits) {
086 return (int)(nextLong() >>> (Long.SIZE - bits));
087 }
088
089 /**
090 * Optimized version of the {@link Random#nextBytes(byte[])} method for
091 * 64-bit random engines.
092 */
093 @Override
094 public void nextBytes(final byte[] bytes) {
095 for (int i = 0, len = bytes.length; i < len;) {
096 int n = min(len - i, Long.BYTES);
097
098 for (long x = nextLong(); --n >= 0; x >>= Byte.SIZE) {
099 bytes[i++] = (byte)x;
100 }
101 }
102 }
103
104 @Override
105 public float nextFloat() {
106 return random.toFloat2(nextLong());
107 }
108
109 /**
110 * Optimized version of the {@link Random#nextDouble()} method for 64-bit
111 * random engines.
112 */
113 @Override
114 public double nextDouble() {
115 return random.toDouble2(nextLong());
116 }
117
118
119 /**
120 * Create a new {@code Random64} instance, where the random numbers are
121 * generated by the given long {@code supplier}.
122 *
123 * @param supplier the random number supplier
124 * @return a new {@code Random64} instance
125 * @throws java.lang.NullPointerException if the given {@code supplier} is
126 * {@code null}.
127 */
128 public static Random64 of(final LongSupplier supplier) {
129 Objects.requireNonNull(supplier);
130
131 return new Random64() {
132 private static final long serialVersionUID = 1L;
133
134 @Override
135 public long nextLong() {
136 return supplier.getAsLong();
137 }
138
139 @Override
140 public void setSeed(final long seed) {
141 throw new UnsupportedOperationException(
142 "The 'setSeed(long)' method is not supported this instance."
143 );
144 }
145 };
146 }
147
148 }
|