Illustrating the probability distribution.
★★★
If a computer's random number generator is providing an accurate distribution of numbers, when you simulate the roll of two dice and record the sum, you should achieve a triangular curve in the result of a thousand rolls. This program illustrates the output of the random number generating algorithm used by the Random class in C#.
Write a program that outputs the number of 2’s, 3’s, 4’s etc. that are rolled by adding two random numbers between one and six. The number of rolls to simulate is input by the user.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
distribution that:distribution subprogram.How many rolls to simulate? :
2 : 34
3 : 61
4 : 82
5 : 131
6 : 147
7 : 168
8 : 130
9 : 101
10 : 79
11 : 44
12 : 23
// Distribution of two dice program
using System;
class Submission
{
---
// -------------------------
// Globals
// -------------------------
static Random random_generator = new Random();
---
// -------------------------
// Subprograms
// -------------------------
static void distribution(int rolls)
{
---
// Initialise dice and results
int[] dice = {0, 0};
int[] result = new int[13];
---
for (int roll = 0; roll < 13; roll++)
{
---
result[roll] = 0;
---
}
---
// Simulate dice rolls
for (int counter = 0; counter < rolls; counter++)
{
---
dice[0] = random_generator.Next(1, 7);
dice[1] = random_generator.Next(1, 7);
---
int sum = dice[0] + dice[1];
---
result[sum] += 1;
---
}
---
// Output result
for (int roll = 2; roll < result.Length; roll++)
{
---
Console.WriteLine($"{roll} : {result[roll]}");
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("How many rolls to simulate? :");
int rolls = Convert.ToInt32(Console.ReadLine());
---
distribution(rolls);
---
}
}