Click here to Skip to main content
11,581,402 members (61,924 online)
Click here to Skip to main content

Tagged as

Let's randomize IEnumerable

, 12 Oct 2011 CPOL 4K 1
Rate this:
Please Sign up or sign in to vote.
I was actually going to suggest a simpler version of cechode's version, but along the same lines.public static IEnumerable Randomize(this IEnumerable source){ Random r = new Random(); return source.Select(x => new { Key = r.Next(), Value = x }) .OrderBy(x => x.Key) ...
I was actually going to suggest a simpler version of cechode's version, but along the same lines.
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
{
  Random r = new Random();
 
  return source.Select(x => new { Key = r.Next(), Value = x })
   .OrderBy(x => x.Key)
   .Select(x => x.Value);
}

Edit: In response to http://blogs.msdn.com/b/ericlippert/archive/2011/01/31/spot-the-defect-bad-comparisons-part-four.aspx[^], I've updated it to generate a single random number to sort upon.

License

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

Share

About the Author

Andrew Rissing
Software Developer (Senior)
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 1 Random numbers should never be fed i... Pin
Qwertie12-Oct-11 8:39
memberQwertie12-Oct-11 8:39 
GeneralRe: Updated :) It is essentially now what cechode put forth, bu... Pin
Andrew Rissing12-Oct-11 9:00
memberAndrew Rissing12-Oct-11 9:00 
GeneralRe: Sometimes I feel that being a programmer is much like being ... Pin
Kabwla.Phone13-Oct-11 22:54
memberKabwla.Phone13-Oct-11 22:54 
GeneralReason for my vote of 5 As promissed, a better vote! Pin
Kabwla.Phone12-Oct-11 3:54
memberKabwla.Phone12-Oct-11 3:54 
GeneralRe: ;-) Pin
Andrew Rissing12-Oct-11 4:01
memberAndrew Rissing12-Oct-11 4:01 
GeneralReason for my vote of 5 Useful, i think about the same idea. Pin
Ubloobok12-Oct-11 1:23
memberUbloobok12-Oct-11 1:23 
GeneralReason for my vote of 3 Can you do a performance test to see... Pin
Kabwla.Phone11-Oct-11 22:04
memberKabwla.Phone11-Oct-11 22:04 
GeneralRe: There is no need to run performance testing against the two.... Pin
Andrew Rissing12-Oct-11 3:47
memberAndrew Rissing12-Oct-11 3:47 
GeneralRe: Sorry, initially read your comment as referring to Cechode's... Pin
Andrew Rissing12-Oct-11 3:53
memberAndrew Rissing12-Oct-11 3:53 
GeneralReason for my vote of 5 i like it better than mine Pin
cechode11-Oct-11 5:04
membercechode11-Oct-11 5:04 

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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 12 Oct 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid