Rolling a 6, 8, 10 and 12 sided dice.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
using System;Brings the types from the System namespace into your program — that’s how you get access to Console, Random, Convert, and many other built-in types. Other namespaces like System.Collections.Generic or System.IO give you access to lists, dictionaries, file I/O, and more.
new Random(seed)Creates a new pseudo-random number generator. If you pass a seed value, the generator produces the same sequence every time — useful for deterministic tests. Omit the seed (new Random()) to use the current time as the seed and produce a different sequence each run.
random.Next(min, max)Generates a random integer between min (inclusive) and max (exclusive). To roll a dice with faces sides, use random.Next(1, faces + 1).
Questions to think about with this program to check your understanding:
Identify two constants in the program that could be variables instead.
The numbers 1 and 6 could be constants in this version of the program although that would limit the program to only simulating the roll of a six-sided dice.
State what the two parameters are used for in the random.Next call.
The lowest number (inclusive) and the upper bound (exclusive) of the range. random.Next(1, faces + 1) produces any value from 1 up to and including faces.
Change the program so that it:
Roll a D
6
You rolled a 3
Roll a D
8
You rolled an 8
Roll a D
10
You rolled a 4
Roll a D
12
You rolled an 11
// Polyhedral dice program
using System;
class Submission
{
---
// -------------------------
// Globals
// -------------------------
static Random random_generator = new Random();
---
// -------------------------
// Subprograms
//--------------------------
// Function to roll a dice.
static int roll_dice(string dice)
{
---
int faces = Convert.ToInt32(dice);
int result = random_generator.Next(1, faces + 1);
return result;
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Roll a D");
string dice = Console.ReadLine();
int number = roll_dice(dice);
---
if (number == 8 || number == 11)
{
---
Console.WriteLine($"You rolled an {number}");
---
}
---
else
{
---
Console.WriteLine($"You rolled a {number}");
---
}
---
}
---
}