using Microsoft.VisualStudio.TestTools.UnitTesting;
public void RollingDices()
int totalAttempts = 10000;
for (int i = 0; i < totalAttempts; i++)
double probability = (double)successCount / totalAttempts;
Console.WriteLine($"Probability of rolling 6-5-4 within three attempts: {probability:P}");
Console.WriteLine($"An error occurred: {ex.Message}");
private bool SimulateDiceRoll()
Random random = new Random();
for (int i = 0; i < 3; i++)
int roll1 = random.Next(1, 7);
int roll2 = random.Next(1, 7);
int roll3 = random.Next(1, 7);
if ((roll1 == 6 && roll2 == 5 && roll3 == 4) ||
(roll1 == 6 && roll3 == 5 && roll2 == 4) ||
(roll2 == 6 && roll1 == 5 && roll3 == 4) ||
(roll2 == 6 && roll3 == 5 && roll1 == 4) ||
(roll3 == 6 && roll1 == 5 && roll2 == 4) ||
(roll3 == 6 && roll2 == 5 && roll1 == 4))
public class DiceRollingTests
public void RollingDices_PrintsProbability()
DiceRolling diceRolling = new DiceRolling();
StringWriter consoleOutput = new StringWriter();
Console.SetOut(consoleOutput);
diceRolling.RollingDices();
string output = consoleOutput.ToString();
Assert.IsTrue(output.StartsWith("Probability of rolling 6-5-4 within three attempts:"));
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
public void RollingDices_HandlesException()
DiceRolling diceRolling = new DiceRolling();
StringWriter consoleOutput = new StringWriter();
Console.SetOut(consoleOutput);
MockRandom.ShouldThrowException = true;
var originalRandom = DiceRollingAccessor.random;
DiceRollingAccessor.random = new MockRandom();
diceRolling.RollingDices();
string output = consoleOutput.ToString();
Assert.IsTrue(output.StartsWith("An error occurred:"));
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
DiceRollingAccessor.random = originalRandom;
MockRandom.ShouldThrowException = false;
private static class DiceRollingAccessor
public static Random random = new Random();
private class MockRandom : Random
public static bool ShouldThrowException { get; set; }
public override int Next(int minValue, int maxValue)
if (ShouldThrowException)
throw new Exception("Simulated random number generation exception.");
return base.Next(minValue, maxValue);