Gibstick__

Untitled

Jul 27th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. // this will run a simulations to show Charlie some shit and stuff
  2. // assume n[0] is you
  3. public class sim {
  4.     static int size;//Holds the number of people
  5.     static int[] n;// holds the number of wins for each person
  6.    
  7.     //This is where the program enters
  8.     public static void main(String[] args) {
  9.        
  10.         // Set this to the number of iterations you want(higher is more accurate)
  11.         // Attempts to take input from the command line arguments
  12.         int iterations = 10000;
  13.         if (args.length > 0){
  14.             try{
  15.                 iterations = Integer.parseInt(args[0]);
  16.             } catch (NumberFormatException e) {
  17.                 System.out.println("Argument is not an integer, reverting to default");
  18.             }
  19.         }
  20.        
  21.         //Some init code
  22.         size = (int)Math.pow(10,8);// Sets the number of people
  23.         System.out.println("Starting, using "+size+" people and "+iterations+" iterations");
  24.         n = new int[size];// Declars array to count number of wins
  25.        
  26.         // some variables for logic
  27.         int win = 0;
  28.         int lose = 0;
  29.         boolean exit;
  30.         int which = 0;
  31.        
  32.         // This makes sure you go through like ten thousand iterations
  33.         for (int Q = 0; Q< iterations; Q++){
  34.        
  35.             // Prints some info into console
  36.             if (Q % 200 == 0) System.out.println("Run #"+Q+" , "+win+" wins so far");
  37.            
  38.             // resets all the values for logic
  39.             for (int i = 0; i < size; i++){
  40.                 n[i] = 0;
  41.             }
  42.             exit = false;
  43.            
  44.             // Keeps going until you lose or win
  45.             while (!(exit)){
  46.            
  47.                 which =(int)(Math.random() * (size)); // Selects who won
  48.                 n[which]++; // adds to their score
  49.                
  50.                 //if which = 0 then you win, else someone else won
  51.                
  52.                 // if you win, or someone wins three times, exits
  53.                 if ((which ==0)||(n[which] >= 3)){
  54.                     exit = true;
  55.                 }
  56.             }
  57.            
  58.             // Checks to see if you won or lost
  59.             if (which == 0) {
  60.                 win ++;
  61.             } else lose ++;
  62.         }
  63.        
  64.         //calculates the answer
  65.         //int answer = (win/iterations)*100;
  66.        
  67.         // This shows you the answer
  68.         System.out.println("You won "+win+" times out of "+iterations+" runs");
  69.         //System.out.println("That means you had a "+answer+"% chance of winning");
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment