9

Possible Duplicate:
Randomize a List<T> in C#
shuffle (rearrange randomly) a List<string>
Random plot algorithm

Hi I have the following list and I want to output the model into a list but do so randomly. I have seen a few examples but they seem to be really convuluted. I just want a simple way to do this?

List<Car> garage ----randomise------> List<string> models


List<Car> garage = new List<Car>();

garage.Add(new Car("Citroen", "AX"));
garage.Add(new Car("Peugeot", "205"));
garage.Add(new Car("Volkswagen", "Golf"));
garage.Add(new Car("BMW", "320"));
garage.Add(new Car("Mercedes", "CLK"));
garage.Add(new Car("Audi", "A4"));
garage.Add(new Car("Ford", "Fiesta"));
garage.Add(new Car("Mini", "Cooper"));
Community
  • 1
  • 1
Peter Crouch
  • 103
  • 1
  • 1
  • 3
  • possible duplicate of [Randomize a List in C#](http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp) or [Shuffle List](http://stackoverflow.com/questions/2301015/shuffle-listt) or ... – Tim Schmelter Aug 29 '12 at 14:18

2 Answers2

12

I think all you want is this, it's a simple way to do it;

Random rand = new Random();
var models = garage.OrderBy(c => rand.Next()).Select(c => c.Model).ToList();

//Model is assuming that's the name of your property

Note : Random(), ironically, isn't actually very random but fine for a quick simple solution. There are better algorithms out there to do this, here's one to look at;

http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

saj
  • 4,626
  • 2
  • 26
  • 25
  • 2
    [Why does using Random in Sort causing [Unable to sort IComparer.Compare error](http://stackoverflow.com/questions/4129995/why-does-using-random-in-sort-causing-unable-to-sort-icomparer-compare-error) – L.B Aug 29 '12 at 14:24
  • @L.P The OP asked for a simple solution, and this is one, the link that you have supplied pretty much states what I have stated in my answer too. – saj Aug 29 '12 at 14:26
  • 2
    Also its complexity is `N*LogN` while it can be done in `N` – L.B Aug 29 '12 at 14:27
  • 1
    @saj Simple or no, it's a bad solution that shouldn't ever be used. Why propagate a terrible solution when a fantastic one is already so simple. – Servy Aug 29 '12 at 14:34
  • Thanks, this is exactly what I wanted. I have seen the other more efficient methods but performance isn't really an issue, the list won't get too much bigger than I have show and it doesn't need to be ABSOLUTELY random – Peter Crouch Aug 29 '12 at 14:34
  • 2
    @PeterCrouch the problem is not " true randomness". Above code may **never** finish in bad luck :) – L.B Aug 29 '12 at 14:35
  • 1
    So having code that might not finish, might throw exceptions, is much less likely to actually randomly sort the data, and takes longer, is worth saving you ~3 lines of code (given that any solution will still just be copy-pasted from somewhere else anyway). Sorry, but saving a few lines of code that I don't even have to write just isn't worth that for me, and I couldn't in good conscience suggest it to...anyone...for those reasons. Also remember that SO questions last; you're not just giving it to one person. This question may stay on SO for a while and be seen by other people. – Servy Aug 29 '12 at 14:47
  • @sery I disagree, despite its shortcomings, the OPs question seems like a good case to use it, if ever. I agree, that with a few more lines of code you could have a much better solution. In regards to leaving it here for others to see, that's fine as long as I specify the alternative, which I have done and as long as the shortcomings are highlighted, which we definitely have done with this discussion. So I have no problem leaving it here for those who wish to use it for those simple scenarios – saj Aug 29 '12 at 15:09
  • 1
    Bad idea, see comments [here](http://stackoverflow.com/a/3456788/207655). – o0'. Oct 10 '13 at 08:39
  • see also https://blogs.msdn.microsoft.com/ericlippert/2011/01/31/spot-the-defect-bad-comparisons-part-four/ why it can crash – marczellm May 26 '16 at 16:02
-1

I've seen some messy code for this pre-LINQ, but the LINQ way seems pretty clean.

Maybe give this a shot?

http://www.ookii.org/post/randomizing_a_list_with_linq.aspx

Random rnd = new Random();
var randomizedList = from item in list
                     orderby rnd.Next()
                     select item;
SuperNES
  • 2,760
  • 9
  • 37
  • 49
  • 1
    [Why does using Random in Sort causing [Unable to sort IComparer.Compare error](http://stackoverflow.com/questions/4129995/why-does-using-random-in-sort-causing-unable-to-sort-icomparer-compare-error) – L.B Aug 29 '12 at 14:25
  • Bad idea, see comments [here](http://stackoverflow.com/a/3456788/207655). – o0'. Oct 10 '13 at 08:40