Link to home
Start Free TrialLog in
Avatar of rcham68
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("*");
           }
          
           
          
       }


}

Open in new window

Avatar of HooKooDooKu
HooKooDooKu

Based on the description, it sounds like what is desired is a 2-D array (6x6) where the 1st dimension relates to the roll of the 1st die and the 2nd dimension relates to the roll of the 2nd die (otherwise, the whole thing could be done with a 1-D array of length 36).

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?
Avatar of rcham68

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
Avatar of HooKooDooKu
HooKooDooKu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rcham68

ASKER

Still was a little confusing but after working with it I got it.