-1

Hello im very new to C# and coding, so need some basic help. If the user chooses to roll multiple dices, (2,3,4,5,6,7,8 etc), what do you do to make it roll randomly on all the dices? For example: "Dices rolled: 2,5,3". Instead of it now being "Dices rolled: 2,2,2", or "4,4,4", basiaclly the same number.

 static int RollTheDice(Random rndObject)
    {
        Random dice = new Random();
        int nr = dice.Next(1, 7);  // if user requests to roll multiple dices how
                                   // do you make all the rolls random and not the same


        return nr;
    }

    static void Main()
    {
        Random rnd = new Random();
        List<int> dices = new List<int>();

        Console.WriteLine("\n\tWelcome to the dicegenerator!");


        bool go = true;
        while (go)
        {
            Console.WriteLine("\n\t[1] Roll the dice\n" +
                "\t[2] Look what you rolled\n" +
                "\t[3] Exit");
            Console.Write("\tChoose: ");
            int chose;
            int.TryParse(Console.ReadLine(), out chose);

            switch (chose)
            {
                case 1:
                    Console.Write("\n\tHow many dices do you want to roll?: ");
                    bool input = int.TryParse(Console.ReadLine(), out int antal);

                    if (input)
                    {
                        for (int i = 0; i < antal; i++)
                        {
                            dices.Add(RollTheDice(rnd));
                        }
                    }
                    break;
                case 2:
                    Console.WriteLine("\n\tDices rolled: ");
                    foreach (int dice in dices)
                    {
                        Console.WriteLine("\t" + dice);
                    }
                    break;
                case 3:
                    Console.WriteLine("\n\tThank you for rolling the dice!");
                    Thread.Sleep(1000);
                    go = false;
                    break;
                default:
                    Console.WriteLine("\n\tChoose between 1-3 in the menu.");
                    break;
A.Johansson
  • 11
  • 1
  • 2
  • 1
    Side note: the term "dices" in not correct. The plural is "dice". Singular is "die". – 001 Oct 28 '17 at 18:38
  • Do Google and Google and Google some more before posting. Nearly all the answers a new programmer will need are already here. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Oct 28 '17 at 18:42

1 Answers1

-1

You are creating a new Random every time, which will produce similar numbers if called within a short time frame. Refer to it here: How do I generate a random int number in C#?

You are already passing in a Random to your function, use it instead of creating a new one!

static int RollTheDice(Random rndObject)
{
    int nr = rndObject.Next(1, 7);  // if user requests to roll multiple dices how
                               // do you make all the rolls random and not the same
    return nr;
}
Felix Guo
  • 2,700
  • 14
  • 20