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.
In these cases, it is always useful to look at the documentation: Random() .
Here it tells you:
A little clarification; computers are very bad at generating true random values. What is called "random" is usually actually pseudo-random ; Based on an initial number ( seed ), some formulas are used to obtain the following.
That is, an object created by
Random()
will return a series of values depending on when it is created. If you create two objects with the same initial value (=at the same time, or almost), the series of values will be identical.Solution: There are two possible solutions:
Make a small pause in the execution so that at least 1ms passes.
Cause the two numbers to be initialized differently; for instance
Add constructor:
and when creating them
This makes each die have a different seed, ensuring that the sequences will not be the same.
UPDATE
On second thought, the solution above has a small problem: there is no guarantee that two consecutive random numbers are not the same; while the probability is minuscule (
1 / (2 ^ 32)
, o1 entre 4.294.967.296
), it is not 0. And if that unlikely case 1 occurs , then the dice will roll the same numbers again (again, less than once every four billion program executions) .So this variation would be better:
Also, as I indicated in a comment, another option would be to create a single given object and simply execute it more times; in real life several are often used to make the rolls faster but that in a program is not a problem. So:
1 To give us an idea, the possibility that rolling two perfect dice 10 times, in each roll the dice get the same value is
1 entre 6 ^ 10
or1 entre 60.466.176
, which is much greater than the probability that this error occurs.aleatorio
Make the propertystatic
aleatorio
The reason is that by making the fieldstatic
both instances share the same objectRandom
. In this way, each time Value is called, a random number different from the previous one will be obtained.The Random class does not really generate random numbers but rather these are based on a seed (
seed
), by not passing the value of seed as a parameter, one is generated based on the current date.If two instances of the class are created
Random
consecutively, the seed is being initialized to the same value, so the two instances will generate the same "random numbers"You can use the following code on the button click and you will see that it is quite good.