Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Rolling a 6, 8, 10 and 12 sided dice.

Polyhedral dice

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

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).

Investigate

Questions to think about with this program to check your understanding:

Item question

Identify two constants in the program that could be variables instead.

Reveal answer

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.

Structure question

State what the two parameters are used for in the random.Next call.

Reveal answer

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.

Make

Change the program so that it:

  1. Reads what sided dice to roll in the main program.
  2. If an 8 or an 11 are rolled it outputs “You rolled an” followed by the result of the roll.
  3. If any other number is rolled it outputs “You rolled a” followed by the result of the roll.

Typical inputs and outputs from the program would be:

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
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// 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}");
---
        }
---
    }
---
}