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