How to shuffle or randomize a generic list using C#

I have been working on question paper website in which I need to shuffle the options of multiple choice questions, all options are stored in a generic list. So I have written a function which will shuffle the list of option with out affecting the original list.

/// <summary>
/// Generates a shuffled list without affecting original list.
/// </summary>
/// <typeparam name="T">I am using structure</typeparam>
/// <param name="list">list to be shuffled</param>
/// <returns></returns>
public List<t> Shuffle<t>(List<t> list)


List<t> shuffledList = new List<t>();
//make a list of the index for list
System.Collections.ArrayList indexes = new System.Collections.ArrayList(list.Count);
for (int iCounter = 0; iCounter < rnd =" new"> 0)

//get a random index from the list of index

int index = rnd.Next(0, indexes.Count);

//pick the object from list based on index value stored in list of indexes


//remove the selected index from list of indexes



return shuffledList;


For questions or further explanation please ask in comment box.

Subscribe for our monthly newsletter for updated articles and useful code scripts.

Share It

comments powered by Disqus