CSharp - Write program to Roll a Single Dice

Requirements

You will write a program that "throws" a dice three times.

Hint

To work with chance, you need a random number generator.

In C#, you use the Random object for that purpose.

You first create a Random object by calling its constructor.

Then you can use its method called Next.

The Next method (action) requires two parameters in parentheses.

  • The lower bound of the interval of generated numbers
  • The upper bound increased by 1.

Demo

using System;

class Program{/*from w ww  .jav a  2  s .c  o  m*/
    static void Main(string[] args)    {
        // Creating random number generator object 
        Random randomNumbers = new Random();

        // Repeatedly throwing a die 
        int number1 = randomNumbers.Next(1, 6 + 1);
        int number2 = randomNumbers.Next(1, 6 + 1);
        int number3 = randomNumbers.Next(1, 6 + 1);

        // Output 
        Console.WriteLine("Thrown numbers: " +
            number1 + ", " +
            number2 + ", " +
            number3);
    }
}

Result