Mega Code Archive

 
Categories / C# / Development Class
 

Randomises elements in List

using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for ObjectUtils /// </summary> namespace UTSDAL.LINQSQL.Utils {     public class ObjectUtils     {         /// <summary>         /// wraps predefined objects with desired attributes and provides them to calling instances         /// </summary>         public ObjectUtils()         {         }         /// <summary>         /// randomises elements in List         /// </summary>         /// <typeparam name="T"></typeparam>         /// <param name="list"></param>         /// <returns></returns>         public static List<T> Randomize<T>(List<T> list)         {             List<T> randomizedList = new List<T>();             Random rnd = new Random();             while (list.Count > 0)             {                 int index = rnd.Next(0, list.Count); //pick a random item from the master list                 randomizedList.Add(list[index]); //place it at the end of the randomized list                 list.RemoveAt(index);             }             return randomizedList;         }     } }