gpt4 book ai didi

java - 选择项目大概

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:59 26 4
gpt4 key购买 nike

我目前正在尝试用java制作一个可能基于选择的系统,但它似乎工作得不太好,我当前的代码如下所示:

private ArrayList<SelectableObjekt> obs;
obs = new ArrayList<>();
obs.add(new SelectableObjekt("hello, with 3% probably", 30)); //Will be devied in the SelectableObjekt class by 10 so 30 will become 3% and 700 will become 70%
obs.add(new SelectableObjekt("hello, with 70% probably", 700));
obs.add(new SelectableObjekt("hello, with 90% probably", 900));
obs.sort(new Comparator<SelectableObjekt>() {
@Override
public int compare(SelectableObjekt o1, SelectableObjekt o2) {

return Double.compare(o1.getChance(), o2.getChance());
}
});

并选择对象:

private SelectableObjekt selectRandom() {
double perc = ((int)ThreadLocalRandom.current().nextInt(1001))/10D;
for (int i = 0; i < obs.size(); i++) {
SelectableObjekt obj = obs.get(i);
if(obj.getChance() > perc) {
return obj;
}
}
return obs.get(chestDrops.size()-1);
}

总之,SelectableObjekt 内部包含一个机会整数,可以是 0.1-100。 100 意味着 100%,所以 32.3 意味着 32.3%,但是选择不能正常工作,我得到 70% 的频率比 90% 多,我无法理解为什么会发生这种情况。

最佳答案

代码的编写方式并不罕见。您有三个范围。

0 - 3% - the first option
3 - 70% - the second option.
70 - 90% - the third option.
90 - 100% - also the third option.

这意味着“70%”选项应占约 67% 的时间,“90%”选项应占约 30% 的时间。

关于java - 选择项目大概,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52468774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com