I am doing a code to generate mathematicals constants. The constants have a firstValue. They have a method generate to get a value. The value generated must be randomly chosen in the fluctuation space using the granularity parameter. It means that a random number is multiply by the granularity parameter so that the random value generated stay in the fluctuation space. The code works, its generate random value in the space. But some constants generated are of the form 0.90000004 when they have to be 0.9. The class aleatoire(min, max) generate a number between min and max included with its method generate. I tested the class aleatoire, its work well giving just int.
public class Constante {
private float firstValue;
private float fluctuation;
private float granularite;
private float value;
private int stepNumber;
public Constante(float firstValue, float fluctuation, float granularite) {
this.firstValue = firstValue;
this.fluctuation = fluctuation;
this.granularite=granularite;
this.value=firstValue;
this.stepNumber= (int)(((fluctuation/granularite)));
}
public void generate(){
Aleatoire random=new Aleatoire(0,stepNumber);
if (new Aleatoire(1,2).generate()%2!=0) {
value = firstValue + (random.generate() * granularite);
}
else{
value = firstValue - (random.generate() * granularite);
}
}
public float getValue(){
return value;
}
}