Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Last active April 21, 2017 10:39
Show Gist options
  • Save d3ep4k/b6aceeb869662a7027792969b871b557 to your computer and use it in GitHub Desktop.
Save d3ep4k/b6aceeb869662a7027792969b871b557 to your computer and use it in GitHub Desktop.
To know that your chances improve if you keep trying. :)
import java.util.concurrent.ThreadLocalRandom;
public class Probability {
public static void main(String[] args){
if(args.length != 2){
System.out.println("java Probability <Samples> <Probability percentage>")
System.exit(0);
}
int test = Integer.parseInt(args[1]);
if(test > 100){
System.out.println("Probability Percentage cannot be greater than 100%")
System.exit(0);
}
int samples = Integer.parseInt(args[0]);
int min = 1;
int max = 100;
int counter = 0;
for(int i=0; i<=samples; i++){
int random = ThreadLocalRandom.current().nextInt(min, max + 1);
if((random>0)&&(random<=test)){
counter++;
}
}
double mean = 1.0*counter/samples;
// System.out.println("Mean: "+mean);
System.out.println("Mean deviation: "+Math.abs((1.0*test/samples)-mean));
System.out.println("Mean percentage: "+100*mean+"%");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment