Semester 1 Final

Code

    /// Name: Koosha Kimelman
    /// Period: 7
    /// Program Name: Semester 1 Final
    /// File Name: Sem1Final.java
    /// Date Completed: 1/21/16
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class Sem1Final {
    
        public static void main(String[] args) throws Exception { // Just random little pauses so it's not all of a sudden.
        
            Scanner k = new Scanner(System.in);
            Random r = new Random();
            
            int f, n, x, h, t; // flips, number (of flips done), x just for the random number, heads, tails
            
            h = 0;
            t = 0;
            
            do {
                Thread.sleep(300);
                System.out.println("How many flips do you want?");
                f = k.nextInt();
            } while (!(f >= 1 && f <= 2100000000)); // To keep it in the range.
            
            Thread.sleep(300);
            
            System.out.println("Okay, I will flip a coin " + f + " time(s).\n");
            
            Thread.sleep(500);
            
            for (n = 0; n < f; n++) { // I'm on functions at the time of typing this so I know the for loops already.
                x = r.nextInt(2);
                if (x == 0) {
                    h++;
                    System.out.println("Heads");
                }
                else if (x == 1) {
                    t++;
                    System.out.println("Tails");
                }
                if (f <= 50)
                    Thread.sleep(100);
                else if (f > 50 && f <= 500) // Just so if you put in a larger number, it prints them faster, whereas a small number will print them slower.
                    Thread.sleep(10);
                else if (f > 500 && f <= 10000) // If you go over 10000 there is no forced pause so it will go just that little bit faster quickly.
                    Thread.sleep(1);
            }
            
            Thread.sleep(750);
            
            System.out.println("\nYou got a total of " + h + " heads, and " + t + " tails.");
            
            Thread.sleep(400);
            
            System.out.println("That is " + (((double)h/f)*100) + "% heads, and " + (((double)t/f)*100) + "% tails.");
            
            Thread.sleep(500);
            
            System.out.println("\nBye now.");
        }
    }
    // The best way to CONSISTENTLY get as close to 50/50 as possible with these coin flips, is the choose the absolute largest even number that will not overflow, e.g. around 2.1 billion. You could get exactly 50% with 
    any even number, but not consistently. You may get one heads and one tails with two flips, but you may also get two heads or two tails. This small deviation from the center means a massive amount in smaller numbers, 
    but the same deviation with larger numbers has much less effect. For example: six flips, 4 heads and 2 tails, both one away from 50%, is 67/33 (rounded), which is a difference of 34% (rounded), whereas if you did 
    200 flips, and got 102 heads and 98 tails, each of them TWO away from 50%, it is 51/49, much closer to 50 than the 67/33 which even had a smaller difference between the two outcomes. All even numbers have a chance of 
    getting 50/50, and the larger the number, the less likely that is to happen, but the average for running it with a higher number multiple times will almost always (just the tiniest little sliver of a chance to get 
    for example like 80% heads and 20% tails, or even all heads or all tails) get much closer to 50% than if you ran the program the same number of times with a smaller number.
    

Picture of the output

Semester 1 Final