Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Dice Rolling Simulation

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That answers it for me, thanks.
 
Lucas Martin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!