2

How can I take a List and order it by random?

List< Testimonial > testimonials = new List< Testimonial >();
testimonials.Add(new Testimonial {1} ); 
testimonials.Add(new Testimonial {2} );
testimonials.Add(new Testimonial {2} ); 
testimonials.Add(new Testimonial {3} );
testimonials.Add(new Testimonial {4} );

How would I use

testimonials.OrderBy<>

in order to make it random?

brenjt
  • 15,997
  • 13
  • 77
  • 118

3 Answers3

8

Here is the solution for that.

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;
}

Source Link: http://www.dreamincode.net/code/snippet4233.htm

This Method will randomize any Generic list in C#

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
  • 2
    You should include the relevant part of the solution here with the link being a reference. – ChrisF Jun 09 '11 at 14:47
  • This worked perfect. No issues I'm going to attach it as a helper so I can use it for other things in my project. – brenjt Jun 09 '11 at 15:01
5
var random = new Random(unchecked((int) (DateTime.Now.Ticks));

var randomList = testimonials.OrderBy(t => random.Next(100));
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Can I ask why you changed it from testimonials.Lenth to 100? – brenjt Jun 09 '11 at 14:49
  • @brenjt - So that the LINQ query didn't have to continually evaluate testimonials.Length (depending on the type of collection, that might cause poor performance). – Justin Niessner Jun 09 '11 at 15:00
  • 1
    This wont actually be random. Need to do a seed. If you try this. Stop the instance, then start again you will find you get the same order. It may even repeat each time you call the method. I generally have some function that gets me a seed based on Datetime.Now. – Chad Jun 09 '11 at 15:00
  • @Chad - Updated. The seed method obviously isn't the best...but it'll work. And it actually uses OrderBy like the OP wanted. – Justin Niessner Jun 09 '11 at 15:08
  • @Justin - Still doesn't return a random order. It looks like it would work but I'm a Novice :D – brenjt Jun 09 '11 at 15:12
  • @brenjt - Is it not giving any random order or picking the same 'random' order each time? – Justin Niessner Jun 09 '11 at 15:15
  • It's not giving any random order. – brenjt Jun 09 '11 at 15:16
  • 1
    @brenjt - I just realized that I never specified that you have to assign the result of OrderBy to another collection (it doesn't create the random order in place) to see the results. I've tested and am getting random order each time. – Justin Niessner Jun 09 '11 at 15:18
  • @justin yes the seed now will do just fine it will recur occasionally but will give better results with no obvious repeats for a single client – Chad Jun 09 '11 at 15:21
  • @Justin - Oh of coarse, that make perfect sense. Now it works perfectly! – brenjt Jun 09 '11 at 15:24
1

Justin Niessner's solution is simple and effective (and the solution I would have given), but it's NlogN complexity. If performance is a big deal, you can shuffle a list in linear time using a Fischer-Yates-Durstenfeld shuffle. Have a look at this SO question: An extension method on IEnumerable needed for shuffling

Community
  • 1
  • 1
KeithS
  • 70,210
  • 21
  • 112
  • 164