Solved

Random dice roll using multidimensional retangular array

Posted on 2010-11-15
4
1,831 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:rcham68
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34139155
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?
0
 

Author Comment

by:rcham68
ID: 34139709
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.
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 34140645
IMHO, you're totally off track with things such as creating a 1 dimensional array of 361 elements. That sounds like you want to keep in memory what was the result of the 360 different rolls (not sure even if you were doing that why you want 361 elements rather than 360).

But in any case, the requirement does not include tracking the history of the rolls (i.e. you don't have to know what the 1st roll was vs what the 2nd roll was, etc).  You only have to track the number of times each of the 36 possible outcomes happen, so you only need an array with 36 total elements.  

Now you can create that array as either a one-dimensional array with 36 elements:  [0]=1,1; [1]=1,2 ... [5]=1,6; [6]=2,1; ...[35]=6,6.

You can also create a two-dimensional array with 6 elements in each dimension: [0][0]=1,1; [0][1]=1,2;[5][5]=6,6  (and since the assignment specifies 2-D, you'll want something like this).

From there, you just have to have a loop that rolls the two die 36 times, each time incrementing the appropriate element in the array the roll represents.  Then report the results in your array.

What you should find is that if you try something like 360,000,000 rolls (rather than just 360), that the value if each element in the array should be nearly equal as the random number generator should be equally distributing the possible outcomes.

The only array you need is an array to track a count of the 36 possible results.  You could do that with a one dimensional array where element [0] = roll of 1,1 while element [2]

  Each dimension of the array would hold the number of times   Then you can run the test for 360 rolls, 360,000 rolls, or 360,000,000 rolls.  As for the type of data in the array, you only have to track the number of times that
0
 

Author Closing Comment

by:rcham68
ID: 34146652
Still was a little confusing but after working with it I got it.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 37
performance tune IIS 10 on win 2016 that only runs one website 4 58
Hey!! 5 33
Test the speeds on my PC Drives 12 43
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question