English 中文(简体)
如何在C#中随机列出清单?
原标题:How to enumerate list items randomly in C#?
  • 时间:2010-11-01 20:05:22
  •  标签:
  • c#

我对各位大专家提出了一个具有挑战性的问题。 这在我的法典中还不是实际使用,而是来自我刚才的想法。

如果我有一个<代码>IList<T>,那么我如何执行一个随机抽取清单的计数人,这种数据可以同时由多个校对使用?

我之所以需要这样做,是因为我需要在<代码>List<SslStream>中向客户发送数据的内容because SslStream is not thread-safe 。 随机抽取一些要素(但确保我照亮them all)会减少24小时的冲突概率,并会改善I/O约束行动的业绩。

Please keep in mind that even if I told you why I need such an enumerator, I still like challenge. There can be other ways of sending the same data to multiple clients, but my question remains the same :) :)

最佳回答
问题回答

与此类似(显然需要生产):

class RandomList<T> : IEnumerable<T> {
     private readonly IList<T> list;
     private readonly Random rg;
     private readonly object sync = new Object();
     public RandomList(IList<T> list) : this(list, new Random()) { }

     public RandomList(IList<T> list, Random rg) {
         Contract.Requires<ArgumentNullException>(list != null);
         Contract.Requires<ArgumentNullException>(rg != null);
         this.list = list;
         this.rg = rg;
     }

     public IEnumerator<T> GetEnumerator() {
         List<int> indexes;
         // Random.Next is not guaranteed to be thread-safe
         lock (sync) {
             indexes = Enumerable
                 .Range(0, this.list.Count)
                 .OrderBy(x => this.rg.Next())
                 .ToList();
         }
         foreach (var index in indexes) {
             yield return this.list[index];
         }
     }
}
      IEnumerator IEnumerable.GetEnumerator() {
          return GetEnumerator();
      }
}

应:

public static IEnumerable<T> YieldRandom<T>(this IList<T> items) {
    Random random = new Random();
    HashSet<int> yielded = new HashSet<int>(items.Count);

    for (int i=0; i<items.Count; i++) {
        // find an index we haven t yielded yet
        int yieldIndex;
        do {
            yieldIndex = random.Next(items.Count);
        }
        while (yielded.Contains(yieldIndex));

        yielded.Add(yieldIndex);
        yield return items[yieldIndex];
    }
}

I m 确信可以在以下地点使用更多的准则:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签