Good day!
I'm learning to program in C# and I'm making a program that gives me the result of two dice.
So I make a Dice class and create two objects dice1 and dice2 and call a method that calculates a random number for me.
The problem that always calculates the two equal dice.
This is how I wrote the Given class:
class Dado
{
private int dado;
private Random aleatorio = new Random();
public Dado()
{
dado = 0;
}
public int Valor()
{
dado = aleatorio.Next(1, 7);
return dado;
}
}
and from the form I create the two objects given1 and given2, and within a button I "throw" the dice.
private Dado dado1, dado2;
public Form1()
{
InitializeComponent();
dado1 = new Dado();
dado2 = new Dado();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Dado 1 salio: " + Convert.ToString(dado1.Valor()) + " y Dado 2 salio: " + Convert.ToString(dado2.Valor());
}
But whenever I press the button the two dice fall on the same number, they change but they fall the same. Example:
First throw dice1 = 2 and dice2 = 2 Second throw dice1 = 5 and dice2 = 5 Third throw dice1 = 3 and dice2 = 3
it's as if when the given2 object calls the Value method it doesn't work.
Thank you very much for your time.
Greetings.