Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Dice Rolling Simulation

Lucas Martin
Greenhorn
Posts: 8
I have coded a dice rolling simulation to roll a single die 36,000 times, store the data in the array and display how many times the face value landed. My question is what do I need to do to change this to simulate rolling 2 die to display the output of 2-12 face value, so adding the face value of the 2 randomly rolled die and incrementing face value. Here is the code I have for the single die:

Greg Brannon
Bartender
Posts: 563
So you have an array that can hold 6 values, 1 - 6, and a random number generator that generates random values 1 - 6. With two dice, the array will have to be scaled to hold the values 2 - 12 (11 total), and your random number generator will have to be modified to generate values from 2 - 12. Modifying the code you've already written should be relatively easy.

Lucas Martin
Greenhorn
Posts: 8
Greg Brannon wrote:So you have an array that can hold 6 values, 1 - 6, and a random number generator that generates random values 1 - 6. With two dice, the array will have to be scaled to hold the values 2 - 12 (11 total), and your random number generator will have to be modified to generate values from 2 - 12. Modifying the code you've already written should be relatively easy.

I thought about that, wouldn't that throw off the results of rolling 2 die with a 1-6 outcome and adding the values? I guess I am failing at the logic here.

Matthew Brown
Bartender
Posts: 4568
9
• 1
Lucas Martin wrote:I thought about that, wouldn't that throw off the results of rolling 2 die with a 1-6 outcome and adding the values?

Yes, it would. You need to extend array, but then generate the index by adding together two random numbers from 1-6.

Lucas Martin
Greenhorn
Posts: 8
That answers it for me, thanks.

Lucas Martin
Greenhorn
Posts: 8
Well I thought I knew what I was doing and have spent some timeon this but apparently not. So now I am getting dice1 has not been initialized and I do not think I am summing up the two random 1,6 numbers into the total and adding them to the array correctly, any final touch ups are appreciated, thanks.

Greg Brannon
Bartender
Posts: 563
Matthew Brown wrote:
Lucas Martin wrote:I thought about that, wouldn't that throw off the results of rolling 2 die with a 1-6 outcome and adding the values?

Yes, it would. You need to extend array, but then generate the index by adding together two random numbers from 1-6.

Sorry, I didn't think through the subtleties. I'd have gotten it wrong.

Lucas Martin
Greenhorn
Posts: 8
I got it: I was initializing them correctly, but to my surprise I added the braces and works like a charm *forehead denter*

Greg Brannon
Bartender
Posts: 563
As penance, I modified your program to include a 2nd column that shows how messed up generating the sum from a single random number would be, about what you'd expect if the random number generator does what it's supposed to do:

A sample run:

Lucas Martin
Greenhorn
Posts: 8
Exactly, the 7's are much more likely to add up from the 2 die sums and it shows this.
Well at least now it's here for people to learn from.