randomgeneratorfortest.java

来自「jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦」· Java 代码 · 共 135 行

JAVA
135
字号
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.impl;

import org.jgap.*;

/**
 * A random generator only determined for testing purposes. With this, you can
 * specify the next value which will be returned. It is also possible to
 * specify a sequence to be produced.
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class RandomGeneratorForTest
    implements RandomGenerator, java.io.Serializable {

  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.14 $";

  private long m_nextLong;
  private double m_nextDouble;
  private boolean m_nextBoolean;
  private int[] m_nextIntSequence;
  private float[] m_nextFloatSequence;
  private double m_nextGaussian;
  private int m_intIndex, m_floatIndex;

  public RandomGeneratorForTest() {
  }

  public RandomGeneratorForTest(int a_nextInt) {
    this();
    setNextInt(a_nextInt);
  }

  public RandomGeneratorForTest(double a_nextDouble) {
    this();
    setNextDouble(a_nextDouble);
  }

  public RandomGeneratorForTest(float a_nextFloat) {
    this();
    setNextFloat(a_nextFloat);
  }

  public RandomGeneratorForTest(long a_nextLong) {
    this();
    setNextLong(a_nextLong);
  }

  public RandomGeneratorForTest(boolean a_nextBoolean) {
    this();
    setNextBoolean(a_nextBoolean);
    setNextInt(1);
  }

  public int nextInt() {
    int result = m_nextIntSequence[m_intIndex++];
    if (m_intIndex >= m_nextIntSequence.length) {
      m_intIndex = 0;
    }
    return result;
  }

  public int nextInt(int a_ceiling) {
    return nextInt() % a_ceiling;
  }

  public long nextLong() {
    return m_nextLong;
  }

  public double nextDouble() {
    return m_nextDouble;
  }

  public double nextGaussian() {
    return m_nextGaussian;
  }

  public float nextFloat() {
    float result = m_nextFloatSequence[m_floatIndex++];
    if (m_floatIndex >= m_nextFloatSequence.length) {
      m_floatIndex = 0;
    }
    return result;
  }

  public boolean nextBoolean() {
    return m_nextBoolean;
  }

  public void setNextBoolean(boolean a_nextBoolean) {
    m_nextBoolean = a_nextBoolean;
  }

  public void setNextDouble(double a_nextDouble) {
    m_nextDouble = a_nextDouble % 1.0d;
  }

  public void setNextGaussian(double a_nextDouble) {
    m_nextGaussian = a_nextDouble;
  }

  public void setNextFloat(float a_nextFloat) {
    setNextFloatSequence(new float[] {a_nextFloat % 1.0f});
  }

  public void setNextInt(int a_nextInt) {
    setNextIntSequence(new int[] {a_nextInt});
  }

  public void setNextLong(long a_nextLong) {
    m_nextLong = a_nextLong;
  }

  public void setNextFloatSequence(float[] a_sequence) {
    m_floatIndex = 0;
    m_nextFloatSequence = a_sequence;
  }

  public void setNextIntSequence(int[] a_sequence) {
    m_intIndex = 0;
    m_nextIntSequence = a_sequence;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?