rcham68
asked on
Random dice roll using multidimensional retangular array
I am trying to use the example in the book that I am reading but I am getting confused with this exercise. I am trying to use a Random object to generate a random integer in the range 1 to 6 for each die. There are 36 possible combinations (6 possible numbers for die 1 times 6 possible numbers for die 20. Repeat this for 360 times. Use a two dimensional rectangular array to count the frequency of each combination. Display the frequency counts.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Dice
{
public static void Main(string[] args)
{
Random randomNumber = new Random();
int[,] rolls = { { 0,0,0,0,0,0 } };
int roll = 0;
while (roll <= 360)
{
int die1 = randomNumber.Next(1, 7);
int die2 = randomNumber.Next(1, 7);
rolls[die1 - 1, die2 - 1]++;
roll++;
}
Console.WriteLine("Overall distribution:");
int[] frequency = new int[361];
foreach (int turn in roll)
{
++frequency[turn / 360];
}
for (int count = 0; count < frequency.Length; count++)
{
if (count == 36)
Console.Write(" 360: ");
else
{
Console.Write("{0:D2}-{1:D2}: ", count * 36, count * 36 + 360);
}
for (int stars = 0; stars < frequency[count]; stars++)
Console.Write("*");
}
}
}
ASKER
Yes that is what I want to do but I am not sure where I need to go with the code that I have or if I am totally off track.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Still was a little confusing but after working with it I got it.
If I'm right, the idea is that array element "Frequency[2][4]" would indicate the number of times the 1st die was a 3 (=2 for a zero based array index) and the 2nd die was a 5 (=4 for a zero based array).
So what you want to do is roll the two dice and increment the appropriate array value for the two dice for 360 rolls. Then iterate the array to display the results.
Otherwise, what's the question?