All Downloads are FREE. Search and download functionalities are using the official Maven repository.

toxgene.core.genes.literals.ToxNumber Maven / Gradle / Ivy

/**
 * Generates random numeric values.
 *
 * @author Denilson Barbosa
 * @version 0.1
 */

package toxgene.core.genes.literals;

import toxgene.core.ToXgeneErrorException;
import toxgene.core.random.ToxRandom;

import java.io.PrintStream;
import java.text.DecimalFormat;

public class ToxNumber extends LiteralGene{
  public static final int INTEGER = 1;
  public static final int REAL = 2;

  /**
   * Type of the number to be generated:
   * 

*

    *
  • integer *
  • real *
*/ private int type; /** * Pattern for presenting the number. */ private String pattern; /** * Used for ensuring only legal values are returned */ private float minInclusive, maxInclusive; /** * Used for generating sequentially increasing numbers. */ private double current, increment; /** * Determines whether these number should be generated sequentially. */ private boolean sequential; /** * Determines whether this is a decreasing sequence of numbers. */ private boolean decreasing = false; /** * Pseudo-random number generator */ private ToxRandom rand; /** * DecimalFormat formats the printing of the numbers generated. */ private DecimalFormat nf; /** * Constructor for random real tox-numbers; specifies the number of * decimal places to be used and whether the numbers are sequentially * generated or not. */ public ToxNumber(int type, float minInclusive, float maxInclusive, boolean sequential, String pattern, ToxRandom rand){ this.sequential = sequential; this.type = type; this.minInclusive = minInclusive; this.maxInclusive = maxInclusive; if (pattern.length() != 0){ this.pattern = pattern; } else{ //these are the default formatting patterns; if (type == INTEGER){ pattern = "0;-0"; } else{ pattern = "0.#;-0.#"; } } this.rand = rand; if (sequential){ increment = (float) rand.nextInt(); if (increment <0){ this.current = this.maxInclusive; decreasing = true; } else{ this.current = this.minInclusive; } } nf = new DecimalFormat(pattern); } /** * Returns the base type for literals of this gene. */ public String baseType(){ if (type == INTEGER){ return ("integer"); } return ("real"); } /** * Outputs a random value of this gene. */ public void generate(PrintStream outStream){ switch (type){ case INTEGER: { if (sequential){ //check whether this value is within the allowed range if ((current < minInclusive) || (current> maxInclusive)){ throw new ToXgeneErrorException("value \""+nf.format(current) +"\" out of interval ["+minInclusive+","+maxInclusive +"] in sequential tox-number."); } outStream.print(nf.format(current)); current = current + increment; } else{ outStream.print(nf.format(rand.nextInt())); } break; } case REAL:{ if (sequential){ //check whether this value is within the allowed range if ((current < minInclusive) || (current > maxInclusive)){ throw new ToXgeneErrorException("value \""+nf.format(current) +"\" out of interval ["+minInclusive+"," +maxInclusive+"] in sequential tox-number."); } outStream.print(nf.format(current)); current += increment; } else{ outStream.print(nf.format(rand.nextFloat())); } break; } } } /** * Resets the gene. Affects only genes for sequential numbers. */ public void reset(){ if (sequential){ if (increment > 0){ current = minInclusive; } else{ current = maxInclusive; } } } };




© 2015 - 2025 Weber Informatics LLC | Privacy Policy