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 java.util.Random;
023
024 /**
025 * Abstract {@code Random} class with additional <i>next</i> random number
026 * methods.
027 *
028 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
029 * @since 1.2
030 * @version 1.2 — <em>$Date: 2013-12-16 $</em>
031 */
032 abstract class PRNG extends Random {
033
034 private static final long serialVersionUID = 1L;
035
036 /**
037 * Create a new {@code PRNG} instance with the given {@code seed}.
038 *
039 * @param seed the seed of the new {@code PRNG} instance.
040 */
041 protected PRNG(long seed) {
042 super(seed);
043 }
044
045 /**
046 * Create a new {@code PRNG} instance with a seed created with the
047 * {@link math.random#seed()} value.
048 */
049 protected PRNG() {
050 this(math.random.seed());
051 }
052
053 /**
054 * Returns a pseudorandom, uniformly distributed int value between min and
055 * max (end points included).
056 *
057 * @param min lower bound for generated integer
058 * @param max upper bound for generated integer
059 * @return a random integer greater than or equal to {@code min} and less
060 * than or equal to {@code max}
061 * @throws IllegalArgumentException if {@code min >= max}
062 *
063 * @see math.random#nextInt(Random, int, int)
064 */
065 public int nextInt(final int min, final int max) {
066 return math.random.nextInt(this, min, max);
067 }
068
069 /**
070 * Returns a pseudorandom, uniformly distributed int value between min
071 * and max (end points included).
072 *
073 * @param min lower bound for generated long integer
074 * @param max upper bound for generated long integer
075 * @return a random long integer greater than or equal to {@code min}
076 * and less than or equal to {@code max}
077 * @throws IllegalArgumentException if {@code min >= max}
078 *
079 * @see math.random#nextLong(Random, long, long)
080 */
081 public long nextLong(final long min, final long max) {
082 return math.random.nextLong(this, min, max);
083 }
084
085 /**
086 * Returns a pseudorandom, uniformly distributed int value between 0
087 * (inclusive) and the specified value (exclusive), drawn from the given
088 * random number generator's sequence.
089 *
090 * @param n the bound on the random number to be returned. Must be
091 * positive.
092 * @return the next pseudorandom, uniformly distributed int value
093 * between 0 (inclusive) and n (exclusive) from the given random
094 * number generator's sequence
095 * @throws IllegalArgumentException if n is smaller than 1.
096 *
097 * @see math.random#nextLong(Random, long)
098 */
099 public long nextLong(final long n) {
100 return math.random.nextLong(this, n);
101 }
102
103 /**
104 * Returns a pseudorandom, uniformly distributed double value between
105 * min (inclusively) and max (exclusively).
106 *
107 * @param min lower bound for generated float value
108 * @param max upper bound for generated float value
109 * @return a random float greater than or equal to {@code min} and less
110 * than to {@code max}
111 *
112 * @see math.random#nextFloat(Random, float, float)
113 */
114 public float nextFloat(final float min, final float max) {
115 return math.random.nextFloat(this, min, max);
116 }
117
118 /**
119 * Returns a pseudorandom, uniformly distributed double value between
120 * min (inclusively) and max (exclusively).
121 *
122 * @param min lower bound for generated double value
123 * @param max upper bound for generated double value
124 * @return a random double greater than or equal to {@code min} and less
125 * than to {@code max}
126 *
127 * @see math.random#nextDouble(Random, double, double)
128 */
129 public double nextDouble(final double min, final double max) {
130 return math.random.nextDouble(this, min, max);
131 }
132
133 }
|