【问题标题】:Generating random numbers and inserting into array without repeats [duplicate]生成随机数并插入数组而不重复[重复]
【发布时间】:2012-08-30 22:07:41
【问题描述】:

可能重复:
Random number generator in C# - unique values

我正在尝试编写一个生成随机数的 C# 程序,检查该数字是否在我的数组中,如果是,则重复生成该数字,否则将该数字插入我的数组的插槽 [i]

到目前为止,这是我的代码:

        int check = 0;


        Random rand = new Random();

        int[] lotto = new int[6];



        for (int i = 0; i < lotto.Length; )
        {
            check = rand.Next(1, 41);
            while (!(lotto.Contains(check)))
            {

                lotto[i] = check;
                i++;
            }

            Console.WriteLine("slot " + i + " contains " + check);

        }
        Console.Read();
    }

更新:谢谢你想通了,用while替换了if :)

【问题讨论】:

  • 为什么 SU 上有 C# 标签?
  • 你的问题不是很清楚。你能详细说明你想要什么吗?
  • while (lotto.Contains(check)) check = rand.next(1, 41); lotto[i] = check; 应该这样做...
  • @PriscillaKorostchuk 这很容易实现,就在将新数字插入数组之前,检查它是否已经存在。如果是 - 跳过数字,并生成新的数字(确保您没有前进到数组中的下一个位置)。
  • 不应该是if (!lotto.Contains(check))!,我想?在 C# 中,! 表示“不是”。

标签: c# arrays random


【解决方案1】:

我猜你的问题是什么不起作用,我猜你忘记了一个 ! 并使用了一个未声明的变量 i

if (!lotto.Contains(check)) // Ensure the number has not been chosen
{
    lotto[count] = check; // Set the number to its correct place
    count=count+1
}

【讨论】:

    【解决方案2】:

    如果您想生成不重复的随机数,请使用经过 FIPS 140-2 验证的随机数生成器(请参阅http://www.hhs.gov/ocr/privacy/hipaa/administrative/securityrule/fips1402.pdf 第 36 页上的第 4.9.3 节)。

    如果这被用于任何严肃的游戏目的,正如您的变量命名所暗示的那样,我会推荐比Random 具有更好随机性的东西。

    【讨论】:

    • 这只是一个涵盖嵌套 for 循环的课堂实验,没什么太高级的..:)
    【解决方案3】:

    你可以试试这个:

    for (int i = 0; i < lotto.Length;) 
    {
      check = rand.Next(1, 41);
      Console.WriteLine("Random number to be checked is -> "+check);
    
      if (!lotto.Contains(check))
      {
         lotto[i] = check;
         i++;
      }
    
      Console.WriteLine("slot " + i + " contains " + check);
    }
    

    请注意,我已从 for 语句中删除 i++,并已放入 if 块中。

    您也可以尝试使用其他循环结构,但这是为了对您的代码进行最少的编辑。

    编辑:

    好吧,我尝试了代码,似乎对我有用。完整代码如下:

    int check = 0;
    
    int[] lotto = new int[6];
    
    Random rand = new Random();
    
    for (int i = 0; i < lotto.Length; )
    {
        check = rand.Next(1, 41);
        Console.WriteLine("Random number to be checked is -> " + check);
    
        if (!lotto.Contains(check))
        {
            lotto[i] = check;
            i++;
        }
    
        Console.WriteLine("slot " + i + " contains " + check);
    }
    
    Console.ReadKey();
    

    您也可以使用while 构造:

    int check = 0;
    
    int[] lotto = new int[6];
    
    Random rand = new Random();
    
    int i = 0;
    
    while (i < lotto.Length)
    {
        check = rand.Next(1, 41);
        Console.WriteLine("Random number to be checked is -> " + check);
    
        if (!lotto.Contains(check))
        {
            lotto[i] = check;
            i++;
        }
    
        Console.WriteLine("slot " + i + " contains " + check);
    }
    
    Console.ReadKey();
    

    基本上,这与前面的代码功能相同。

    【讨论】:

    • 我得到一个永无止境的循环,似乎没有超过插槽 1 及其编号:s
    • 我将生成命令移到 for 循环 (doh) 中,但它仍在插入数组中已经存在的数字...
    • 好吧,我试过了,似乎对我有用。
    猜你喜欢
    • 2015-07-12
    • 2018-07-03
    • 2016-05-05
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多