C#List随机交换取数据(C#实现的洗牌算法)

C#实现的洗牌算法

 

 

 

/// <summary>
/// 洗牌算法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listtemp"></param>
public void Reshuffle<T>(List<T> listtemp)
{
//随机交换
Random ram = new Random();
int currentIndex;
T tempValue;
for (int i = 0; i < listtemp.Count; i++)
{
currentIndex = ram.Next(0, listtemp.Count - i);
tempValue = listtemp[currentIndex];
listtemp[currentIndex] = listtemp[listtemp.Count - 1 - i];
listtemp[listtemp.Count - 1 - i] = tempValue;
}
}


你可能感兴趣的:(C#)