2

I want to do is shuffle the array below every time I load my console application. For example batman could be next to name[1], name[2] or name[3] instead of 'name[0]' every time.

            heroes[] names = new heroes[4];

            names[0] = batman;              
            names[1] = ironman;             
            names[2] = hulk;
            names[3] = flash;

How to do it?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • You could potentially use a [Fisher-Yates Shuffle/Knuth Shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). [This](http://www.dotnetperls.com/fisher-yates-shuffle) is a C# implementation but there should be a couple around the place if you do a search.... – nkvu Apr 04 '13 at 18:02
  • Jared Butler, I've removed all "thank you notes"/"I'm learning" text from the post... but I don't see anything "specific" about your shuffling. Feel free to revert my changes if you like, but make sure to explain what "specific" way you need to shuffle elements. Otherwise it is duplicate of very popular question (above). – Alexei Levenkov Apr 04 '13 at 18:09

2 Answers2

3

Use list and this extension method:

public static class ListExtensions
{
    /// <summary>
    /// Shuffle algorithm as seen on page 32 in the book "Algorithms" (4th edition) by Robert Sedgewick
    /// </summary>
    public static void Shuffle<T>(this IList<T> source)
    {
        var n = source.Count;
        for (var i = 0; i < n; i++)
        {
            // Exchange a[i] with random element in a[i..n-1]
            var r = i + RandomProvider.Instance.Next(0, n - i);
            var temp = source[i];
            source[i] = source[r];
            source[r] = temp;
        }
    }
}

public static class RandomProvider
{
    [ThreadStatic]
    public static readonly Random Instance;

    static RandomProvider()
    {
        Instance = new Random();
    }
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
-2
        heroes[] names = new heroes[4];
        names[0] = batman;
        names[1] = ironman;
        names[2] = hulk;
        names[3] = flash;

        var rnd = new Random(DateTime.Now.Second);
        for (int i = 0; i < heroes.Length; i++)
        {
            names[i] = heroes[rnd.Next(0, heroes.Length - 1)];
        }

That should point you in the right direction.

Clint
  • 6,133
  • 2
  • 27
  • 48
  • 1
    This will have a high chance of including the same name twice and leaving one out. – Lee White Apr 04 '13 at 18:03
  • @KonradRudolph Please do expand, simple sweeping statements like that do nobody any good. – Clint Apr 04 '13 at 18:03
  • Oops, no, I see what I did :P – Clint Apr 04 '13 at 18:03
  • Removed my other comment - looking at this again, I don't know what this is doing. That second array names `heroes` is not defined, but this is either going to randomly duplicate names and leave names out, or is going to zero out the already assigned names. Hard to say as the heroes array/list is not defined. – pstrjds Apr 04 '13 at 18:17
  • For a playing card analogy, randomly choosing N cards but not removing them (what you're doing) is not the same as shuffling the deck and taking N cards. – Austin Salonen Apr 04 '13 at 18:58