不断生成相同的数字
Same number keeps being generated
当我每次尝试生成一个新的随机整数来填充数组时,它只会返回相同的数字。种子不会改变。我确定有一个简单的答案,但我已经看了几个小时了,就是看不到。
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++;
}
}
}
}
}
你的随机数对所有卡号都相同的原因是因为你在两个嵌套的 for 循环之前生成一个随机数并用它来填充每个卡号
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++;
}
}
为了修复它,您需要在分配 _cardNumber
时在 for 循环内生成随机数
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++;
}
如果有帮助请告诉我!
当我每次尝试生成一个新的随机整数来填充数组时,它只会返回相同的数字。种子不会改变。我确定有一个简单的答案,但我已经看了几个小时了,就是看不到。
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++;
}
}
}
}
}
你的随机数对所有卡号都相同的原因是因为你在两个嵌套的 for 循环之前生成一个随机数并用它来填充每个卡号
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++;
}
}
为了修复它,您需要在分配 _cardNumber
时在 for 循环内生成随机数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++;
}
如果有帮助请告诉我!