Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Created January 13, 2019 12:12
Show Gist options
  • Save d3ep4k/4dccebbcd5a3e9aba577e9e9f8af7df9 to your computer and use it in GitHub Desktop.
Save d3ep4k/4dccebbcd5a3e9aba577e9e9f8af7df9 to your computer and use it in GitHub Desktop.
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