gpt4 book ai didi

c# - 从 C# 中的 List 中选择 N 个随机元素

转载 作者:IT王子 更新时间:2023-10-29 03:31:49 24 4
gpt4 key购买 nike

我需要一个快速算法来从通用列表中选择 5 个随机元素。例如,我想从 List<string> 中获取 5 个随机元素.

最佳答案

使用 linq:

YourList.OrderBy(x => rnd.Next()).Take(5)

关于c# - 从 C# 中的 List<T> 中选择 N 个随机元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com