+ 1

How do I get this code to repeat 100 times while storing and averaging the number of times a six is rolled?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Playing_Around { class MainClass { public static void Main(string[] args) { Random numberGen = new Random(); int numberOfAttempts = 0; int attempt = 0; while (attempt != 6) { attempt = numberGen.Next(1, 7); Console.WriteLine("Tom Rolled: " + attempt + "."); numberOfAttempts++; } Console.WriteLine ("It took Tom " + numberOfAttempts + " attempts to roll a 6."); Console.ReadKey(); } } } Any help would be appreciated. I am curious how I could get this code to repeat 100 times while storing and averaging the number of times a six is rolled? Sorry, have only been learning code for a couple of weeks. :)

10th Jan 2018, 11:33 PM
Jeffery Jones
Jeffery Jones - avatar
7 odpowiedzi
+ 2
Oh, sorry. I didn't see yours. Thank you too, Dan Walker! Really helps a lot. Google wasn't helping.
11th Jan 2018, 12:11 AM
Jeffery Jones
Jeffery Jones - avatar
+ 1
I'd suggest wrapping the relevant code inside another loop, e.g. trialRuns and repeat 100 times. Define an array outside of both loops to store each trial run, and then you can average those values (: Once you've done that (get it working first!) try and extract the single trial code to another method which will tidy it up a bit
11th Jan 2018, 12:03 AM
Dan Walker
Dan Walker - avatar
+ 1
Thank you, Faisal! I will give it a try.
11th Jan 2018, 12:07 AM
Jeffery Jones
Jeffery Jones - avatar
+ 1
Wow, I am having a heck of a time implementing either of these methods due to my lack of experience. I am having a difficult time deciding which loop to use, etc. I guess I learn better by example. But, once I figure it out, I won't soon forget it. 😅
11th Jan 2018, 2:16 AM
Jeffery Jones
Jeffery Jones - avatar
0
What I would suggest is that you replace the while loop with a for loop and set an if statement within it checking to see if it equals 6 or not. To store the results, you can create an empty array and every time a number is rolled, you can append that number to the array. Then, you can set a for/while loop to go through the array and set a variable to be added by 1 every time it comes across a 6. To average the results, add up all the numbers in the array and divide that by the number of elements in the array. This will find the average number that was rolled, which may return a decimal.
11th Jan 2018, 12:03 AM
Faisal
Faisal - avatar
0
Hey what about me :( lol jk
11th Jan 2018, 12:10 AM
Dan Walker
Dan Walker - avatar
- 1
No problem! If it doesn't work just let me know and I'll help you out with It 😉
11th Jan 2018, 12:08 AM
Faisal
Faisal - avatar