toxgene.core.random.ToxUserDefined Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ToxGene Show documentation
Show all versions of ToxGene Show documentation
Modified ToXGene for the iBench project.
The newest version!
/**
* Implements a Gene for enumeration simpletypes.
*
* @version 0.1
* @author Denilson Barbosa
*/
package toxgene.core.random;
import java.util.Vector;
import java.util.Random;
import toxgene.core.ToXgeneErrorException;
import toxgene.core.genes.CreateGeneException;
import toxgene.util.FloatObjectPair;
public class ToxUserDefined implements ToxRandom{
private Vector options;
private float min, max, sum;
private Random rand;
public ToxUserDefined(float min, float max, long seed){
this.min = min;
this.max = max;
if (max < min){
throw new CreateGeneException("invalid min and max values for "+
"ToxGaussian random generator!");
}
options = new Vector();
sum = 0;
rand = new Random(seed);
}
public long nextInt(){
return (Math.round((double)nextFloat()));
}
public float nextFloat(){
float pick = 100*rand.nextFloat();
int size = options.size();
if (size ==0)
return ((Float)((FloatObjectPair)options.elementAt(0)).value()).floatValue();
for (int i=0;i max){
throw new CreateGeneException("invalid distribution value");
}
sum += percent;
options.add(new FloatObjectPair(sum, new Float(value)));
}
public float minValue(){
return min;
}
public float maxValue(){
return max;
}
public long nextInt(float value){
if (value < min){
throw new ToXgeneErrorException("cannot update distribution max value: "+
value+" is smaller than minimum value: "+
min);
}
if (value == max)
return (Math.round(nextFloat()));
return (Math.round((double)min + ((value-min) * rand.nextFloat())));
}
}