我有一个代理类,可以执行以下操作:
public class Agent {
private Context<Object> context;
private Geography<Object> geography;
public int id;
boolean female;
public Agent(Context<Object> context, Geography<Object> geography, int id, boolean female) {
this.id = id;
this.context = context;
this.geography = geography;
this.female = female;
}
... setters getters
... do things methods
}
在上下文构建器类中,将代理添加到上下文(由纬度和经度坐标组成的地理空间)中,我想使代理的女性百分比随机化(female = true)。
for (int i = 0; i < 100; i++) {
Agent agent = new Agent(context, geography, i, false);
int id = i++;
if(id > 50) {
boolean female = true;
}
context.add(agent);
//specifies where to add the agent
Coordinate coord = new Coordinate(-79.6976, 43.4763);
Point geom = fac.createPoint(coord);
geography.move(agent, geom);
}
我相信上面的代码会将最后50位探员构造为女性。我该如何做才能随机将它们创建为女性?我更改了创建的代理数量。
最佳答案
使用您的代码,您总是可以创建一个MALE代理。
在创建Agent
实例之前,尝试评估它是否为女性:
Agent agent = null;
boolean isFemale = false;
for (int i = 0; i < 100; i++) {
int id = i++;
if(id > 50) {
isFemale = true;
}
agent = new Agent(context, geography, i, isFemale);
context.add(agent);
//specifies where to add the agent
Coordinate coord = new Coordinate(-79.6976, 43.4763);
Point geom = fac.createPoint(coord);
geography.move(agent, geom);
}
如果您希望随机,请尝试使用随机实用程序:
Random random = new Random();
agent = new Agent(context, geography, i, random.nextBoolean());
希望这可以帮助