| 1 |
package fun; |
| 2 |
|
| 3 |
import java.util.Random; |
| 4 |
|
| 5 |
import shared.Parse; |
| 6 |
import shared.Shared; |
| 7 |
import shared.Tools; |
| 8 |
|
| 9 |
public class Chance { |
| 10 |
|
| 11 |
|
| 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: "+Tools.format("%.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 |
} |