2 Replies - 1917 Views - Last Post: 13 September 2012 - 10:19 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Randomize a Generic List

Posted 17 August 2009 - 08:54 AM

Description: Need .Net Framework 3.5 or higher
Need reference to System.Collections.Generic NamespaceSnippet to randomize a Generic list of type T

/// <summary>
/// method for randomizing a generic list of type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">the list we wish to randomize</param>
/// <returns></returns>
public static List<T> RandomizeGenericList<T>(IList<T> originalList)
{
    List<T> randomList = new List<T>();
    Random random = new Random();
    T value = default(T);

    //now loop through all the values in the list
    while (originalList.Count() > 0)
    {
        //pick a random item from th original list
        var nextIndex = random.Next(0, originalList.Count());
        //get the value for that random index
        value = originalList[nextIndex];
        //add item to the new randomized list
        randomList.Add(value);
        //remove value from original list (prevents
        //getting duplicates
        originalList.RemoveAt(nextIndex);
    }

    //return the randomized list
    return randomList;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Randomize a Generic List

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Randomize a Generic List

Posted 17 August 2009 - 08:54 AM

Description: Need .Net Framework 3.5 or higher
Need reference to System.Collections.Generic NamespaceSnippet to randomize a Generic list of type T

/// <summary>
/// method for randomizing a generic list of type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">the list we wish to randomize</param>
/// <returns></returns>
public static List<T> RandomizeGenericList<T>(IList<T> originalList)
{
    List<T> randomList = new List<T>();
    Random random = new Random();
    T value = default(T);

    //now loop through all the values in the list
    while (originalList.Count > 0)
    {
        //pick a random item from th original list
        var nextIndex = random.Next(0, originalList.Count);
        //get the value for that random index
        value = originalList[nextIndex];
        //add item to the new randomized list
        randomList.Add(value);
        //remove value from original list (prevents
        //getting duplicates
        originalList.RemoveAt(nextIndex);
    }

    //return the randomized list
    return randomList;
}

Was This Post Helpful? 0
  • +
  • -

#3 Flycatcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-September 11

Re: Randomize a Generic List

Posted 14 September 2011 - 03:57 AM

One small error: all the .Count() statements should read just .Count (it's a property not a method).
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Randomize a Generic List

Posted 13 September 2012 - 10:19 AM

Not true, Count() is a method. http://msdn.microsof...5(v=vs.90).aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1