我正在寻找一种可以随机生成角色统计数据的方法(如技能、攻击、防御……)。 假设我的统计数据来自
1 - 100
现在我想要
之间的统计数据1 - 30 a probability of 30%
31 - 50 a probability of 45%
51 - 75 a probability of 20%
76 - 100 a probability of 5%
我知道我可能可以使用 Random
类或 Math.random()
但不确定如何使用。
提前致谢!
最佳答案
一个选项是生成 0 到 100 范围内的随机数,然后使用一系列 if-else
语句来确定为您的角色生成哪些统计数据:
public void printRandomStats() {
Random random = new Random();
int next = random.nextInt(101);
if (next <= 30) {
// this will happen 30% of the time
System.out.println("Generating stats from 1-30");
} else if (next <= 75) {
// this will happen 45% of the time
System.out.println("Generating stats from 31-75");
} else if (next <= 95) {
// this will happen 20% of the time
System.out.println("Generating stats from 76-95");
} else {
// this will happen 5% of the time
System.out.println("Generating stats from 96-100");
}
return;
}
关于java - 为角色生成随机统计数据,其中高统计数据出现的可能性低于低统计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334911/