• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping Through Rolling 1 Die a Million Times and Returning How Many Times Each Value Occurred  RSS feed

 
Chan Duller
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been struggling for hours to figure out how to do this. I already have a method that randomizes the answer every time I roll the dice called throwDice(). However, I can't seem to figure out how to return the answer. I am attaching my code thus far.

Add to your demo() method a loop that invokes throwDice() a million times, and print out the number of times you got a 1, the number of times you got a 2, and so on up to 6. These numbers should all be about 1/6th of a million. Here is an example of what your output should resemble (it won't be identical):
1: 166674396
2: 166690012
3: 166659722
4: 166662230
5: 166665847
6: 166647793

Screen-Shot-2015-10-15-at-2.42.04-PM.png
[Thumbnail for Screen-Shot-2015-10-15-at-2.42.04-PM.png]
Screen-Shot-2015-10-14-at-8.35.37-PM.png
[Thumbnail for Screen-Shot-2015-10-14-at-8.35.37-PM.png]
 
Liutauras Vilda
Marshal
Posts: 4658
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without looking at the pictures you attached, what the logic would be if you were meant to solve it in plain english?

1. Roll a dice
2. Check which number you got
3. Register that number
4. Do step 1 again (as many times as you need)
...
5. Print results of 1, 2, 3, 4, 5 and 6.

Does that sound correct?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would seem to me that since you have a random number generator that generates int's between 1 and 6 you would have something along this line:

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall challenge people, when you have sorted it out the old‑fashioned way, to show you how to use a Stream to solve your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!