Java 随机数只有几次

Java 随机数只有几次,java,Java,我怎样才能在x%的时间里得到一个随机数 int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); 和大于10的任何数字的y% 也许微不足道,但我找不到一个简单的解决办法 谢谢你的帮助!: 您希望随机应用x还是确定性应用x 随机变量: 如果希望x也是随机的,可以生成一个随机值: int percent = ThreadLocalRandom.current().nextInt(1, 100); if (percent &

我怎样才能在x%的时间里得到一个随机数

int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
和大于10的任何数字的y%

也许微不足道,但我找不到一个简单的解决办法 谢谢你的帮助!:

您希望随机应用x还是确定性应用x

随机变量:

如果希望x也是随机的,可以生成一个随机值:

int percent = ThreadLocalRandom.current().nextInt(1, 100);
if (percent < x) {
    // ...get the actual random value.
}

有了这个,你将得到精确的x%的真值,你调用你的方法越多。开始时,除非是100%,否则没有,但随着时间的推移,精确度会提高。

通常使用随机数生成器。其中大多数返回一个间隔[0,1]内的数字,因此您可以检查该数字是否为随机数,而不是大于10的任何数字?max小于10
// object attribute.
private Integer accumulator = 0;

// in method (should be synchronized, at least on accumulator, if you use multithreading):
accumulator += x;
boolean overflow = false;
if (accumulator >= 100) {
    overflow = true;
    accumulator %= 100; // this applies modulo 100 to accumulator.
}

if (overflow) {
    // ...get the actual random value.
}
double x=0.1;
if( Math.random() <= x ) {
   int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
}