jeudi 22 septembre 2016

A roll dice returns the same value to both

I built a simple class:

class Die
{
    //Vars

    int number;
    Random R = new Random();

    //Buliding Command

    public Die ()
    {
        number = R.Next(1, 7);
    }

    // Roll
    public void Roll()
    {
         R = new Random();
        number = R.Next(1,7);
    }

    // Get Number

    public int GetNum()
    {
        return number;
    }
}

But whenever I write 2 Dices, it gives the same value to both of them when rolling:

static void Main(string[] args)
{
    Die Dice1 = new Die();
    Die Dice2 = new Die();

    while (Dice1.GetNum() != 6 && Dice2.GetNum() != 6)
    {
        Dice1.Roll();
        Dice2.Roll();
        Console.WriteLine("Dice 1: "+Dice1.GetNum()+"     Dice 2: "+Dice2.GetNum());
    }


    Console.ReadKey();
}

What I get in return is that Dice1 and Dice2 is ALWAYS equal to each other after rolling:

Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 1 Dice 2: 1 Dice 1: 6 Dice 2: 6




Aucun commentaire:

Enregistrer un commentaire