在开发中遇到了要将显示商品随机显示的需求,试用了很多的算法,发现该算法效率最高,写下了希望对大家有用。
?
/// <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;
 ??????????? }
 ??????? }
