所以在我的模拟器中,我试图更准确地控制创造性别的生物的机会。最初我只有50%的几率使用RND,但我意识到这会在以后引起问题。因此,我想到每次制作一个生物并决定性别时我可以根据当前比例改变/调整每个性别的%几率,例如:当前人口中男性为70%,女性为30%。所以可以让下一个生物有70%的机会成为女性并且这样做。我的问题是,我正在努力实现这一点,下面是一些信息:
public void setGender2() {
int fper = gcount.get(ctype+Gender.F); int mper = gcount.get(ctype+Gender.M);
int tcc = fper + fper;
int gmf = rNum(0,100); //Calls the random number method.
if (fper == mper) { //When first used the total will be 0 so do this.
gchance = 50;
if (gmf <= gchance) g = Gender.F; //If the random number is less than the calculated gchance %.
else g = Gender.M;
}
else {
gchance = (int)(100-(((double)gcount.get(ctype+g)/(double)tcc)*100)); //Calculates the % for a gender.
if (fper < mper) { //When there is less females...
if (gmf <= gchance) g = Gender.F;
else if (gmf > gchance) g = Gender.M;
}
else if (mper < fper) { //When there is less males...
if (gmf <= gchance) g = Gender.M;
else if (gmf > gchance) g = Gender.F;
}
}
gcount.replace(ctype+g, gcount.get(ctype+g)+1); //update the count for this creature type + gender.
}
性别信息存储在名为gcount的HashMap中。每种生物类型&amp;性别是关键,例如Fish(ctype)+ Gender - 然后是一个与它一起存储的值,由底部的replace命令改变。
事情是以这种方式实施它看起来非常......不整洁,所以希望别人有更好的建议......?
感谢。
答案 0 :(得分:0)
我会尝试这样的事情......
int males = 2; // <- your map value here
int females = 1; // <- your map value here
int total = males + females;
double chanceMale = .5;
if (total > 0) {
chanceMale = females / (double)total;
}
然后简单地将你的随机数与chanceMale * 100进行比较,以确定它是否是男性(女性)。
答案 1 :(得分:0)
因此,唯一的问题是如何最好地应用我从中获得的机会/百分比来确定选择哪个性别。目前我唯一能想到的是:
int rgen = rNum(0,100) //(random number between 1 and 100).
if (chanceMale > chanceFemale) {
if (rgen < chanceMale) g = Gender.M
else g = Gender.F
}
else if (chanceFemale > chanceMale) {
if (rgen < chanceFemale) g = Gender.F
else g = Gender.M
}
//Only issue is when rgen is equal to chanceMale/Female.
如果有一个更好的方法可以提出任何建议......?