VincentOostelbos

Lives left simulation

Jul 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. package livesPredictor;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. public class LivesPredictor {
  7.     static Random rnd;
  8.     static double winProb = 0.3175;
  9.    
  10.     public static void main(String[] args) {
  11.         rnd = new Random();
  12.         ArrayList<Integer> lives = new ArrayList<Integer>();
  13.         double p;
  14.        
  15.         for(int i=0; i<4; i++) lives.add(0); // 0 lives left, 1 life left, 2 lives left, 3 lives left; each start at 0
  16.        
  17.         for(int t=0; t<1000000; t++) {
  18.             p = rnd.nextDouble();
  19.             int l = 3;
  20.            
  21.             while(p > winProb && l > 0) {
  22.                 l--;
  23.                 p = rnd.nextDouble();
  24.             }
  25.            
  26.             int count = lives.get(l);
  27.             lives.set(l, count+1); // Increment lives left at end
  28.         }
  29.        
  30.         for(int i=0; i<4; i++) {
  31.             System.out.println("Times with " + i + " lives left: " + lives.get(i) + ".");
  32.         }
  33.     }
  34. }
Add Comment
Please, Sign In to add comment