gpt4 book ai didi

JAVA : generate integer numbers randomly with probabilities

转载 作者:行者123 更新时间:2023-12-01 07:22:09 26 4
gpt4 key购买 nike

如果数字范围从 (150),如果数字范围从 (50100)?

int integer = new Random().nextInt(100) + 1;

// Probabilities
..... code here ....

我该怎么做?

最佳答案

这里有一个方法getRandom(),它返回满足您指定条件的单个随机数。它实际上使用 09 之间的随机数来确定使用两个范围中的哪一个。

public int getRandom() {
Random random = new Random();
int val = random.nextInt(10);

if (val < 3) {
return random.nextInt(50) + 1; // random range 1 to 50
}
else {
return random.nextInt(51) + 50; // random range 50 to 100
}
}

关于JAVA : generate integer numbers randomly with probabilities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33359500/

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