对于多个item的随机概率获取 randparam
程序员文章站
2023-12-25 20:35:33
...
/**
* 按照百分比返回值
*
* @param item
* 物品ID数组
* @param ratio
* 按照百分比整数初始化数组,所有数值和等于100×N
* @param rand
* 随机数
* @return
*/
public static int itemRatio(int[] item, int[] ratio, int rand) {
if (item == null || ratio == null || item.length == 0
|| ratio.length == 0) {
return -1;
}
int sum = 0;
for (int i = 0; i < ratio.length; i++) {
if (rand >= sum && rand < sum + ratio[i]) {
return item[i];
}
sum += ratio[i];
}
return 1;
}
* 按照百分比返回值
*
* @param item
* 物品ID数组
* @param ratio
* 按照百分比整数初始化数组,所有数值和等于100×N
* @param rand
* 随机数
* @return
*/
public static int itemRatio(int[] item, int[] ratio, int rand) {
if (item == null || ratio == null || item.length == 0
|| ratio.length == 0) {
return -1;
}
int sum = 0;
for (int i = 0; i < ratio.length; i++) {
if (rand >= sum && rand < sum + ratio[i]) {
return item[i];
}
sum += ratio[i];
}
return 1;
}