【问题标题】:Create Random User algorithm in c# [closed]在 C# 中创建随机用户算法 [关闭]
【发布时间】:2013-11-22 11:19:12
【问题描述】:

我想根据文件夹中的照片数量创建用户。

例如:

user.1 random(4)[photos1-4]
dosomething(user.1)

user.2 random(6)[photos5-10]
dosomething(user.2)

user.3 random(3)[photos11-13]
dosomething(user.3)

user.last [photos.leftover]
dosomething(user.last)

关于如何做到这一点的想法?

【问题讨论】:

  • 问题目前完全不清楚。请说清楚
  • 不清楚是轻描淡写

标签: c# algorithm random


【解决方案1】:

执行此操作的最佳方法是加载您的工作列表,随机化列表,然后将列表放入某种形式的队列中,以供最终工作人员提取。

private BlockingCollection<string> GetWorkSoruce()
{
    List<string> sourceList = GetListOfFiles(); //How you get your list is up to you.

    Shuffle(sourceList); //see http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp

    //Create a thread safe queue that many consumers can pull from.
    var collection = BlockingCollection<string>(new ConcurrentQueue<string>(sourceList));
    collection.CompleteAdding();

    return collection;
}

现在您的每个工作人员(用户)都可以退出队列并获得工作要做。由于队列是 ConcurrentQueue,因此您可以让来自多个线程的多个工作人员同时工作。

private void WorkerDoWork(BlockingCollection<string> workSource, int itemsToTake)
{
    foreach(var imagePath in workSource.GetConsumingEnumerable().Take(itemsToTake))
    {
        ProcessImage(imagePath);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2014-12-01
    • 1970-01-01
    • 2015-07-28
    • 2020-12-22
    • 2014-01-15
    相关资源
    最近更新 更多