Tuesday, July 20, 2010

Suffle a List and Find the index of Item

int index = infoList.IndexOf(item);

public class Util
{
public static List RandomPermutation(List list)
{
List retList = new List(list);
//list.CopyTo(retList.ToArray(), 0);

Random random = new Random();
for (int i = 0; i < list.Count; i++)
{
int swapIndex = random.Next(i, list.Count);
if (swapIndex != i)
{
T temp = retList[i];
retList[i] = retList[swapIndex];
retList[swapIndex] = temp;
}
}
return retList;
}
}

No comments: