Random64.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.8.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  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
046  @since 1.1
047  @version 2.0
048  */
049 public abstract class Random64 extends PRNG {
050 
051     private static final long serialVersionUID = 1L;
052 
053     protected Random64(final long seed) {
054         super(seed);
055     }
056 
057     protected Random64() {
058         this(random.seed());
059     }
060 
061     /**
062      * Force to explicitly override the Random.nextLong() method. All other
063      * methods of this class are implemented by calling this method.
064      */
065     @Override
066     public abstract long nextLong();
067 
068 
069     @Override
070     public boolean nextBoolean() {
071         return (nextLong() 0x8000000000000000L!= 0L;
072     }
073 
074     @Override
075     public int nextInt() {
076         return (int)(nextLong() >>> Integer.SIZE);
077     }
078 
079     @Override
080     protected int next(final int bits) {
081         return (int)(nextLong() >>> (Long.SIZE - bits));
082     }
083 
084     /**
085      * Optimized version of the {@link Random#nextBytes(byte[])} method for
086      * 64-bit random engines.
087      */
088     @Override
089     public void nextBytes(final byte[] bytes) {
090         for (int i = 0, len = bytes.length; i < len;) {
091             int n = min(len - i, Long.BYTES);
092 
093             for (long x = nextLong(); --n >= 0; x >>= Byte.SIZE) {
094                 bytes[i++(byte)x;
095             }
096         }
097     }
098 
099     @Override
100     public float nextFloat() {
101         return random.toFloat2(nextLong());
102     }
103 
104     /**
105      * Optimized version of the {@link Random#nextDouble()} method for 64-bit
106      * random engines.
107      */
108     @Override
109     public double nextDouble() {
110         return random.toDouble2(nextLong());
111     }
112 
113 
114     /**
115      * Create a new {@code Random64} instance, where the random numbers are
116      * generated by the given long {@code supplier}.
117      *
118      @param supplier the random number supplier
119      @return a new {@code Random64} instance
120      @throws java.lang.NullPointerException if the given {@code supplier} is
121      *         {@code null}.
122      */
123     public static Random64 of(final LongSupplier supplier) {
124         Objects.requireNonNull(supplier);
125 
126         return new Random64() {
127             private static final long serialVersionUID = 1L;
128 
129             @Override
130             public long nextLong() {
131                 return supplier.getAsLong();
132             }
133 
134             @Override
135             public void setSeed(final long seed) {
136                 throw new UnsupportedOperationException(
137                     "The 'setSeed(long)' method is not supported this instance."
138                 );
139             }
140         };
141     }
142 
143 }