Search code examples
c#dice

How do I make a dice roll with the option to tell how many throws you want to do


I am new to programming and I have a task tomorrow. I have to make a dice where the "Thrower" is going to choose how many rolls they want to throw. I know the random function a little bit but how am I going to make the number of throws to be executed with the random function. I have tried different


Solution

  • I'm assuming this is schoolwork, so I won't give you everything. This should get you what you need, but you'll have to handle errors like invalid input and moving this into a function if that is required.

    private static void Main(string[] args)
    {
        Console.WriteLine("Enter number of throws:");
        string numThrows = Console.ReadLine();
    
        Random rnd = new Random();
    
        for (int i = 0; i < Convert.ToInt32(numThrows); i++)
        {
            int dice_number = rnd.Next(1, 7);
            Console.WriteLine("Dice result: " + dice_number);
        }
    }
    

    Console.ReadLine is how you get user input.

    The Random class allows you to generate random numbers, it's best to reuse a single instance rather than create new ones, so it is created outside of the for loop.

    Loop to the inputted number of throws. .Next(1, 7) limits this to a six sided dice.

    Write the results to the console.