-1

I'm writing a program where the user gets to pick a number of dice to be rolled and it then calculates the average of the dice rolls. Everything works as it should but the dice rolls are all identical instead of new numbers. How do I fix this AND return them all? It is the first method that should be the problem.

Thanks

    static int RullaTärning(Random slumpObjekt)
    {            
        Random slump = new Random();                                        
        int slumpa = slump.Next(1, 6);
        return slumpa;
     
    }
    static void Main()
    {
        Random slump = new Random(); 
        List<int> tärningar = new List<int>(); 

        Console.WriteLine("\n\tVälkommen till tärningsgeneratorn!");

        bool kör = true;
        while (kör)
        {
            Console.WriteLine("\n\t[1] Rulla tärning\n" +
                "\t[2] Kolla vad du rullade\n" +
                "\t[3] Avsluta");
            Console.Write("\tVälj: ");
            int val;
            int.TryParse(Console.ReadLine(), out val);

            switch (val)
            {
                case 1:
                    Console.Write("\n\tHur många tärningar vill du rulla: ");
                    bool inmatning = int.TryParse(Console.ReadLine(), out int antal);

                    if (inmatning)
                    {
                        for (int i = 0; i < antal; i++)
                        {
                            
                            tärningar.Add(RullaTärning(slump));
                        }
                    }
                    break;
                case 2:
                    int sum = tärningar.Sum() / tärningar.Count; 
                    if (tärningar.Count <= 0)
                    {
                        Console.WriteLine("\n\tDet finns inga sparade tärningsrull! ");
                    }
                    else
                    {
                        Console.WriteLine("\n\tRullade tärningar: ");
                        foreach (int tärning in tärningar)
                        {
                            Console.WriteLine("\t" + tärning);
                        }
                        Console.WriteLine("\n\tMedelvärdet på alla tärningsrull: " + sum); 
                    }

                    break;
                case 3:
                    Console.WriteLine("\n\tTack för att du rullade tärning!");
                    Thread.Sleep(1000);
                    kör = false;
                    break;
                default:
                    Console.WriteLine("\n\tVälj 1-3 från menyn.");
                    break;
            }
        }
    }
}

}

Strand
  • 7
  • 3
  • It's best if you create `Random` once as it seeds off the clock so multiple calls to your method in quick succession will end up using the same seed and thus you'll get the same random number until the clock ticks over to a new value. – juharr Oct 19 '21 at 17:24

1 Answers1

0

new Random() creates an object seeded with the current time. The default clock's accuracy is system dependent but is generally not accurate enough to consistently return different values if called in a loop, so when you call the same method quickly it will seed the object with the same time. Since you're passing an instantiated Random object into the method already your simplest solution is to call Next on that object instead.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17