0

I have to write a code that rolls 10 dice "N" number of times. I have to make a histogram of the results (10 through 60) using "*" to show the bell curve that should appear. How can I combine all the different ArrayLists i have to make my code look much cleaner? Is there a simpler way to do this all together?

I am getting the correct results; I just need a cleaner or more efficient way to show my code.

public class TenDice {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of trials: ");
        int roll = scan.nextInt();


        ArrayList<String> results10 = new ArrayList<>();
        ArrayList<String> results11 = new ArrayList<>();
        ArrayList<String> results12 = new ArrayList<>();
        ArrayList<String> results13 = new ArrayList<>();
        ArrayList<String> results14 = new ArrayList<>();
        ArrayList<String> results15 = new ArrayList<>();
        ArrayList<String> results16 = new ArrayList<>();
        ArrayList<String> results17 = new ArrayList<>();
        ArrayList<String> results18 = new ArrayList<>();
        ArrayList<String> results19 = new ArrayList<>();
        ArrayList<String> results20 = new ArrayList<>();
        ArrayList<String> results21 = new ArrayList<>();
        ArrayList<String> results22 = new ArrayList<>();
        ArrayList<String> results23 = new ArrayList<>();
        ArrayList<String> results24 = new ArrayList<>();
        ArrayList<String> results25 = new ArrayList<>();
        ArrayList<String> results26 = new ArrayList<>();
        ArrayList<String> results27 = new ArrayList<>();
        ArrayList<String> results28 = new ArrayList<>();
        ArrayList<String> results29 = new ArrayList<>();
        ArrayList<String> results30 = new ArrayList<>();
        ArrayList<String> results31 = new ArrayList<>();
        ArrayList<String> results32 = new ArrayList<>();
        ArrayList<String> results33 = new ArrayList<>();
        ArrayList<String> results34 = new ArrayList<>();
        ArrayList<String> results35 = new ArrayList<>();
        ArrayList<String> results36 = new ArrayList<>();
        ArrayList<String> results37 = new ArrayList<>();
        ArrayList<String> results38 = new ArrayList<>();
        ArrayList<String> results39 = new ArrayList<>();
        ArrayList<String> results40 = new ArrayList<>();
        ArrayList<String> results41 = new ArrayList<>();
        ArrayList<String> results42 = new ArrayList<>();
        ArrayList<String> results43 = new ArrayList<>();
        ArrayList<String> results44 = new ArrayList<>();
        ArrayList<String> results45 = new ArrayList<>();
        ArrayList<String> results46 = new ArrayList<>();
        ArrayList<String> results47 = new ArrayList<>();
        ArrayList<String> results48 = new ArrayList<>();
        ArrayList<String> results49 = new ArrayList<>();
        ArrayList<String> results50 = new ArrayList<>();
        ArrayList<String> results51 = new ArrayList<>();
        ArrayList<String> results52 = new ArrayList<>();
        ArrayList<String> results53 = new ArrayList<>();
        ArrayList<String> results54 = new ArrayList<>();
        ArrayList<String> results55 = new ArrayList<>();
        ArrayList<String> results56 = new ArrayList<>();
        ArrayList<String> results57 = new ArrayList<>();
        ArrayList<String> results58 = new ArrayList<>();
        ArrayList<String> results59 = new ArrayList<>();
        ArrayList<String> results60 = new ArrayList<>();

        for (int i = 0; i < roll; i++) {

            double dice1 = Math.random();
            dice1 = (int) (dice1 * 6);

            double dice2 = Math.random();
            dice2 = (int) (dice2 * 6);

            double dice3 = Math.random();
            dice3 = (int) (dice3 * 6);

            double dice4 = Math.random();
            dice4 = (int) (dice4 * 6);

            double dice5 = Math.random();
            dice5 = (int) (dice5 * 6);

            double dice6 = Math.random();
            dice6 = (int) (dice6 * 6);

            double dice7 = Math.random();
            dice7 = (int) (dice7 * 6);

            double dice8 = Math.random();
            dice8 = (int) (dice8 * 6);

            double dice9 = Math.random();
            dice9 = (int) (dice9 * 6);

            double dice10 = Math.random();
            dice10 = (int) (dice10 * 6);

            double total = dice1 + dice2 + dice3 + dice4 + dice5 +
                    dice6 + dice7 + dice8 + dice9 + dice10;

            if (total == 10) {
                results10.add("*");
            }
            if (total == 11) {
                results11.add("*");
            }
            if (total == 12) {
                results12.add("*");
            }
            if (total == 13) {
                results13.add("*");
            }
            if (total == 14) {
                results14.add("*");
            }
            if (total == 15) {
                results15.add("*");
            }
            if (total == 16) {
                results16.add("*");
            }
            if (total == 17) {
                results17.add("*");
            }
            if (total == 18) {
                results18.add("*");
            }
            if (total == 19) {
                results19.add("*");
            }
            if (total == 20) {
                results20.add("*");
            }
            if (total == 21) {
                results21.add("*");
            }
            if (total == 22) {
                results22.add("*");
            }
            if (total == 23) {
                results23.add("*");
            }
            if (total == 24) {
                results24.add("*");
            }
            if (total == 25) {
                results25.add("*");
            }
            if (total == 26) {
                results26.add("*");
            }
            if (total == 27) {
                results27.add("*");
            }
            if (total == 28) {
                results28.add("*");
            }
            if (total == 29) {
                results29.add("*");
            }
            if (total == 30) {
                results30.add("*");
            }
            if (total == 31) {
                results31.add("*");
            }
            if (total == 32) {
                results32.add("*");
            }
            if (total == 33) {
                results33.add("*");
            }
            if (total == 34) {
                results34.add("*");
            }
            if (total == 35) {
                results35.add("*");
            }
            if (total == 36) {
                results36.add("*");
            }
            if (total == 37) {
                results37.add("*");
            }
            if (total == 38) {
                results38.add("*");
            }
            if (total == 39) {
                results39.add("*");
            }
            if (total == 40) {
                results40.add("*");
            }
            if (total == 41) {
                results41.add("*");
            }
            if (total == 42) {
                results42.add("*");
            }
            if (total == 43) {
                results43.add("*");
            }
            if (total == 44) {
                results44.add("*");
            }
            if (total == 45) {
                results45.add("*");
            }
            if (total == 46) {
                results46.add("*");
            }
            if (total == 47) {
                results47.add("*");
            }
            if (total == 48) {
                results48.add("*");
            }
            if (total == 49) {
                results49.add("*");
            }
            if (total == 50) {
                results50.add("*");
            }
            if (total == 51) {
                results51.add("*");
            }
            if (total == 52) {
                results52.add("*");
            }
            if (total == 53) {
                results53.add("*");
            }
            if (total == 54) {
                results54.add("*");
            }
            if (total == 55) {
                results55.add("*");
            }
            if (total == 56) {
                results56.add("*");
            }
            if (total == 57) {
                results57.add("*");
            }
            if (total == 58) {
                results58.add("*");
            }
            if (total == 59) {
                results59.add("*");
            }
            if (total == 60) {
                results60.add("*");
            }


        }
        System.out.println("10: " + results10 + "\n" + "11: " + results11 + "\n" + "12: " + results12 + "\n" + "13: " + results13 + "\n" +
                "14: " + results14 + "\n" + "15: " + results15 + "\n" + "16: " + results16 + "\n" + "17: " + results17 + "\n" +
                "18: " + results18 + "\n" + "19: " + results19 + "\n" + "20: " + results20 + "\n" + "21: " + results21 + "\n" +
                "22: " + results22 + "\n" + "23: " + results23 + "\n" + "24: " + results24 + "\n" + "25: " + results25 + "\n" +
                "26: " + results26 + "\n" + "27: " + results27 + "\n" + "28: " + results28 + "\n" + "29: " + results29 + "\n" +
                "30: " + results30 + "\n" + "31: " + results31 + "\n" + "32: " + results32 + "\n" + "33: " + results33 + "\n" +
                "34: " + results34 + "\n" + "35: " + results35 + "\n" + "36: " + results36 + "\n" + "37: " + results37 + "\n" +
                "38: " + results38 + "\n" + "39: " + results39 + "\n" + "40: " + results40 + "\n" + "41: " + results41 + "\n" +
                "42: " + results42 + "\n" + "43: " + results43 + "\n" + "44: " + results44 + "\n" + "45: " + results45 + "\n" +
                "46: " + results46 + "\n" + "47: " + results47 + "\n" + "48: " + results48 + "\n" + "49: " + results49 + "\n" +
                "50: " + results50 + "\n" + "51: " + results51 + "\n" + "52: " + results52 + "\n" + "53: " + results53 + "\n" +
                "54: " + results54 + "\n" + "55: " + results55 + "\n" + "56: " + results56 + "\n" + "57: " + results57 + "\n" +
                "58: " + results58 + "\n" + "59: " + results59 + "\n" + "60: " + results60);
    }
}
5
  • 5
    Whenever you start using variable names like var1,var2,var3 to do the same thing, you should probably use an array or a List instead. Commented Feb 13, 2016 at 20:26
  • 1
    To add to what @RealSkeptic said, if you're using the copy+paste functionality more than once or twice - you're doing something wrong. Commented Feb 13, 2016 at 20:28
  • 5
    do this if they pay your by the number of lines of code:) Commented Feb 13, 2016 at 20:28
  • @RealSkeptic if I need to modify them after i make them (putting a "*" into the list every time the sum of the dice equals that number) how can i simplify my code? Commented Feb 13, 2016 at 20:31
  • @bayou.io I wish i was being paid for this at all haha Commented Feb 13, 2016 at 21:11

3 Answers 3

1

It's time to use the Stream API in Java 8.

To get the sum of 10 dice from 1 to 6 you can do

Random rnd = new Random();

long sum = IntStream.range(0, 10).map(i -> rand.nextInt(6) + 1).sum();

You can make many rolls and get the distribution of these.

Map<Long, Long> rollCount = 
       IntStream.range(0, roll)
                .map(r -> rnd.ints(10, 1, 7).sum())
                .collect(Collectors.groupingBy(s -> s, Collectors.counting()));

So this will roll 10 dice, rolls times and get a count of each of the results.

You can print the distribution with

rollCount.entrySet().stream()
         .sorted(e -> e.getKey())
         .forEach(e -> System.out.println(e + ": " + 
               IntStream.range(0, 10).mapToObj(i -> "*").collect(Collectors.joining("")));

Lets say we can't use Java 8. We can use loops and a much simpler code.

 List<String> countList = new ArrayList<>();
 for(int i = 0; i <= 6 * 10; i++)
     countList.add(""); // start with nothing.
 Random rnd = new Random();
 for (int r = 0; r < roll; r++) {
     int sum = 0;
     for (int i = 0; i < 10; i++)
         sum += rnd.randInt(6) + 1;
     String stars = countList.get(sum);
     stars += "*";
     countList.set(sum, stars);
 }
Sign up to request clarification or add additional context in comments.

21 Comments

I am in a Freshman-level Java course and as much as I'd like to use Stream API, my professor just wants us doing it his way for now, which only involves arrays and loops so far :/
@MichaelG -- even java is too "high level" as a teaching tool. they should really start from pointers...
@bayou.io I wish I had a say in that
With the Stream API, int sum = new Random().ints(10, 0, 7).sum();
I think you really want i < 6 * 10; to go up to 60 -> i <= 6 * 10; Since the sum of 10 dice can be 60
|
0

Why not use an nested arraylist for each trial?

public class TenDice {

    private static String strJoin(String[] lst, String delimeter) {
        StringBuilder sb = new StringBuilder();

        for (String s : lst) {
            if (s != null) {
                sb.append(s).append(delimeter);
            }
        }

        sb.setLength(sb.length() - delimeter.length());
        return sb.toString();
    }

    public static class HistogramBar {
        private String[] data;
        private int top;
        private int size;

        public HistogramBar(int size) {
            data = new String[size];
        }

        public void add(String s) {
            data[top++] = s;
            size++;
        }

        public String[] getData() {
            return data;
        }

        public int getSize() {
            return size;
        }
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of trials: ");
        int trials = scan.nextInt();
        int numberDice = 10;
        int sidesPerDice = 6;

        HistogramBar[] results = getDiceTrialHistogram(numberDice, sidesPerDice, trials);
        System.out.println(results.length);

        for (int i = 0; i < results.length; i++) {
            int sum = i + 10;
            int length = results[i].getSize();
            System.out.printf("%d: %d\n", sum, length);
        }

        System.out.println("\nHistogram");

        for (HistogramBar histBar : results) {
            System.out.printf("|%s\n", strJoin(histBar.getData(), ""));
        }
    }

    private static HistogramBar[] getDiceTrialHistogram(int numberDice, int sidesPerDice, int trials) {
        int totalSum = numberDice * sidesPerDice;
        int size = totalSum - numberDice + 1;

        HistogramBar[] results = new HistogramBar[size];
        for (int i = 0; i < size; i++) {
            results[i] = new HistogramBar(trials);
        }

        Random r = new Random();

        for (int i = 0; i < trials; i++) {
            int total = 0;

            for (int rolls = 0; rolls < numberDice; rolls++) {
                total += r.nextInt(6) + 1;
            }

            HistogramBar totalList = results[total - numberDice + 1];
            totalList.add("*");
        }

        return results;
    }

}

Output

Enter number of trials: 
1000
10: 0
11: 0
12: 0
13: 0
14: 0
15: 0
16: 0
17: 0
18: 0
19: 0
20: 1
21: 1
22: 5
23: 7
24: 7
25: 20
26: 28
27: 24
28: 47
29: 40
30: 48
31: 43
32: 69
33: 58
34: 64
35: 77
36: 73
37: 62
38: 85
39: 33
40: 51
41: 36
42: 33
43: 19
44: 29
45: 10
46: 11
47: 7
48: 8
49: 4
50: 0
51: 0
52: 0
53: 0
54: 0
55: 0
56: 0
57: 0
58: 0
59: 0
1000

Histogram
|
|
|
|
|
|
|
|
|
|
|*
|*
|***
|********
|************
|***********************
|*********************
|**************************
|**************************
|*********************************************
|***********************************************
|**********************************************************
|**********************************************************
|**************************************************************************
|*******************************************************************************************
|******************************************************************
|*******************************************************************
|*****************************************************
|********************************************************************
|**************************************************************
|**************************************
|********************************
|**************************************
|***************************
|******************
|***********
|******
|**********
|*****
|****
|*
|
|
|
|
|
|
|
|
|

11 Comments

My first solution was way off, sorry about that :)
this is great, how can i get the sums in the row next to the given histogram?
Update the printf statement to include histBar.size()
oh sorry, not the totals, the numbers like 10-60 that u have in the first part of the output i want in front of the histogram results
Then use the first loop, but change the length variable to be the strJoin of the list
|
0

