import java.util.concurrent.ThreadLocalRandom;

/**
 * Calculates the probability of each possible sum when rolling a pair of dice. 
 * 
 * This program seems much longer than should be necessary for such a simple task. An 
 * improved version of the program will use arrays to significantly simplify the code. 
 *
 * @author Drue Coles
 */
public class DiceSums {

    public static void main(String[] args) {

        // counters for each possible sum
        int c2 = 0;
        int c3 = 0;
        int c4 = 0;
        int c5 = 0;
        int c6 = 0;
        int c7 = 0;
        int c8 = 0;
        int c9 = 0;
        int c10 = 0;
        int c11 = 0;
        int c12 = 0;

        // The calculation is performed by Monte Carlo simulation.
        final int rolls = 10_000_000;
        for (int i = 0; i < rolls; i++) {
            switch (rollDice()) {
                case 2:
                    c2++;
                    break;
                case 3:
                    c3++;
                    break;
                case 4:
                    c4++;
                    break;
                case 5:
                    c5++;
                    break;
                case 6:
                    c6++;
                    break;
                case 7:
                    c7++;
                    break;
                case 8:
                    c8++;
                    break;
                case 9:
                    c9++;
                    break;
                case 10:
                    c10++;
                    break;
                case 11:
                    c11++;
                    break;
                case 12:
                    c12++;
            }
        }
        
        // Format and display results.
        System.out.println("SUM \t PROBABILITY");        
        
        double p2 = (double) c2 / rolls * 100;
        System.out.printf("2 \t %.1f%% %n", p2);

        double p3 = (double) c3 / rolls * 100;
        System.out.printf("3 \t %.1f%% %n", p3);
        
        double p4 = (double) c4 / rolls * 100;
        System.out.printf("4 \t %.1f%% %n", p4);
        
        double p5 = (double) c5 / rolls * 100;
        System.out.printf("5 \t %.1f%% %n", p5);
        
        double p6 = (double) c6 / rolls * 100;
        System.out.printf("6 \t %.1f%% %n", p6);
        
        double p7 = (double) c7 / rolls * 100;
        System.out.printf("7 \t %.1f%% %n", p7);
        
        double p8 = (double) c8 / rolls * 100;
        System.out.printf("8 \t %.1f%% %n", p8);
        
        double p9 = (double) c9 / rolls * 100;
        System.out.printf("9 \t %.1f%% %n", p9);
        
        double p10 = (double) c10 / rolls * 100;
        System.out.printf("10 \t %.1f%% %n", p10);
        
        double p11 = (double) c11 / rolls * 100;
        System.out.printf("11 \t %.1f%% %n", p11);

        double p12 = (double) c12 / rolls * 100;
        System.out.printf("12 \t %.1f%% %n", p12);
    }

    /**
     * Rolls a pair of dice.
     *
     * @return the sum of the numbers rolled
     */
    private static int rollDice() {
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        int die1 = rand.nextInt(1, 7);
        int die2 = rand.nextInt(1, 7);
        return die1 + die2;
    }
}