Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Three dices problem, stuck at extracting biggest occurence to return frequency

1. ## Three dices problem, stuck at extracting biggest occurence to return frequency

Hello,
First off, I am a complete beginner, with only - I'd say 60 - 70 hours of practice overall.
I have to write a code for the following problem: you have three dices (typical, 6 faces) which you have to roll as many times as the user chooses with an initial input (like in "how many times do you want to roll the dices" kind of input), and I have to generate a simple table with the first column being all possible sum-of-the-three combinations (from 3 to 18, obviously) and in the second column the frequency of each possible combination (hence the frequency of sum=3, frequency of sum=4 and so on till the frequency of sum=18). While I am able to perform everything right and get random number of sums for various number of rolls, it seems over my head to figure out the code to get the frequency for each of the 16 possible sums. I am drooling for a little bit of help here. Anybody? Really appreciated  Reply With Quote

2. ## Re: Three dices problem, stuck at extracting biggest occurence to return frequency

I believe in this case the frequency is the same as the odds or the expected "frequency" that a given sum is to occur. You may have sixteen possible sums, but some of them can be made in different ways. For example, 6 can be either 1 + 1 + 4, 1 + 2 + 3, or 2 + 2 + 2.

The best way to start is to try and work this out on paper in a step by step process. They try and transfer that process into code.

Regards,
Jim  Reply With Quote

3. ## Re: Three dices problem, stuck at extracting biggest occurence to return frequency

Thank you Jim, yes, the pattern of the sum is irrelevant for my query, it does not matter the "composition" of a sum. And yes, I did it on paper, I simply don't know how to translate the logic in code lines, that is the problem. As an example, if I choose to roll the dices 11 times, I get 11 different sums, with a certain pattern of frequencies, like (again, just as an example) 2 times sum=4, 3 times sum=5, 3 times sum=6 2 times sum=8 and 1 time sum=11. Well, my table should look something like - first column, from top to bottom, 3, 4, 5, 6,... 18, second column, top to bottom (for this example) 0 (because sum=3 didn't appear in my roll), 2 (because 4 showed up 2 times), 3, 3, 0 (for 7, because it didn't show in the roll), 2, 0, 0, 1, 0, 0, etc. So the question is how do I code the total number of times a certain sum happened to appear in a particular roll.
Anyways, thank you for the reply  Reply With Quote

4. ## Re: Three dices problem, stuck at extracting biggest occurence to return frequency

I guess I'm thinking of frequencies different than you are. I was thinking of them as the probability (odd) that a given sum would be rolled. To do that you don't need to actually roll any dice. You know that 3 can only occur one way as 1 + 1 + 1. Same for 18. So the expected frequency of getting a 3 or 18 would 1 out of 16. So you would need to simply count how many times each sum occurred. To do the sums, nested loops could be used.

Does the problem statement explain what is meant by frequency?

Regards,
Jim  Reply With Quote

5. ## Re: Three dices problem, stuck at extracting biggest occurence to return frequency

Yes, that is correct if you think in terms of probabilities, but I don't have to make a table with the probable number a certain sum might show up in a certain number of rolls (which is a pure mathematical/probabilistic approach, and, indeed, you don't even need to roll dices for that), rather I need to extract the number of times a certain type of sum shows up in a finite number of rolls (number which will absolutely differ from a series of rolls to the other, depending on the random generator). As an attempt to be crystal clear, if I make ten sets of 15 rolls and I fill up the table for each set of rolls, the columns will be different every single time, even if the tendency will be for the sums towards the middle to be the most frequent (the most number of combinations possible) while the sums at the extremes (3 and 18) will tend to be less frequent because of the limited possible combinations, right?
The problem states "show the number of times each sum appears on a certain set of rolls", which I interpreted as frequency. In other words, if in a set of 20 rolls the sum of 11 appears 5 times, I would call that the "frequency" of sum 11 showing up in the results. Am I interpreting it wrong?
Thank you Jim  Reply With Quote

6. ## Re: Three dices problem, stuck at extracting biggest occurence to return frequency

Perhaps you could do it this way. Have a HashMap<Integer,Integer>. They keys would be the actual sum possibilities. The values would be the number of times you rolled that sum. You could do this thousands of times. The key for the sum 3 might have 30 times. The key for the sum 10 might have 300 times. You could also do it with an array. The indices would be the sums and the values would be the number of times you rolled that sum. You would ignore indices 0, 1, and 2 since those are impossible sums. To double check everything, the sum of each sums count should equal the number of times you threw the dice.

If you try to to show more information than that(e.g. what the values of the dice were or weren't) the tables would get cumbersome. Nor am I certain how you would do that.

Regards,
Jim  Reply With Quote