【问题标题】:Foreach list search without repetition [duplicate]Foreach列表搜索不重复[重复]
【发布时间】:2017-01-15 14:37:54
【问题描述】:

如何将随机数字放入列表而不重复? 这是我的代码,有时数字重复但我不知道为什么

Random losowa = new Random();
List<int> pula = new List<int>();

private void LosujPytania()
{            
    int a = losowa.Next(1,20);
    while (pula.Count < 10)
    {
        foreach (int i in pula)
        {
            if (a == i)
            {
                a = losowa.Next(1, 20);
                break;
            }

        }
        pula.Add(a);
    }
}

【问题讨论】:

  • 每次调用Next 时都会生成一个介于 1 到 20 之间的数字。不能保证它们是唯一的。你可以看看 Fisher-Yates shuffle 并尝试实施它。 dotnetperls.com/fisher-yates-shuffle

标签: c# list search foreach repeat


【解决方案1】:

下面的代码创建不重复的数字列表。解决问题的关键是使用list.Contains()

    using System;
    using System.Collections.Generic;

    public class Program
    {
        public static void Main()
        {
            var list = new List<int>();             
            var rand = new Random();

            while(list.Count <10)
            {
                var number = rand.Next(1,20);

                if(! list.Contains(number))
                    list.Add(number);               
            }

            foreach(var item in list)           
                Console.WriteLine(item);  
        }
    }

【讨论】:

    【解决方案2】:

    您应该检查此号码是否以前生成过,如果重复,则生成另一个号码。

    改变这个:

    private void LosujPytania()
    {            
        int a = losowa.Next(1,20);
        while (pula.Count < 10)
        {
            foreach (int i in pula)
            {
                if (a == i)
                {
                    a = losowa.Next(1, 20);
                    break;
                }
            }
            pula.Add(a);
        }
    }
    

    通过这个:

    private void LosujPytania()
    {            
        int a = losowa.Next(1,20);
        pula.Add(a);
        while (pula.Count < 10)
        {
            do
            {
                a = losowa.Next(1, 20);
            } while(pula.Contains(a));
    
            pula.Add(a);
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以这样做,您可以查看我的 cmets 其他详细信息:

       private static void LosujPytania()
                  {         
      
              Random losowa = new Random();
              List<int> pula = new List<int>();
      
                      int a = losowa.Next(1,20);
                      while (pula.Count < 10)
                      {
                          //Your code is not really checking for duplicates so I replace it with boolean condition below
                          //foreach (int i in pula)
                          //{
                          //    if (a == i)
                          //    {
                          //        a = losowa.Next(1, 20);
                          //        break;
                          //    }
      
                          //}
      
                          a = losowa.Next(1, 20);
                          //This will check if your list doesn't contain your numbers yet before adding to make sure everything is unique
                          if (!pula.Contains(a))
                          pula.Add(a);
                      }
              }
      

      【讨论】:

        【解决方案4】:

        如果你想要没有重复的数字, 我认为使用HashSet会更好。

                    Random losowa = new Random();
                    HashSet<int> pula = new HashSet<int>();
                    while (pula.Count < 10)
                    {
                        pula.Add(r.Next(20));
                    }
        

        或者如果你真的需要列表,你可以使用辅助方法 类似:

          private void LosujPytania()
            {
                Random losowa = new Random();
                List<int> pula = new List<int>();
                int a = losowa.Next(1, 20);
                pula.AddRange(Get10RandomNumbers(losowa));
            }
        
            private IEnumerable<int> Get10RandomNumbers(Random losowa)
            {
                HashSet<int> ints = new HashSet<int>();   
                while (ints.Count < 10)
                {
                    ints.Add(losowa.Next(20));
                }
                return ints;
        
            }
        

        【讨论】:

          【解决方案5】:

          一个简单的 2 行解决方案:

          var rnd = new Random();
          var list = Enumerable.Range(0, 20).OrderBy(x => rnd.Next()).Take(10).ToList();
          

          解释:

          Enumerable.Range(0, 20) 将返回一个带有数字 0 到 19 的 IEnumerable&lt;int&gt;

          OrderBy(x =&gt; rnd.Next()) 会将值按随机顺序排序。

          Take(10) 将返回 IEnumerable&lt;int&gt; 的前 10 个数字,

          最后,ToList() 将返回这些 int 值的列表。

          【讨论】:

            猜你喜欢
            • 2014-11-22
            • 2019-05-25
            • 2015-08-28
            • 1970-01-01
            • 2014-04-21
            • 1970-01-01
            • 2014-09-17
            • 2015-07-25
            • 2014-12-14
            相关资源
            最近更新 更多