Click here to Skip to main content
12,630,420 members (31,808 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.7K views
2 bookmarked
Posted

Let's randomize IEnumerable

, 9 Oct 2011 CPOL
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)

Share

About the Author

Yvan Rodrigues
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., 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), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

You may also be interested in...

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 9 Oct 2011
Article Copyright 2011 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid