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

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

/**
 * Implements a random generator for logNormal 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 ToxLogNormal implements ToxRandom{
  private float min, max, M, V, M2;
  private Random rand;

  public ToxLogNormal(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 "+
																		"ToxLogNormal random generator!");
		}

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

		rand = new Random(seed);
  }

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

  public float nextFloat(){
		//gets the next random # using a logNormal distribution, and
		//shifts/scales it to the specified parameters.
		float gauss = M + (float) (V * rand.nextGaussian());
		float result = (float) Math.exp(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;
  }

  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