I have 10 generic agents:
public class Agent {
private Context<Object> context;
private Geography<Object> geography;
public int id;
public boolean isFemale;
public double random;
public Agent(Context<Object> context, Geography<Object> geography, boolean isFemale, double random) {
this.context = context;
this.geography = geography;
this.isFemale = isFemale;
this.random = random;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isFemale() {
return isFemale;
}
public void setFemale(boolean isFemale) {
this.isFemale = isFemale;
}
public double getRandom() {
return random;
}
public void setRandom(double random) {
this.random = random;
}
public void methods {
... does things
}
The agents are created in a geography context (latitude and longitude). I am trying to construct my agents as randomly male or female. The code that I am using in the context builder to create the agents is below:
Agent agent = null;
boolean isFemale = false;
for (int i = 0; i < 10; i++) {
double random = RandomHelper.nextDoubleFromTo(0, 1);
if (random > 0.33){
isFemale = true;
}
agent = new Agent(context, geography, isFemale, random);
context.add(agent);
Coordinate coord = new Coordinate(-79.6976, 43.4763);
Point geom = fac.createPoint(coord);
geography.move(agent, geom);
}
When I test the code, I am getting that they are all female. What am I doing wrong? If anything I would think that they would all be male since booleans are by default false.
Your boolean
not updating for every iteration once it becomes isFemale = true
it remains true for other values.You can add else part to set it false
.
for (int i = 0; i < 10; i++) {
isFemale = false;//Set it here
double random = RandomHelper.nextDoubleFromTo(0, 1);
if (random > 0.33){
isFemale = true;
//...
OR
if (random > 0.33){
isFemale = true;
} else {
isFemale = false;
}
OR
agent = new Agent(context, geography, random > 0.33, random);