Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助提供一个算法,根据计数和百分比在Java中生成一个随机布尔值吗_Java_Algorithm_Random - Fatal编程技术网

需要帮助提供一个算法,根据计数和百分比在Java中生成一个随机布尔值吗

需要帮助提供一个算法,根据计数和百分比在Java中生成一个随机布尔值吗,java,algorithm,random,Java,Algorithm,Random,我不确定我是否给这篇文章的标题正确。如果我没有,请告诉我,我会编辑标题 我想做的是模拟“真实世界”的电池充电情况: 1st charge == 100% chance of an error (a.k.a., boolean false) 2nd charge == 90% chance of an error 3rd charge == 80% chance of an error 4th charge == 70% chance of an error 5th charge ==

我不确定我是否给这篇文章的标题正确。如果我没有,请告诉我,我会编辑标题

我想做的是模拟“真实世界”的电池充电情况:

 1st charge == 100% chance of an error (a.k.a., boolean false)
 2nd charge == 90% chance of an error
 3rd charge == 80% chance of an error
 4th charge == 70% chance of an error
 5th charge == 60% chance of an error
 6th charge == 50% chance of an error
 7th charge == 40% chance of an error
 8th charge == 30% chance of an error
 9th charge == 20% chance of an error
10th charge == 10% chance of an error
所以,我需要的是一个算法,根据这些百分比生成一个真或假,但我不知道怎么做。我知道有
Random
ThreadLocalRandom
但是没有办法为
nextBoolean()
输入任何边界或值。我想我可以这样做:

switch(charge){
    case 1:
        if(ThreadLocalRandom.nextInt(10,10) > 10) return ThreadLocalRandom.nextBoolean();
        break;
    case 2:
        if(ThreadLocalRandom.nextInt(9,10) > 9) return ThreadLocalRandom.nextBoolean();
        break;
    case 3:
        if(ThreadLocalRandom.nextInt(8,10) > 8) return ThreadLocalRandom.nextBoolean();
        break;
    case 4:
        if(ThreadLocalRandom.nextInt(7,10) > 7) return ThreadLocalRandom.nextBoolean();
        break;
    case 5:
        if(ThreadLocalRandom.nextInt(6,10) > 6) return ThreadLocalRandom.nextBoolean();
        break;
    case 6:
        if(ThreadLocalRandom.nextInt(5,10) > 5) return ThreadLocalRandom.nextBoolean();
        break;
    case 7:
        if(ThreadLocalRandom.nextInt(4,10) > 4) return ThreadLocalRandom.nextBoolean();
        break;
    case 8:
        if(ThreadLocalRandom.nextInt(3,10) > 3) return ThreadLocalRandom.nextBoolean();
        break;
    case 9:
        if(ThreadLocalRandom.nextInt(2,10) > 2) return ThreadLocalRandom.nextBoolean();
        break;
    case 10:
        if(ThreadLocalRandom.nextInt(1,10) > 1) return ThreadLocalRandom.nextBoolean();
        break;
}
正如你所看到的,我不知道我在做什么,所以我需要一些帮助


谢谢

一个简单的机制是根据电荷计数计算
0.0
1.0
之间的值,然后将其与随机
双精度
进行比较

public void test() {
    // Test each charge.
    for (int charge = 0; charge < 10; charge++) {
        // Using ints here so output is clearer - could just as easily use bool.
        List<Integer> results = new ArrayList<>();
        for (int test = 0; test <= 100; test++) {
            // Use 0 or 1 debending on random biased boolean.
            results.add(biasedBoolean(1.0 - ((float) charge / 10)) ? 0 : 1);
        }
        System.out.println("Charge " + (charge + 1) + " -> " + results);
    }
}

Random random = new Random();

