shuffle an array in csharp

main.cs
//create random object
Random rng = new Random();

//array to shuffle
int[] arr = {1, 2, 3, 4, 5};

//fisher yates shuffle algorithm
for (int i = arr.Length - 1; i > 0; i--)
{
    int j = rng.Next(i + 1);
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

//print shuffled array
Console.WriteLine(string.Join(", ", arr));
337 chars
18 lines

The code above shuffles an array of integers using the Fisher-Yates algorithm, which randomly swaps each element of the array with another element.

gistlibby LogSnag