jeudi 24 mai 2018

Need help coming up with an algorithm to generate a random boolean in Java depending on count and percentages

I'm not sure if I even titled this post correctly. If I didn't, let me know and I'll edit the title.

What I am trying to do is simulate a "real-world" situation for charging batteries:

 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

So, what I need is an algorithm to generate a true or false depending on these percentages, but I have no idea how to do it. I know there is Random and ThreadLocalRandom but there is no way to input any bounds or values for nextBoolean(). I figured I could do something like this:

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;
}

As you can see, I have no idea what I am doing, so I need some help.

Thanks!




Aucun commentaire:

Enregistrer un commentaire