🎲
Dice Score Frequencies
Week 31
On a typical dice (with sides 1-6), it's known the most common combination in games when rolling two dice and summing their scores together is 7 - you want to find out how common other combinations are, so as to get a statistical advantage when playing many dice games with others
Not all games use 6-sided dice however - there are also other, admittedly less common, variations (of sides 4, 6, 8, 10, 12, 20) shown here - all except the 10-sided dice are platonic solids and you can assume each dice is fair (i.e. equal chance of landing on any one particular side)
Your goal is to determine the number of ways that different scores can be achieved by rolling 2 dice of the same number of sides - e.g. rolling 2 12-sided dice
For example, suppose we ran our program using hypothetical 2 and 3 sided dice (these wouldn't be platonic solids, so would be difficult to make fair - but assume they are for this output example) - the combined scores should be output in ascending order along with the number of ways to achieve that score (i.e. in the example, for a 2-sided dice, with 2 rolls, getting a score of 1 is impossible, so not included - getting a score of 2 is possible with 1 combination (1 + 1), a score of 3 is possible with 2 combinations 1 + 2 or 2 + 1 and a score of 4 is possible with just 1 combination of 2 + 2)
The number of sides your program should simulate are: 4, 6, 8, 10, 12 & 20 (in that order) - the output should be in the format demonstrated above
Although you shouldn't include this in the answer, as an extension/thought exercise, you should hopefully see a pattern and hence be able to easily determine the frequency distribution for any fair n-sided dice - even an e.g. hypothetical billion sided dice. It might also be interesting to plot what happens when increasing the number of dice - i.e. rolling 3 dice and summing them, then 4 and so on
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 | If we call the dice-size n, then you can create an integer array (representing the count of each score) of size n + n - 1 (since with 2 dice rolls, the lowest score you can achieve is 2, while the highest is n + n - giving a total of n + n - 1 unique values - e.g. a 4 sided-dice can achieve 7 scores (2, 3, 4, 5, 6, 7, 8) if rolled twice) |
| 3 | You can then have 2 loops (1 nested inside the other) from 1 to n simulating all the dice roll scores - i.e. 1 + 1, 1 + 2, 1 + 3...1 + n...2 + 1, 2 + 2 etc - you can then increment the array element at that position - - 2 (if using 0-based arrays...else -1 if using 1-based arrays), since the first array index corresponds to a score of 2, not 0 |
| 4 | Your array should now correctly have a count of the number of ways each score can be achieved - now you just have to loop through and output it |