Click here to Skip to main content
Click here to Skip to main content

Let's randomize IEnumerable

, 9 Oct 2011
Rate this:
Please Sign up or sign in to vote.
I needed to randomize a collection. Here's my solution.
I needed to randomize an enumerable collection. Here's what I came up with in the 60 seconds I thought it deserved.
 
Your challenge:
  1. Do it better. better = faster with same number of items and/or faster with n items.
     
  2. Prove it.
using System;
using System.Collections.Generic;
 
/// <summary>
/// Extension class for IEnumerable&lt;T&gt;
/// </summary>
static class IEnumerableExtension
{
    /// <summary>
    /// Randomizes the specified collection.
    /// </summary>
    /// <typeparam name="T">The type of the collection.</typeparam>
    /// <param name="collection">The collection.</param>
    /// <returns>The randomized collection</returns>
    public static IEnumerable<T> Randomize<T>(this IEnumerable<T> collection)
    {
        // Put all items into a list.
        var list = new List<T>(collection);
        var randomizer = new Random();
        // And pluck them out randomly.
        for (int i = list.Count; i > 0; i--)
        {
            int r = randomizer.Next(0, i);
            yield return list[r];
            list.RemoveAt(r);
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Yvan Rodrigues
President Red Cell Innovation Inc.
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the manufacturing sector. He runs Red Cell Innovation Inc./L'innovation de Globules Rouges, a consulting company focusing on efficiency and automation of manufacturing and business processes for small businesses, healthcare, and the public sector. He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).
 
Yvan draws on experience at Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.
 
Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, MonoTouch, and Mono for Android.
 
Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.
Follow on   Google+   LinkedIn

Comments and Discussions

 
GeneralRe: Yes, but it would be algorithmically better. You're essentia... PinmemberHenry.Ayoola13-Oct-11 0:12 
GeneralRe: The OP's code is O(n^2) unless List has some really sophisti... PinmemberHenry.Ayoola13-Oct-11 0:11 
GeneralRe: Good points! The problem that both solutions face is that b... PinmemberYvan Rodrigues11-Oct-11 4:21 
GeneralReason for my vote of 4 Simple idea but efficacious! PinmemberSandroBoz10-Oct-11 21:43 
Generalhttp://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Th... PinmemberHenry.Ayoola10-Oct-11 3:03 
GeneralRe: I did exactly what the OP did before I discovered the Fisher... PinmemberWjousts11-Oct-11 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 9 Oct 2011
Article Copyright 2011 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid