0

I'm checking the frequency of the dice totals after 36 million rolls. I planned to make an arrayList and use it as a tally system when any of the 11 results came up. How would I add 1 to one of the items in the list?

int die1, die2, dicetotal;

ArrayList<Integer> results = new ArrayList<Integer>();  
results.add(0); //for the 11th

for(int i = 0; i < 36000000; i++){
          die1 = (int)(Math.random() * 6) + 1
          die2 = (int)(Math.random() * 6) + 1
          dicetotal = die1 + die2;

          Switch (dicetotal){
                 case 2: results.set(0, CURRENT + 1);
                 ...
                 ...
                 ...

      }
2
  • 1
    Why an ArrayList and not just an int[] ? Commented Jun 16, 2013 at 2:30
  • idk, it's in the current chapter that I'm working in and I thought it would look good to include it. Would you mind showing the possibility with int[]? Commented Jun 16, 2013 at 2:32

4 Answers 4

2

ArrayList would be overkill for this. But if you must, (untested code)

First initalize your array to contain 13 elements (0 to 12) so that IndexOutOfBoundsException won't pop up. They would be initialized to zero.

results = new ArrayList<Integer>(13);

Then just get the element, add one, and set it

results.set(dicetotal, results.get(dicetotal) + 1);

Indeed, you should just use an int[] if you know the size of the array beforehand, and won't change during the program. They are faster than ArrayList.

// initialize
results = new int[13];
// could be new int[11]; if you put results in array elements 0 to 10.

// ...

// add to tally
results[dicetotal]++;

// or, instead, pack the array because dice roll results can only be in 2 to 12. 
// Put such results in array element 0 to 10, 
// by subtracting 2 to the index before putting in
results[dicetotal - 2]++;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton! Definitely helped.
1

It would make more sense to store the frequencies in an array of integers. For one, it would be much simpler to increment the value for a particular result:

int[] frequencies = new int[11]; // sum of two dice can be 2-12

for(int i = 0; i < 36000000; i++){
    die1 = (int)(Math.random() * 6) + 1
    die2 = (int)(Math.random() * 6) + 1
    dicetotal = die1 + die2;

    // increment frequency:
    frequencies[dicetotal-2] = frequencies[dicetotal-2]+1;
}

Now your frequencies array has the frequency of the dice result of two at index 0.

4 Comments

the range 2-12 is inclusive, so the array should have 11 elements, not 10.
Yep. Was totally not paying attention there. Thanks, @luiges90 .
Array size must be 11 or he will get AIOBE when dicetotal = 12
@darijan -- Array size is 11. Perhaps refresh your browser window?
1

Use a fixed-size array for 11 possible scores you can get:

int die1, die2, dicetotal;

int[] totals= new int[11];
for(int i = 0; i < 36000000; i++){
    die1 = (int)(Math.random() * 6) + 1
    die2 = (int)(Math.random() * 6) + 1
    dicetotal = die1 + die2;

    //just increment the right position
    totals[dicetotal-2]++;
}

1 Comment

I had something else in mind. Changed it after a few seconds, but you seem to have caught it. :)
0

This may seem like an overkill but creating a wrapper for the counter so you can store it in a map will make the code a bit easier at the eys and easier to expand.

public static class Count {
    private int count = 0;

    public int getCount() {
        return count;
    }

    public void increment() {
        count++;
    }

    @Override
    public String toString() {
        return "" + count;
    }
}

public static void main(String[] args) {
    Map<Integer, Count> diceHistogram = new HashMap<>();
    for (int i = 2; i <= 12; i++) {
        diceHistogram.put(i, new Count());
    }

    for (int i = 0; i < (1E+6); i++) {
        int diceOne = rnd.nextInt(6) + 1;
        int diceTwo = rnd.nextInt(6) + 1;
        int sum = diceOne + diceTwo;

        diceHistogram.get(sum).increment();
    }

    System.out.println(diceHistogram);
}

Outputs the number of occurances for each dice-combo.

2=28043, 3=55745, 4=83489, 5=110517, 6=138823, 7=166928, 8=138466, 9=111321, 10=83532, 11=55469, 12=27667

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.