001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.impl;
011:
012: import org.jgap.*;
013:
014: /**
015: * A random generator only determined for testing purposes. With this, you can
016: * specify the next value which will be returned. It is also possible to
017: * specify a sequence to be produced.
018: *
019: * @author Klaus Meffert
020: * @since 1.1
021: */
022: public class RandomGeneratorForTesting implements RandomGenerator,
023: java.io.Serializable {
024:
025: /** String containing the CVS revision. Read out via reflection!*/
026: private static final String CVS_REVISION = "$Revision: 1.1 $";
027:
028: private long m_nextLong;
029: private double m_nextDouble;
030: private boolean m_nextBoolean;
031: private int[] m_nextIntSequence;
032: private float[] m_nextFloatSequence;
033: private double m_nextGaussian;
034: private int m_intIndex, m_floatIndex;
035:
036: public RandomGeneratorForTesting() {
037: }
038:
039: public RandomGeneratorForTesting(int a_nextInt) {
040: this ();
041: setNextInt(a_nextInt);
042: }
043:
044: public RandomGeneratorForTesting(double a_nextDouble) {
045: this ();
046: setNextDouble(a_nextDouble);
047: }
048:
049: public RandomGeneratorForTesting(float a_nextFloat) {
050: this ();
051: setNextFloat(a_nextFloat);
052: }
053:
054: public RandomGeneratorForTesting(long a_nextLong) {
055: this ();
056: setNextLong(a_nextLong);
057: }
058:
059: public RandomGeneratorForTesting(boolean a_nextBoolean) {
060: this ();
061: setNextBoolean(a_nextBoolean);
062: setNextInt(1);
063: }
064:
065: public int nextInt() {
066: int result = m_nextIntSequence[m_intIndex++];
067: if (m_intIndex >= m_nextIntSequence.length) {
068: m_intIndex = 0;
069: }
070: return result;
071: }
072:
073: public int nextInt(int a_ceiling) {
074: return nextInt() % a_ceiling;
075: }
076:
077: public long nextLong() {
078: return m_nextLong;
079: }
080:
081: public double nextDouble() {
082: return m_nextDouble;
083: }
084:
085: public double nextGaussian() {
086: return m_nextGaussian;
087: }
088:
089: public float nextFloat() {
090: float result = m_nextFloatSequence[m_floatIndex++];
091: if (m_floatIndex >= m_nextFloatSequence.length) {
092: m_floatIndex = 0;
093: }
094: return result;
095: }
096:
097: public boolean nextBoolean() {
098: return m_nextBoolean;
099: }
100:
101: public void setNextBoolean(boolean a_nextBoolean) {
102: m_nextBoolean = a_nextBoolean;
103: }
104:
105: public void setNextDouble(double a_nextDouble) {
106: m_nextDouble = a_nextDouble % 1.0d;
107: }
108:
109: public void setNextGaussian(double a_nextDouble) {
110: m_nextGaussian = a_nextDouble;
111: }
112:
113: public void setNextFloat(float a_nextFloat) {
114: setNextFloatSequence(new float[] { a_nextFloat % 1.0f });
115: }
116:
117: public void setNextInt(int a_nextInt) {
118: setNextIntSequence(new int[] { a_nextInt });
119: }
120:
121: public void setNextLong(long a_nextLong) {
122: m_nextLong = a_nextLong;
123: }
124:
125: public void setNextFloatSequence(float[] a_sequence) {
126: m_floatIndex = 0;
127: m_nextFloatSequence = a_sequence;
128: }
129:
130: public void setNextIntSequence(int[] a_sequence) {
131: m_intIndex = 0;
132: m_nextIntSequence = a_sequence;
133: }
134: }
|