【问题标题】:Picking a random string from a list with no replacement C#从没有替换 C# 的列表中选择一个随机字符串
【发布时间】:2016-11-26 12:22:37
【问题描述】:

我有一个字符串列表,我想从这个列表中随机选择。当一个字符串被选中时,它必须从列表中移除。只有当所有字符串都被选中后,列表才会重新填充。我如何做到这一点?

【问题讨论】:

  • 随机播放列表,然后逐个使用字符串。

标签: c# list random


【解决方案1】:

首先,制作一个随机数生成器。

Random rand = new Random();

然后,生成一个数字并从列表中删除该项目。我假设您使用的是System.Collections.Generic.List

int num = Random.Next(list.Count);
list.RemoveAt(num);

【讨论】:

    【解决方案2】:

    您可以非常简单地实现它:

      public static partial class EnumerableExtensions {
        public static IEnumerable<T> RandomItemNoReplacement<T>(this IEnumerable<T> source, 
          Random random) {
    
          if (null == source)
            throw new ArgumentNullException("source");
          else if (null == random)
            throw new ArgumentNullException("random");
    
          List<T> buffer = new List<T>(source);
    
          if (buffer.Count <= 0)
            throw new ArgumentOutOfRangeException("source");
    
          List<T> urn = new List<T>(buffer.Count);
    
          while (true) {
            urn.AddRange(buffer);
    
            while (urn.Any()) {
              int index = random.Next(urn.Count);
    
              yield return urn[index];
    
              urn.RemoveAt(index);
            }
          }
        }
      }
    

    然后使用它:

    private static Random gen = new Random();    
    
    ...
    
    var result = new List<string>() {"A", "B", "C", "D"}
      .RandomItemNoReplacement(gen)
      .Take(10);
    
    // D, C, B, A, C, A, B, D, A, C (seed == 123)  
    Console.Write(string.Join(", ", result));
    

    【讨论】:

      【解决方案3】:

      我想,你需要像下一节课一样思考:

      public class RandomlyPickedWord
          {
              public IList<string> SourceWords{ get; set; }
      
              protected IList<string> Words{ get; set; }
      
              public RandomlyPickedWord(IList<string> sourceWords)
              {
                  SourceWords = sourceWords;
              }
      
              protected IList<string> RepopulateWords(IList<string> sources)
              {
                  Random randomizer = new Random ();
                  IList<string> returnedValue;
                  returnedValue = new List<string> ();
      
                  for (int i = 0; i != sources.Count; i++)
                  {
                      returnedValue.Add (sources [randomizer.Next (sources.Count - 1)]);
                  }
      
                  return returnedValue;
              }
      
              public string GetPickedWord()
              {
                  Random randomizer = new Random ();
                  int curIndex;
                  string returnedValue;
      
                  if ((Words == null) || (Words.Any () == false))
                  {
                      Words = RepopulateWords (SourceWords);
                  }
      
                  curIndex = randomizer.Next (Words.Count);
                  returnedValue = Words [curIndex];
                  Words.RemoveAt (curIndex);
      
                  return returnedValue;
              }
          }
      

      接下来你应该使用哪个:

      IList<string> source = new List<string> ();
                  source.Add ("aaa");
                  source.Add ("bbb");
                  source.Add ("ccc");
                  RandomlyPickedWord rndPickedWord = new RandomlyPickedWord (source);
      
                  for (int i = 0; i != 6; i++)
                  {
                      Console.WriteLine (rndPickedWord.GetPickedWord ());
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        • 2021-07-20
        • 2011-07-19
        相关资源
        最近更新 更多