获取Dictionary中不重复的随机数(返回Dictionary):
public static Dictionary
{
System.Random rand = new System.Random();
Dictionary
int size = allDic.Count;
randomCount = randomCount > size ? size : randomCount;
List
while (newDic.Count < randomCount)
{
TKey tk = values[rand.Next(size)];
if (!newDic.Keys.Contains(tk))
{
newDic[tk] = allDic[tk];
}
}
return newDic;
}
获取List中不重复的随机数(返回List):
public static List
{
System.Random rand = new System.Random();
List
int size = allList.Count;
randomCount = randomCount > size ? size : randomCount;
//List
while (newList.Count < randomCount)
{
TKey tk = allList[rand.Next(size)];
if (!newList.Contains(tk))
{
newList.Add(tk);
}
}
return newList;
}