Solved

Random dice roll using multidimensional retangular array

Posted on 2010-11-15
4
1,876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

623 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