Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this will run a simulations to show Charlie some shit and stuff
- // assume n[0] is you
- public class sim {
- static int size;//Holds the number of people
- static int[] n;// holds the number of wins for each person
- //This is where the program enters
- public static void main(String[] args) {
- // Set this to the number of iterations you want(higher is more accurate)
- // Attempts to take input from the command line arguments
- int iterations = 10000;
- if (args.length > 0){
- try{
- iterations = Integer.parseInt(args[0]);
- } catch (NumberFormatException e) {
- System.out.println("Argument is not an integer, reverting to default");
- }
- }
- //Some init code
- size = (int)Math.pow(10,8);// Sets the number of people
- System.out.println("Starting, using "+size+" people and "+iterations+" iterations");
- n = new int[size];// Declars array to count number of wins
- // some variables for logic
- int win = 0;
- int lose = 0;
- boolean exit;
- int which = 0;
- // This makes sure you go through like ten thousand iterations
- for (int Q = 0; Q< iterations; Q++){
- // Prints some info into console
- if (Q % 200 == 0) System.out.println("Run #"+Q+" , "+win+" wins so far");
- // resets all the values for logic
- for (int i = 0; i < size; i++){
- n[i] = 0;
- }
- exit = false;
- // Keeps going until you lose or win
- while (!(exit)){
- which =(int)(Math.random() * (size)); // Selects who won
- n[which]++; // adds to their score
- //if which = 0 then you win, else someone else won
- // if you win, or someone wins three times, exits
- if ((which ==0)||(n[which] >= 3)){
- exit = true;
- }
- }
- // Checks to see if you won or lost
- if (which == 0) {
- win ++;
- } else lose ++;
- }
- //calculates the answer
- //int answer = (win/iterations)*100;
- // This shows you the answer
- System.out.println("You won "+win+" times out of "+iterations+" runs");
- //System.out.println("That means you had a "+answer+"% chance of winning");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment