Java:性别控制
Java: Gender Control
所以在我的模拟器中,我试图更准确地控制生物在创建时的性别几率。最初我只有 50% 的机会使用 RND,但我意识到这会在以后引起问题。因此,我在考虑每次制作一个生物并决定性别时,我可以 alter/adjust 基于当前比率的每个性别的百分比机会,例如当当前人口为 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 中。每个生物类型和性别都是一个关键,例如Fish (ctype) + Gender - 然后存储一个值,由底部的 replace 命令更改。
以这种方式实现它似乎非常……不整洁,所以希望其他人有更好的建议……?
谢谢。
我会尝试这样的事情...
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 进行比较,以确定它是否是男性(否则是女性)。
所以剩下的唯一问题是如何最好地应用我从中得到的 chance/percent 来确定选择哪种性别。目前我唯一能想到的是:
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.
如果有更好的方法,有什么建议...?
所以在我的模拟器中,我试图更准确地控制生物在创建时的性别几率。最初我只有 50% 的机会使用 RND,但我意识到这会在以后引起问题。因此,我在考虑每次制作一个生物并决定性别时,我可以 alter/adjust 基于当前比率的每个性别的百分比机会,例如当当前人口为 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 中。每个生物类型和性别都是一个关键,例如Fish (ctype) + Gender - 然后存储一个值,由底部的 replace 命令更改。
以这种方式实现它似乎非常……不整洁,所以希望其他人有更好的建议……?
谢谢。
我会尝试这样的事情...
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 进行比较,以确定它是否是男性(否则是女性)。
所以剩下的唯一问题是如何最好地应用我从中得到的 chance/percent 来确定选择哪种性别。目前我唯一能想到的是:
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.
如果有更好的方法,有什么建议...?