65.9K
CodeProject is changing. Read more.
Home

Adding Random Functionality to Generic Lists using Extension Methods

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.40/5 (3 votes)

Apr 28, 2009

CPOL

2 min read

viewsIcon

25071

downloadIcon

170

Extends IList to include a shuffle method and a 'select an element at random' method.

Introduction

The .NET Framework provides a vast array of functionality for manipulating lists. However one of the requirements that frequently crops up is having the ability to reorder a list into a random sequence or select a random element from a list. Both are relatively straightforward and can be achieved effectively with a couple of extension methods on IList<T> meaning they'll be available for use on any List<T> or ObservableCollection<T> as well as any classes that derive from them.

Note: This article has been updated, scroll to the bottom to see the history.

Selecting a Random Element

A method to select an element at random can be implemented using an extension method but must be on a static class.

Things to note are that Random.Next returns a value between 0 and list.Count – 1, i.e. less than the maximum specified (separately, I actually have an extension method for Random that can return a value using inclusive parameter values). The statement default(T) is used because value types cannot be set to null (think int as opposed to int?).

private static Random random = new Random();
 
public static T GetRandom<T>(this IList<T> list)
{
  if (list.Count == 0)
  {
    return default(T);
  }
  return list[random.Next(0, list.Count)];
}

Reordering a List

Now we have a flavour for Random how about manipulating the elements within a list. Again an extension method provides a neat way to do this:

public static void Shuffle <T>(this IList <T> list) 
{ 
  if (list.Count <= 1) 
  { 
    return; // nothing to do 
  } 
  
  for (int i = 0; i < list.Count; i++) 
  { 
    int newIndex = random.Next(0, list.Count); 
    
    // swap the two elements over 
    T x = list[i]; 
    list[i] = list[newIndex]; 
    list[newIndex] = x; 
  }
}

This works by looping over each element in the list and swapping it with another element at a randomly selected position. Note that like many things, this code will work fine when lists are reasonably small but it is outside the scope of this article to discuss optimisations for manipulating lists.

Conclusion

These methods are just two examples of how lists can be extended to include functionality that produces randomised output and by putting them onto a base generic interface that allows for reuse over a great many classes.

History

  • Version 1.0
    • Initial release
  • Version 1.1
    • Updated shuffle algorithm to be properly random
    • Improved sample