In Java 8

int numberDice = 10;
int sidesPerDice = 6;
int roll = 1000;
Random random = new Random();

int[] freq = IntStream.range(0, roll)
    .map(r -> random.ints(numberDice, 1, sidesPerDice + 1).sum())
    .collect(() -> new int[numberDice * sidesPerDice + 1], (a, e) -> ++a[e],
        (a, b) -> { for (int i = 0; i < a.length; ++i) a[i] += b[i];});

IntStream.range(numberDice, freq.length)
    .forEach(i -> System.out.printf("%4d:%4d:%s%n", i, freq[i],
        String.format("%" + (freq[i] + 1) + "s", "").substring(1).replaceAll(" ", "*")));

result

  10:   0:
  11:   0:
  12:   0:
  13:   0:
  14:   0:
  15:   0:
  16:   0:
  17:   0:
  18:   0:
  19:   0:
  20:   1:*
  21:   5:*****
  22:   6:******
  23:   5:*****
  24:  10:**********
  25:  18:******************
  26:  23:***********************
  27:  31:*******************************
  28:  40:****************************************
  29:  41:*****************************************
  30:  45:*********************************************
  31:  51:***************************************************
  32:  54:******************************************************
  33:  62:**************************************************************
  34:  72:************************************************************************
  35:  76:****************************************************************************
  36:  71:***********************************************************************
  37:  67:*******************************************************************
  38:  71:***********************************************************************
  39:  56:********************************************************
  40:  46:**********************************************
  41:  36:************************************
  42:  31:*******************************
  43:  24:************************
  44:  23:***********************
  45:  15:***************
  46:   5:*****
  47:   5:*****
  48:   5:*****
  49:   3:***
  50:   1:*
  51:   1:*
  52:   0:
  53:   0:
  54:   0:
  55:   0:
  56:   0:
  57:   0:
  58:   0:
  59:   0:
  60:   0:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.