# Programming Challenge – Roll the Dice

Write a program which simulates two dice with values from 1 to 6. The program takes a single argument which is the number of dice. The output should contain the values of the dice and the probability for this combination to occur. The probability is expressed as a decimal value between 0 and 1 with three decimal points.

How do you find the probability of a dice roll? Check out this explanation: http://www.math.hawaii.edu/~ramsey/Probability/TwoDice.html

If you think you have it figured out, post below and email me a link to your source so I can check it out!  I’ll post the solution at the end of the week.

Post updated with solution below
[codesyntax lang=”csharp” lines_start=”1″ title=”Solution” blockstate=”collapsed]

```class Program
{
static void Main(string[] args)
{
//welcome the user and start the program
Console.WriteLine("Welcome to the dice probability program");
Console.WriteLine("-------------------------");
Console.WriteLine("Rolling dice");

//create a new dice object and roll
Dice newRun = new Dice();
newRun.Roll();

//display the result of the rolls
Console.WriteLine("Die 1 rolled a {0}", newRun.Die1);
Console.WriteLine("Die 2 rolled a {0}", newRun.Die2);

//probability
//Since each die has six sides and the game is played with two dice, there are 36 (6 x 6) combinations
//find the total of the two dice
int total = newRun.Die1 + newRun.Die2;

double probability = 0;

//compare the total to the probability out of 36
switch (total)
{
case 2: probability = 1.0 / 36.0; // 1 possible combination of dice 1/36
break;

case 3: probability = 2.0 / 36.0; // 2 possible combinations of dice 2/36
break;

case 4: probability = 3.0 / 36.0; // 3 possible combination of dice 3/36
break;

case 5: probability = 4.0 / 36.0; //4 possible combination of dice 4/36
break;

case 6: probability = 5.0 / 36.0; //5 possible combination of dice 5/36
break;

case 7: probability = 6.0 / 36.0; //6 possible combination of dice 6/36
break;

case 8: probability = 5.0 / 36.0; //5 possible combination of dice 5/36
break;

case 9: probability = 4.0 / 36.0; //4 possible combination of dice 4/36
break;

case 10: probability = 3.0 / 36.0; // 3 possible combination of dice 3/36
break;

case 11: probability = 2.0 / 36.0; // 2 possible combinations of dice 2/36
break;

case 12: probability = 2.0 / 36.0; // 1 possible combination of dice 1/36
break;

}

//output result
Console.WriteLine("The probability of this roll is {0:F3}", probability);
}
}

class Dice
{
private int die1;
private int die2;

public int Die1
{
get { return die1; }
set { die1 = value; }
}

public int Die2
{
get { return die2; }
set { die2 = value; }
}

public void Roll()
{
//get a random number object we can the use to determine the die face
Random rand1 = new Random();
Die1 = rand1.Next(1,6);
Die2 = rand1.Next(1,6);
}
}```

[/codesyntax]