dimanche 12 juin 2016

random number the same even thought Im sure Im using 1 instance as required [duplicate]

This question already has an answer here:

I'm not as gifted as some and after looking through the examples on here I can't figure out why I am getting the same number each set of rolls. I thought I was using only 1 instance as said on the forums. Can someone please correct me?

Thanks in advance

    static void Main(string[] args)
    {
        Console.WriteLine("Hi choose a type of dice to throw");
        int dice = Convert.ToInt16(Console.ReadLine());

        Console.WriteLine("Now how many of that dice to throw");
        int numberOfDice = Convert.ToInt16(Console.ReadLine());

        Console.WriteLine("Now how many times to reroll");
        int rerolls = Convert.ToInt16(Console.ReadLine());


        for (int i = 1; i <= rerolls; i++)
        {
            int result = diceThrower(numberOfDice, dice);

            Console.WriteLine("\nRoll #{0} is {1}",i, result);

        }
        Console.ReadLine();

    }

    private static int diceThrower(int numberOfDice, int sideOfDice)
    {
        Random rnd = new Random();
        int diceResult = 0;
        int diceRoll;

        for (int i = 1; i < numberOfDice; i++)
        {
            diceRoll = rnd.Next(1, sideOfDice + 1);
            diceResult = diceResult + diceRoll;
        }

        return diceResult;

    }

Aucun commentaire:

Enregistrer un commentaire