System.Random generator = new Random(DateTime.Now.Millisecond);
int[] lotteryNumber = new int[7];
Console.WriteLine("Your lottery numbers: ");
for (int i = 0; i<7; i++)
{
lotteryNumber[i] = generator.Next(1, 37);
Console.Write("{0} ",lotteryNumber[i]);
}
Console.ReadLine();
我需要制作一个打印7个彩票号码的程序,但不需要重复。上面的代码打印了(1-37)范围内的7个随机数,但重复了appaer。我需要一种方法来防止出现重复的数字。
答案 0 :(得分:12)
最简单的方法IMO将生成一系列所有可能的数字(即1-37),对集合进行洗牌,然后取得前七个结果。
在Stack Overflow上搜索“Fisher-Yates shuffle C#”会找到很多例子。
事实上,您可以修改Fisher-Yates shuffle以在获取结果时产生结果,因此您可以编写如下方法:
var numbers = Enumerable.Range(1, 37).Shuffle().Take(7).ToList();
答案 1 :(得分:1)
您可以使用字典但请确保防止重复键插入。字典的键将作为您需要的唯一数字
答案 2 :(得分:0)
你可以将它们扔进HashSet<int>
。如果您Add
并且它返回false,则生成一个新数字。
答案 3 :(得分:0)
如果你试图从范围内选择数字而不重复,你需要创建一个包含所有可能数字的数组,然后“随机”选择一个随机选择:
int[] allPossibleNumbers = Enumerable.Range(1, 37).ToArray();
int[] lotteryNumber = new int[7];
for (int i = 0; i < 7; i++)
{
int index = r.Next(i, 37);
lotteryNumber[i] = allPossibleNumbers[index];
allPossibleNumbers[index] = allPossibleNumbers[i];
// This step not necessary, but allows you to reuse allPossibleNumbers
// rather than generating a fresh one every time.
// allPossibleNumbers[i] = lotteryNumber[i];
}
答案 4 :(得分:0)
生成包含37个项目的列表。 然后在您的for中,选择一个并删除所选的
答案 5 :(得分:0)
也许这可能会有所帮助,如果您获得现有数字,只需尝试找到不在数组中的新数字:
static void Main(string[] args)
{
System.Random generator = new Random(DateTime.Now.Millisecond); int[] lotteryNumber = new int[7];
Console.WriteLine("Your lottery numbers: ");
for (int i = 0; i < 7; i++)
{
int lNumber = 0;
do
{
lNumber = generator.Next(1, 37);
}
while (lotteryNumber.Contains(lNumber));
lotteryNumber[i] = lNumber;
Console.Write("{0} ", lotteryNumber[i]);
}
Console.ReadLine();
}
答案 6 :(得分:0)
HashSet<int> set = new HashSet<int>();
System.Random generator = new Random(DateTime.Now.Millisecond);
while(set.Count < 7){
set.Add(generator.Next(1,37);
}
这应该可行,因为HashSet会自动忽略重复项。只需循环直到该集合达到您需要的单位数。只有潜在的问题是它有潜在的(不太可能)循环很长一段时间,但它最终应该响应。
答案 7 :(得分:0)
所以我拿了你的原始代码......发现了一些逻辑错误并添加了你想要的修复,以防止随机数重复。
享受!
System.Random generator = new Random(DateTime.Now.Millisecond);
int[] lotteryNumber = new int[7];
int lowerBounds = 1;
int upperBounds = 8;
int maxNumberLotteryValues = 7;
if ( ( upperBounds - lowerBounds ) < (maxNumberLotteryValues))
{
Console.Write("Warning: Adjust your upper and lower bounds...there are not enough values to create a unique set of Lottery numbers! ");
}
else
{
Console.WriteLine("Your lottery numbers: ");
for (int i = 0; i < maxNumberLotteryValues; i++)
{
int nextNumber = generator.Next(lowerBounds, upperBounds);
int count = lowerBounds; //Prevent infinite loop
while ((lotteryNumber.Contains(nextNumber))
&& (count <= upperBounds))
{
nextNumber = generator.Next(lowerBounds, upperBounds);
count++; //Prevent infinite loop
}
lotteryNumber[i] = nextNumber;
Console.Write("{0} ", lotteryNumber[i]);
}
}
Console.ReadLine();
答案 8 :(得分:0)
const int nBalls = 37;
const int nPicks = 6;
int[] balls = new int[nPicks];
Random rnd = new Random(DateTime.Now.Millisecond);
int remainingBalls=nBalls;
int remainingPicks=nPicks;
for (int i = 1; i <= nBalls; i++)
{
if (rnd.Next(1, remainingBalls+1) <= remainingPicks)
balls[--remainingPicks]=i;
remainingBalls--;
}
Console.WriteLine(string.Join(",",balls));
随着nPicks / nBalls变大,将超越Shuffle和HashSet方法。