发布网友 发布时间:2024-10-24 03:31
共2个回答
热心网友 时间:2024-11-06 16:07
输入:列表 输出:随机元素public T GetRandomItem(IList list){ if (list == null) throw new ArgumentNullException("list"); int count = list.Count; if (count == 0) return default(T); int index = new Random().Next(count); return lis..
热心网友 时间:2024-11-06 16:12
int[] array = {1, 2, 3};//按照楼主所说的3个数
Dictionary<int, int> extract = new Dictionary<int, int>();//记录每个数抽取的次数
for (int i = 0; i < array.Length; i++)
{
extract.Add(array[i], 0);
}
Random ran = new Random();
List<int> list = new List<int>();//装载抽取出来的数
for (int i = 0; i < 抽取次数; i++)
{
ExtractData:
int j = ran.Next(0, array.Length);
if (extract[array[j]] < 10)
{
extract[array[j]]++;
list.Add(array[j]);
}
else
{
goto ExtractData;
}
}