Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 1050
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am displeased. You are no longer allowed to read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic