Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- avgRepsToProbAvg is a program that takes:
- * a probability of an event occuring
- * the accuracy the model needs to stop running the indivual simulation
- * number of times that simulation is run (this produces allows for an average to be produced)
- to find the number of repetitions something would require to meet its
- average/predicted probability
- this number is then averaged after the program is run some given amount of times
- Uncomment lines 41 and 72 for more reports on the ourcomes in each run and the successRate after each "shot"
- */
- public class avgRepsToProbAvg {
- //probability of event occuring (ex. 0.4534 equals 45.34% of event occuring)
- public static double probability ;
- //accuracy of needed to stop the program
- public static double accuracy = 0.01 ; //default to accuracy within 1%
- //number of times the program is rerun and averaged with the other results
- public static int runs = 100 ; //default to 100 run-throughs of the program
- public static void printCorrectSyntax() {
- System.out.println("Correct syntax:") ;
- System.out.println("\t java RepsToAvg [avg/predicted probability]") ;
- System.out.println("\tOR: java RepsToAvg [avg/predicted probability] [accuracy of outcome to stop] [number of times program is looped to find avg]") ;
- }
- public static void main (String[] args) {
- if (args.length == 1) {
- probability = Double.parseDouble(args[0]) ;
- } else if (args.length == 3) {
- probability = Double.parseDouble(args[0]) ;
- accuracy = Double.parseDouble(args[1]) ;
- runs = Integer.parseInt(args[2]) ;
- } else {
- System.out.println("Incorrect number of arguments.") ;
- printCorrectSyntax() ;
- System.exit(0) ;
- }
- int outcomes[] = new int[runs] ;
- for (int i = 0; i < runs; i++) {
- outcomes[i] = repsToAvg() ;
- System.out.println(outcomes[i] + "\n") ;
- }
- double avgOutcome = averageOfArray(outcomes) ;
- System.out.println(avgOutcome) ;
- System.exit(1) ;
- }
- public static int repsToAvg() {
- double successRate = 0 ;
- int hit = 0 ;
- int miss = 0 ;
- int total = hit + miss ;
- double lowerProb = probability - accuracy ;
- double upperProb = probability + accuracy ;
- //System.out.println("\n" + lowerProb + " - " + upperProb) ;
- //going to run the little while loop once so we don't encounter a divide by zero issue:
- if (Math.random() < probability) {
- hit++;
- } else {
- miss++;
- }
- total = hit + miss;
- while (((successRate) < lowerProb) || ((successRate) > upperProb)) {
- //System.out.println(Math.random() < probability) ;
- if (Math.random() < probability) {
- hit++ ;
- } else {
- miss++ ;
- }
- total = hit + miss ;
- successRate = (double) hit / (double) total ;
- System.out.println(successRate) ;
- //System.out.println(hit + " " + miss + " " + total + " " + (double) hit / (double) total);
- }
- //System.out.println(successRate) ;
- return total ;
- }
- public static double averageOfArray(int[] array) {
- int sum = 0 ;
- for (int i = 0; i < array.length; i++) {
- sum += array[i] ;
- }
- return (double) sum / (double) array.length ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement