When I try to generate a new random int each time to fill the array it just gives me back the same number. The seed won't change. I'm sure there is an easy answer but I have been looking at it for a few hours and just can't see it.
internal class LingoCard : ILingoCard
{
private ICardNumber[,] _cardNumber;
private int _teller;
private int _oddTeller;
private int rand;
Random rnd = new Random();
public LingoCard(bool useEvenNumbers)
{
ICardNumber[,] cardNumber = new ICardNumber[5,5];
_cardNumber = cardNumber;
if (useEvenNumbers == true)
{
UseEvenNumbers(_cardNumber);
} else {
UseOddNumbers(_cardNumber);
}
}
public void UseEvenNumbers(ICardNumber[,] cardNumber)
{
while (_teller < 70)
{
for (int g = 0; g < 1; g )
{
rand = rnd.Next(0, 70);
CardNumber _walue = new CardNumber(rand);
if (rand % 2 == 0)
{
for (int i = 0; i < cardNumber.GetLength(0); i )
{
for (int j = 0; j < cardNumber.GetLength(1); j )
{
_cardNumber[i, j] = _walue;
}
_teller ;
}
}
}
}
}
CodePudding user response:
The reason your random number is the same for all the card numbers is because you're generating one Random number before the two nested for loops and using it to fill in every card number
rand = rnd.Next(0, 70); // Random number generated once here
CardNumber _walue = new CardNumber(rand);
if (rand % 2 == 0)
{
for (int i = 0; i < cardNumber.GetLength(0); i )
{
for (int j = 0; j < cardNumber.GetLength(1); j )
{
_cardNumber[i, j] = _walue; // same random value assigned for every card number here
}
_teller ;
}
}
In order to fix it you'd need to generate the random number inside the for loop when you are assigning _cardNumber
for (int i = 0; i < cardNumber.GetLength(0); i )
{
for (int j = 0; j < cardNumber.GetLength(1); j )
{
rand = rnd.Next(0, 70) * 2; // Multiply by 2 so the random number is even
CardNumber _walue = new CardNumber(rand);
_cardNumber[i, j] = _walue;
}
_teller ;
}
Let me know if this helps!