2

What I came up with is:

RandomSort()
{
  string[] list = { "Alpha", "Beta", "Gamma", ... }
  Random rnd = new Random();
  string[] list2 = list.OrderBy((x) => rnd.NextDouble()).ToArray();
}

Is there some other way, maybe using a Dictionary or something? Thanks.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • 3
    Exact duplicate of [this question](http://stackoverflow.com/q/108819/128397), where the best answer is actually not the accepted answer but [this one](http://stackoverflow.com/questions/108819/best-way-to-randomize-a-string-array-in-c/110570#110570) which has C# code for a Fisher-Yates Shuffle aka Knuth Shuffle implementation. – Daniel Pryden Jan 30 '11 at 01:11
  • I did a search for random sort and it did not show up. Thanks for the link. – John Alexiou Jan 30 '11 at 02:01
  • 1
    Note that if you are shuffling for the purposes of, say, producing a deck of cards to play online poker, then this is not sufficiently random. "Random" is only pseudo-random; it is an easy exercise to predict the order of all the other cards given a small sample of a pseudo-random shuffled deck. If you need truly unpredictable randomness then use a better randomness algorithm, like the crypto algorithms. – Eric Lippert Jan 30 '11 at 02:59

1 Answers1

6

That certainly is simple but it's O(n log(n)). You can do better performance-wise by using the Fisher Yates shuffle.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452