Problem of the Day
A new programming or logic puzzle every Mon-Fri

Array Shuffle

Given an array of integers create a function to shuffle the numbers in a random way. Thus, given the same input a different shuffled result should be possible. For bonus points solve this problem without using extra space.

Permalink: http://problemotd.com/problem/Array-shuffle/

Comments:

  • Anonymous - 10 years, 1 month ago

    C# implementation using generics:

    private static void ArrayShuffle<T>(T[] array, long shuffleCount = 100)
        {
            var rng = new Random();
    
            for (long i = 0; i < shuffleCount; i++)
            {
                int n = rng.Next(array.Count());
                int m = rng.Next(array.Count());
    
                var tempItem = array[n];
                array[n] = array[m];
                array[m] = tempItem;
            }
        }
    

    reply permalink

Content curated by @MaxBurstein