如何使用C#从列表中选择随机对象?

时间:2013-11-07 12:50:34

标签: c# linq

我有一个包含超过300个对象的IQueryable:

public class Detail
{
    public int Id { get; set; }
    public int CityId { get; set; }
    public bool Chosen { get; set; }
}

IQueryable<Detail> details = ...

我如何反对这个随机挑出50个对象?我假设我需要使用.ToList()转换它,但我不确定如何挑选随机元素。

6 个答案:

答案 0 :(得分:10)

300不是很多,所以是的,把它变成一个List:

IQueryable<Detail> details = ...
IList<Detail> detailList = details.ToList();

现在你可以选择一个随机项目:

var randomItem = detailList[rand.Next(detailList.Count)];

你可以重复50次。然而,这将导致重复,消除它们的过程将变得混乱。

所以使用standard shuffle algorithm然后选择前50个:

Shuffle(detailList);
var selection = detailList.Take(50);

答案 1 :(得分:3)

如果您事先知道可以随机选择的项目总数,则可以在不先转换为列表的情况下进行。

以下方法将为您完成:

/// <summary>Randomly selects items from a sequence.</summary>
/// <typeparam name="T">The type of the items in the sequence.</typeparam>
/// <param name="sequence">The sequence from which to randomly select items.</param>
/// <param name="count">The number of items to randomly select from the sequence.</param>
/// <param name="sequenceLength">The number of items in the sequence among which to randomly select.</param>
/// <param name="rng">The random number generator to use.</param>
/// <returns>A sequence of randomly selected items.</returns>
/// <remarks>This is an O(N) algorithm (N is the sequence length).</remarks>

public static IEnumerable<T> RandomlySelectedItems<T>(IEnumerable<T> sequence, int count, int sequenceLength, System.Random rng)
{
    if (sequence == null)
    {
        throw new ArgumentNullException("sequence");
    }

    if (count < 0 || count > sequenceLength)
    {
        throw new ArgumentOutOfRangeException("count", count, "count must be between 0 and sequenceLength");
    }

    if (rng == null)
    {
        throw new ArgumentNullException("rng");
    }

    int available = sequenceLength;
    int remaining = count;
    var iterator  = sequence.GetEnumerator();

    for (int current = 0; current < sequenceLength; ++current)
    {
        iterator.MoveNext();

        if (rng.NextDouble() < remaining/(double)available)
        {
            yield return iterator.Current;
            --remaining;
        }

        --available;
    }
}

(这里的关键是需要在开始时知道可供选择的项目数量;这确实会减少实用程序。但是如果计数很快并且缓冲所有项目会占用太多内存,这是一个有用的解决方案。)


这是另一种使用Reservoir sampling

的方法

这种方法不需要知道可供选择的项目总数,但它确实需要缓冲输出。当然,它还需要枚举整个输入集合。

因此,当您事先不知道可供选择的项目数量(或者可供选择的项目数量非常大)时,这实际上只是有用。

我建议只按照Henk的答案洗牌,而不是这样做,但我为了感兴趣而把它包括在这里:

// n is the number of items to randomly choose.

public static List<T> RandomlyChooseItems<T>(IEnumerable<T> items, int n, Random rng)
{
    var result = new List<T>(n);
    int index = 0;

    foreach (var item in items)
    {
        if (index < n)
        {
            result.Add(item);
        }
        else
        {
            int r = rng.Next(0, index + 1);

            if (r < n)
                result[r] = item;
        }

        ++index;
    }

    return result;
}

作为Henk答案的附录,这是他提到的Shuffle算法的规范实现。在此,_rngRandom的实例:

/// <summary>Shuffles the specified array.</summary>
/// <typeparam name="T">The type of the array elements.</typeparam>
/// <param name="array">The array to shuffle.</param>

public void Shuffle<T>(IList<T> array)
{
    for (int n = array.Count; n > 1;)
    {
        int k = _rng.Next(n);
        --n;
        T temp = array[n];
        array[n] = array[k];
        array[k] = temp;
    }
}

答案 2 :(得分:3)

Random rnd = new Random();
IQueryable<Detail> details = myList.OrderBy(x => rnd.Next()).Take(50);

答案 3 :(得分:1)

IQueryable<Detail> details = myList.OrderBy(x => Guid.NewGuid()).ToList();

在此之后,只需线性地浏览它:

var item1 = details[0];

这样可以避免重复。

答案 4 :(得分:1)

var l = new List<string>();
l.Add("A");
l.Add("B");
l.Add("C");
l.Add("D");
l.Add("E");
l.Add("F");
l.Add("G");
l.Add("H");
l.Add("I");

var random = new Random();
var nl = l.Select(i=> new {Value=i,Index = random.Next()});

var finalList = nl.OrderBy(i=>i.Index).Take(3);
foreach(var i in finalList)
{
    Console.WriteLine(i.Value);
}

答案 5 :(得分:0)

这就是最终对我有用的东西,它确保不返回任何重复项:

public List<T> GetRandomItems(List<T> items, int count = 3)
{
    var length = items.Count();
    var list = new List<T>();
    var rnd = new Random();
    var seed = 0;

    while (list.Count() < count)
    {
        seed = rnd.Next(0, length);
        if(!list.Contains(items[seed]))
            list.Add(items[seed]);
    }

    return list;
}