【问题标题】:Random value without duplicates没有重复的随机值
【发布时间】:2023-03-19 10:18:01
【问题描述】:
Random r = new Random();
int randomvalue = r.Next(1,20);
*
*
if(randomvalue == 1) something
if(randomvalue == 2) something
*
*
if(randomvalue == 19) something

使该随机值不重复的最佳方法是什么?顺便说一句:它的 WPF,而不是控制台。

【问题讨论】:

标签: c# random int duplicates


【解决方案1】:

试试这个:

var rnd = new Random();

var shuffled =
    Enumerable
        .Range(1, 20)
        .OrderBy(x => rnd.Next())
        .ToArray();

【讨论】:

    【解决方案2】:

    // 这会起作用

    类程序 { 静态无效主要(字符串 [] 参数) {

            List<int> i = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20});
            List<int> r;
    
            r = ShuffleList(i);
    
    
        }
    
    
        private static List<E> ShuffleList<E>(List<E> unshuffledList)
        {
            List<E> sList = new List<E>();
    
            Random r = new Random();
            int randomIndex = 0;
            while (unshuffledList.Count > 0)
            {
                randomIndex = r.Next(0, unshuffledList.Count); 
                sList.Add(unshuffledList[randomIndex]);
                unshuffledList.RemoveAt(randomIndex); //remove so wont be added a second time
            }
    
            return sList; //return the new shuffled list
        }
    }
    

    【讨论】:

      【解决方案3】:

      不清楚重复是什么意思。如果您不想在一行中看到相同的数字,那么只需保留一个哈希表或Dictionary&lt;int,int&gt;,它会保留出现的 last 整数。然后检查下一个数字是否与最后一个数字相同。如果没有,请从字典中删除最后一个数字并放入当前数字。如果它们相同,则从Random 请求另一个随机整数。

      var myDictionary = new Dictionary<int,int>;
      var randomvalue = r.Next(1,20);
      while(myDictionary.ContainsKey(randomvalue))
      {
         randomvalue = r.Next(1,20);
      }
      myDictionary.Clear();
      myDictionary.Add(randomvalue, 123); //123 is just a number. Doesn't matter.
      

      这保证了两个相同的整数永远不会连续出现。

      注意::其他答案很有创意,但“了解您的数据结构”。不要使用列表查找。哈希是我们使用的。

      【讨论】:

      • 你能给我举个例子吗?
      • 请问我应该在哪里以及如何创建 myDictionary?
      • @PetrKonecny 我将其添加到我的答案中。
      【解决方案4】:

      您需要存储随机值是使其唯一的唯一方法。

      试试这个:

      Random r = new Random(); 
      public List<int> randomList = new List<int>();
      int randomvalue = 0;
      Public void newNumber()
      {
          randomvalue = r.Next(0, 20);
      
          if (!randomList.Contains(randomvalue))
          {
              randomList.Add(randomvalue);
              if(randomvalue == 1) 
                //do something
              if(randomvalue == N)
                // do something
          }
      }
      

      【讨论】:

      • 不行,还是不行。我不知道为什么,您的代码看起来应该可以工作。我不知道。
      【解决方案5】:
      using System;
      using System.Collections;
      using System.Collections.Generic;
      
      namespace SOFAcrobatics
      {
          public static class Launcher
          {
              public static void Main ()
              {
                  // 1 to 20 without duplicates
                  List<Int32> ns = new List<Int32>();
                  Random r = new Random();
                  Int32 ph = 0;
                  while (ns.Count < 20)
                  {
                      while (ns.Contains (ph = r.Next(1, 21))) {}
                      ns.Add(ph);
                  }
                  // ns is now populated with random unique numbers (1 to 20)
              }
          }
      }
      

      【讨论】:

        【解决方案6】:

        试试下面的方法:

        随机 randomInstance = new Random();

                List<int> NumList = new List<int>();
                NumList.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7,
                    8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
                    22, 23, 24, 25, 26, 27, 28, 29, 30 });
        
                int index = randomInstance.Next(0, NumList.Count - 1);
                int randomNumber = NumList[index];
                NumList.RemoveAt(index);
        

        【讨论】:

        • 快速填充列表的方法(需要 System.Linq)... NumList.AddRange(Enumerable.Range(startValue, endValue);
        • 如果期望的结果是每个数字只使用一次(例如,以随机顺序询问一堆测验问题),那么我认为这是最好的解决方案,因为它只需要 n 个循环。使用其他方法来跟踪已经使用的随机数将导致许多循环,这些循环什么都不做,只是试图找到一个未使用的随机数。虽然不太可能,但循环可能永远不会结束。
        • 完全标记。这是一种方法,其中您的列表可以包含连续数字或任何元素的列表
        • @Maddy - 如果您拨打电话.Next(0, NumList.Count - 1),那么您将永远无法获得列表中的最后一项。对.Next(...) 的调用中的第二个参数是独占 上限。
        猜你喜欢
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 2011-05-01
        • 1970-01-01
        相关资源
        最近更新 更多