private Boolean biasedBoolean(double bias) {
    return random.nextDouble() < bias;
}
公共无效测试(){
//测试每一次充电。
用于(整数电荷=0;电荷<10;电荷++){
//在这里使用INT以便输出更清晰-可以同样轻松地使用bool。
列表结果=新建ArrayList();

对于(int test=0;test1)。解释和解决方案:

您需要的是:

  • 生成一个介于0和10之间的随机数(使用
    random
    class)
  • 根据
    费用的值
    ,找到随机数的接受范围:
    • 示例:如果费用是
      4
      ,要获得错误,随机数必须是
      [0;7]
      (范围的70%),因此
      返回随机数>=7;

2.较短的解决方案

这可以简化为:

private static boolean isError(int charge) {
    return new Random().nextInt(10) >= (11 - charge);
}

3.测试和演示

使用此
main
,您可以测试该方法的有效性,它测试
nbTest
次,每次
收费
,并查看出现错误的次数

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("##.##%");
    double nbError, nbTest = 100000;
    for (int charge = 1; charge < 11; charge++) {
        nbError = 0;
        for (int j = 0; j < nbTest; j++) {
            nbError += (isError(charge) ? 0 : 1);
        }
        System.out.println(charge + " -> " + df.format(nbError / nbTest));
    }
}


1 -> 100   %   ~100%
2 ->  90,06%   ~ 90%
3 ->  80,31%   ~ 80%
4 ->  69,97%   ~ 70%
5 ->  59,92%   ~ 60%
6 ->  49,9 %   ~ 50%
7 ->  39,9 %   ~ 40%
8 ->  30,08%   ~ 30%
9 ->  19,84%   ~ 20%
10 -> 10,18%   ~ 10%
publicstaticvoidmain(字符串[]args){
DecimalFormat df=新的DecimalFormat(“##.##%”);
双nbError,nbTest=100000;
用于(整数电荷=1;电荷<11;电荷++){
n误差=0;
对于(int j=0;j”+df.格式(nbError/nbTest));
}
}
1 -> 100   %   ~100%
2 ->  90,06%   ~ 90%
3 ->  80,31%   ~ 80%
4 ->  69,97%   ~ 70%
5 ->  59,92%   ~ 60%
6 ->  49,9 %   ~ 50%
7 ->  39,9 %   ~ 40%
8 ->  30,08%   ~ 30%
9 ->  19,84%   ~ 20%
10 -> 10,18%   ~ 10%

无论如何,
返回值总是一样的,所以没有用。整个问题对我来说没有任何意义。你生成一个随机整数,然后立即丢弃它,返回一个完全独立的随机布尔值!你不需要ThreadLocalRandom。只需生成一个随机整数,定义一个截止值,然后如果生成的数字大于截止值,则返回true。
1st charge==100%出错概率
--在对电池充电的情况下,什么是“错误”,为什么出错概率为100%(即始终存在错误)第一次?
Random
类并不是这样使用的,也就是说,为每个随机整数构造一个全新的
Random
对象。它意味着返回一个使用的对象,然后分配给一个变量多次使用。或者,您可以使用
ThreadLocalRandom.current()
返回一个预先初始化的随机对象。@azro回顾这一点,它不是
案例N:return Random=10
那么它将大于或等于100%,但如果它是

public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("##.##%");
    double nbError, nbTest = 100000;
    for (int charge = 1; charge < 11; charge++) {
        nbError = 0;
        for (int j = 0; j < nbTest; j++) {
            nbError += (isError(charge) ? 0 : 1);
        }
        System.out.println(charge + " -> " + df.format(nbError / nbTest));
    }
}


1 -> 100   %   ~100%
2 ->  90,06%   ~ 90%
3 ->  80,31%   ~ 80%
4 ->  69,97%   ~ 70%
5 ->  59,92%   ~ 60%
6 ->  49,9 %   ~ 50%
7 ->  39,9 %   ~ 40%
8 ->  30,08%   ~ 30%
9 ->  19,84%   ~ 20%
10 -> 10,18%   ~ 10%