
toxgene.core.random.ToxExponential Maven / Gradle / Ivy
/**
* Implements a random generator for exponential 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 ToxExponential implements ToxRandom{
private float min, max, B;
private double U;
private Random rand;
public ToxExponential(float min, float max, float B, long seed){
this.min = min;
this.max = max;
this.B = B;
if (max < min){
throw new CreateGeneException("invalid min and max values for "+
"ToxExponential random generator!");
}
if (B < min || B > max){
throw new CreateGeneException(" invalid mean value for "+
"ToxExponential random generator!");
}
rand = new Random(seed);
}
public long nextInt(){
return (Math.round(nextDouble()));
}
private double nextDouble(){
U = 1.0 - rand.nextDouble();
double temp = -B*Math.log(U);
if (temp < min)
return (double) min;
if (temp > max)
return (double) max;
return temp;
}
public float nextFloat(){
float result = (float) nextDouble();
//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);
}
return (Math.round((double)min + ((value-min)*nextDouble())));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy