comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fun/Chance.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package fun;
2
3 import java.util.Locale;
4 import java.util.Random;
5
6 import shared.Parse;
7 import shared.Shared;
8
9 public class Chance {
10
11 //Probability of something with a chance of X happening at least Y times in Z chances
12 public static void main(String[] args){
13
14 int draws;
15 int minSuccess;
16 float prob;
17 long rounds;
18 try {
19 draws = Parse.parseIntKMG(args[0]);
20 minSuccess = Parse.parseIntKMG(args[1]);
21 prob = Float.parseFloat(args[2]);
22 rounds = Parse.parseKMG(args[3]);
23 } catch (Exception e) {
24 System.err.println("Chance (int)draws (int)minSuccess (float)prob (int)rounds");
25 System.exit(1);
26 throw new RuntimeException();
27 }
28
29 Random randy=Shared.threadLocalRandom();
30
31 long passes=0;
32 for(long i=0; i<rounds; i++){
33 int pass=runOneRound(randy, draws, minSuccess, prob);
34 passes+=pass;
35 }
36
37 double odds=passes*1.0/rounds;
38 System.err.println("Probability: "+String.format(Locale.ROOT, "%.6f%%", 100*odds));
39 }
40
41 private static int runOneRound(Random randy, int draws, int minSuccess, float prob) {
42 int success=0;
43 for(int i=0; i<draws && success<minSuccess; i++){
44 if(randy.nextFloat()<=prob){success++;}
45 }
46 return (success>=minSuccess ? 1 : 0);
47 }
48
49 }