Click here to Skip to main content
14,774,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
Write an app to simulate the tossing of three dice 100 times.
Count the frequencies of the faces of the three dice that
show up with the same number. Display the output to the console.

1. Write a pseudocode of your algorithm
2. Use a function or functions to simulate the die toss
3. Use a function to display the output
4. Use appropriate Control structures to develop your algorithm


What I have tried:

class HeartRate
    {
        private string firstname;
        private string lastname;
        private int yearofbirth;
        private int currentyear;

       
        public string FirstName
        {
            get
            {
                return firstname;
            }

            set
            {
                firstname = value;
            }
        }

        public string LastName
        {
            get
            {
                return lastname;
            }

            set
            {
                lastname = value;
            }
        }
Posted
Updated 3-Nov-20 4:36am
v6
Comments
Sandeep Mewara 3-Nov-20 0:05am
   
And your issue is? Where are you stuck?
Richard MacCutchan 3-Nov-20 9:35am
   
Please do not change your question when people provide suggestions and solutions as they will not match the new question.

Start by reading the instructions carefully, and writing a method which simulates rolling a die:
private static Random rand = new Random();
private static int RollDie()
   {
   return rand.Next(6) + 1;
   }

And then test it:
for (int i = 0; i < 10; i++)
    {
    Console.WriteLine(RollDie());
    }
Check that the output looks pretty random:
1
3
4
5
6
5
4
1
2
1

This is important, because if you look closely, you will spot an important difference between your code and mine: I create a static class level Random instance and reuse it eatc time I want a random number, while you create it when you want it inside the method and discard it at the end.

Random number sequences are initialised to a "different starting point" each time you create them - and this is normally the system clock for the computer they are running on. If you create two instance too close together, the clock time hasn't changed at all (just like the time in a boring lesson never changes between two looks at the clock) so the random sequence is started from the same point each time. If your computer is powerful enough - and it almost certainly is - then call a method with creates a Random instance and gets a single value from it 100 times in a small loop and it will generate 100 identical values! Move the instance into the method, and try again, and I get identical values very time:
3
3
3
3
3
3
3
3
3
3

My code creates an instance once, and reuses it, so it always gets random values - they may have some duplicates, but that's the nature of random numbers, they do that just like you can roll a real-world die three times and get a six each time - occasionally! :laugh:

So take little steps, and test them as you go: make sure that it does exactly what you expect it to before you move on to the next bit.
Now you have a working die thrower, you can start on the next bit: rolling three dies by calling the method three times, and checking what you get as a result.

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
BillWoodruff 3-Nov-20 5:35am
   
+5 Good to see another example of your gift for teaching skills the OP needs to solve the problem, rather than writing the code for the OP !
OriginalGriff 3-Nov-20 5:58am
   
Give a man a fish, and you feed him for a day.
Teach a man to fish, and you won't see the bugger for weeks! :D
BillWoodruff 3-Nov-20 7:06am
   
My version of that goes like this: Give a man a nightmare, and he may die of fright; teach a man to scream, and he may be able to live with the horror he sees looking back at him in the mirror.
Your code could work, with some modifications. However, using a bit more the power of the array data structure, it can be made more compact:
class Program
{
    public static int [] die_toss(int iterations)
    {
        const int MAX = 6;
        const int DICE = 3;
        Random r = new Random();
        int []die = new int [DICE];
        int [] occurrence = new int[MAX];

        for (int n = 0; n < iterations; ++n)
        {
            for (int d = 0; d < DICE; ++d)
                die[d] = r.Next(0,MAX);

            if ( die[0] == die[1] && die[0] == die[2])
                    ++occurrence[die[0]];
        }
        return occurrence;
    }
    public static void display(int [] occurrences)
    {
        Console.WriteLine("Face |3x Frequency");
        Console.WriteLine(".....|............");
        for (int n = 0; n < occurrences.Length; ++n)
            Console.WriteLine(string.Format("  {0}  | {1}", (n+1), occurrences[n]));
    }

    static void Main(string[] args)
    {
        int iterations = 100;
        int [] occurrences = die_toss(iterations);
        display(occurrences);
    }
}




[update]
The 'fixed' OP code, as requested.
class Program
 {
     public static int[] DieToss()
     {
         Random r = new Random(); // create object of Random class

         int die1, die2, die3;

        int[] intAnswer = new int[6];

         for (int i = 0; i < 1000000; i++)    // toss 100 time
         {

             die1 = r.Next(1, 7);          // generate random number betwween 1 and 6
             die2 = r.Next(1, 7);
             die3 = r.Next(1, 7);


             if (die1 == 1 && die1 == die2 && die1 == die3)
             {
             		++intAnswer[0];                      // An if condition that state if a die appears with the face = 1 and
                                              // same face appears on the other two dice at same time
                                              // then calculate the number of times the face 1 came out on all 3 die after 100 rolls

             }

             else if (die1 == 2 && die1 == die2 && die1 == die3)
             {
                 ++intAnswer[1];                                           // if three dice has same face then increase count

             }

             else if (die1 == 3 && die1 == die2 && die1 == die3)
             {
                 ++intAnswer[2];;                                           // if three dice has same face then increase count

             }

             else if (die1 == 4 && die1 == die2 && die1 == die3)
             {
                 ++intAnswer[3];;                                           // if three dice has same face then increase count

             }

             else if (die1 == 5 && die1 == die2 && die1 == die3)
             {
                 ++intAnswer[4];;                                           // if three dice has same face then increase count

             }

             else if (die1 == 6 && die1 == die2 && die1 == die3)
             {
                 ++intAnswer[5];;                                           // if three dice has same face then increase count

             }


         }
         return intAnswer;

     }

     public static void Display(int [] intAnswer)
     {
         Console.WriteLine("Face |3x Frequency");
         Console.WriteLine(".....|............");
         Console.WriteLine("  1  | " + intAnswer[0]);
         Console.WriteLine("  2  | " + intAnswer[1]);
         Console.WriteLine("  3  | " + intAnswer[2]);
         Console.WriteLine("  4  | " + intAnswer[3]);
         Console.WriteLine("  5  | " + intAnswer[4]);
         Console.WriteLine("  6  | " + intAnswer[5]);

         //return 0;

     }

     static void Main(string[] args)
     {
         int count;

         
         int [] intAnswer = DieToss();         // call dieToss method
         Display(intAnswer);         // call display method
     }


 }

[/update]
   
v3
Comments
George Swan 3-Nov-20 4:15am
   
Your answer is excellent. Would the code be more robust if the simulator generated values between 1 and 6 rather than 0 and 5?
CPallini 3-Nov-20 4:36am
   
Why should it?
By the way, thank you.
PIEBALDconsult 3-Nov-20 12:06pm
   
If you play Cosmic Wimpout, then you know that the six faces of a cube are 10, planets, triangles, lightning bolts, 5, and stars. :D
George Swan 3-Nov-20 4:52am
   
Because, if you use it to simulate a single roll of the dice, you need to remember to correct the result by adding 1 to it. As a check for accuracy, the probability of a face match with 3 dice is 6/(6*6*6)= 2.8%. In my opinion,the sample size in the question is much too low to verify this.
CPallini 3-Nov-20 4:59am
   
Adding one is not a big deal, in my opinion (and allows you to directly index the occurrence array).
Yes, the sample is definitely too small. One million iterations, makes the compliancy with the statistics better.
BillWoodruff 3-Nov-20 5:39am
   
Voted: 3 If you rewrite the OP's code, does that help them learn ? or, does it teach them there are other people who will do their work for them ?
CPallini 3-Nov-20 5:53am
   
If you rewrite the OP's code, does that help them learn ?
Yes, in my opinion.
Dave Kreskowiak 3-Nov-20 10:39am
   
No, it doesn't. Doing someone's homework for them doesn't really help them. You've taken away the most important part of the assignment. Thinking about the problem and breaking it down. The OP's get working code, but they have no idea how you got there.

I quote:
"Please Could explain to me where am getting it wrong in my code, and how can I fix it to work if I were to use and follow my own code."
Kennnkey 3-Nov-20 5:41am
   
Thank you very much @CPallini the answer fix my problem, I really appreciate the work.
Kennnkey 3-Nov-20 5:43am
   
Please Could explain to me where am getting it wrong in my code , and how can I fix it to work if I were to use and follow my own code. Thank you
CPallini 3-Nov-20 6:29am
   
The major problem of your code is the DieToss method, that doesn't return, as it should, an array of found occurrences.
I've fixed it, see my updated solution.
Kennnkey 3-Nov-20 6:52am
   
Great i have now seen the problem to my work, Thank you very sir for the help and Explanation, I really appreciate you assistance.
CPallini 3-Nov-20 6:55am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900