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

toxgene.core.random.ToxGaussian Maven / Gradle / Ivy

/**
 * Implements a random generator for gaussian probability distributions
 *
 * @author Denilson Barbosa
 * @version 0.1
 */

package toxgene.core.random;


import java.util.Random;
import toxgene.core.ToXgeneErrorException;
import toxgene.core.genes.CreateGeneException;

public class ToxGaussian implements ToxRandom{
  private float min, max, M, V, M2;
  private Random rand;

  public ToxGaussian(float min, float max, float M, float V, long seed){
		this.min = min;
		this.max = max;
		this.M = M;
		this.V = V;

		if (max < min){
			throw new CreateGeneException("invalid min and max values for "+
																		"ToxGaussian random generator!");
		}

		if (M < min || M > max){
			throw new CreateGeneException("invalid mean value for "+
																		"ToxGaussian random generator!");
		}

		rand = new Random(seed);
  }

  public long nextInt(){
		return (Math.round((double)nextFloat()));
  }

  public float nextFloat(){
		//gets the next random # using a gaussian distribution
		float gauss = (float) (V * rand.nextGaussian());
		//shift and scale the result to have the desired mean and variation
		float result = M + gauss;

		//check for consistency
		if (result < min)
			result = min;
		if (result > max)
			result = max;

		return result;
  }

  public float minValue(){
		return min;
  }

  public float maxValue(){
		return max;
  }

	/**
	 * this method readjusts the mean value for the distribution
	 */
  public long nextInt(float value){
		//in some cases we call this function with the max_value as parameter :(
		if (value == max)
			return ((long)nextFloat());
		
		if (value < min){
			throw new ToXgeneErrorException("cannot update distribution max value: "
																			+value
																			+" is smaller than minimum value: "+min);
		}

		M2 = M*(value-min)/(max-min);
		return (Math.round((double)M2-M+(nextFloat())));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy