I’m new to programming and I’m trying to build a die roller game in C#.
The program asks the user for the number of sides and then rolls a dice with a random number.
I have the following pseudocode:
- ask user for the number of sides.
- roll the die with a random number with max range being the number of sides.
- tell the user the number rolled
My question is how do I roll the dice with generating a random number in range specified by the user?
The
System.Randomclass is commonly used to generate casual random numbers.It has an overload of a method called
Nextwhich generates a random integer that is greater than or equal to0and strictly less than the passed integer argument.Thus if the user chooses an
n-sided die, and you have an instance ofRandomr,will generate a random integer between
1andninclusive.It is good practice to create a single instance of
Randomand reuse it, because if you create several instances close together they will all generate the same numbers.