-2

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?

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;

}
Zong
  • 6,160
  • 5
  • 32
  • 46
Regan
  • 1
  • 1
  • 2
    No, you don't use only one instance, you create many and so fast that they still get the same time as seeds. Use one Random instance as a class lvel variable! – TaW Jun 12 '16 at 20:00

1 Answers1

1

Create the rnd object in the Main method and pass that in as an argument to the diceThrower function

steveo40
  • 931
  • 5
  • 11