你的问题有两个部分。首先,你必须对你的集合进行洗牌,以便随机选择项目。要进行洗牌,你可以使用 Fisher-Yates 洗牌算法来正确地进行,或者使用伪随机生成器来对你的项目进行排序。
Fisher-Yates 洗牌算法来源于这个流行答案:
public static IList<T> Shuffle<T>(this IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
return list;
}
然而,我返回列表以便我们可以在参与过程中干净地使用它。此外,如果您不需要干净地洗牌,则可以使用一个简单的
OrderBy
与
i => random.Next()
或
i => Guid.NewGuid()
作为lambda表达式。
其次,一旦它被洗牌,现在您需要取一定比例的项目。您可以通过使用
Take
LINQ方法来轻松完成此操作。
像Shuffle方法一样,您可以将其制作为扩展方法:
public static IEnumerable<int> TakePercentage(this IList<int> list, int percentage)
{
return list.Take(percentage * list.Count / 100);
}
如果您希望直接获得十进制数值(例如0.3):
public static IEnumerable<int> TakePercentage(this IList<int> list, double percentage)
{
return list.Take((int)(percentage * list.Count));
}
最后,要使用它非常简单:
var thirtyPercent = myCollection.Shuffle().Take(30);