Random64.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 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.Random;
025 
026 import org.jenetics.internal.math.random;
027 
028 /**
029  * An abstract base class which eases the implementation of {@code Random}
030  * objects which natively creates random {@code long} values. All other
031  * {@code Random} functions are optimized using this {@code long} values.
032  *
033  * [code]
034  * public class MyRandom64 extends Random64 {
035  *     \@Override
036  *     public long nextLong() {
037  *         // Only this method must be implemented.
038  *         ...
039  *     }
040  * }
041  * [/code]
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044  @since 1.3
045  @version 1.3 &mdash; <em>$Date: 2014-02-15 $</em>
046  */
047 public abstract class Random64 extends PRNG {
048 
049     private static final long serialVersionUID = 1L;
050 
051     private static final int LONG_BYTE_SIZE = 8;
052 
053     protected Random64(long seed) {
054         super(seed);
055     }
056 
057     protected Random64() {
058         this(math.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_BYTE_SIZE);
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 }