Shuffle an Array in C#

  1. Shuffle an Array With the Random Class in C
  2. Shuffle an Array With the RNGCryptoServiceProvider Class in C

This tutorial will discuss the methods to shuffle an array in C#.

Shuffle an Array With the Random Class in C

The Random class generates random numbers in C#. The Random.Next() method generates a random integer value. We can use the Random.Next() method with LINQ to shuffle an array in C#.

using System;
using System.Linq;
using System.Security.Cryptography;

namespace randomize_array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };
            Random random = new Random();
            arr = arr.OrderBy(x => random.Next()).ToArray();
            foreach (var i in arr)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Output:

3
4
5
1
2

In the above code, we shuffled the array of integers arr with the Random.Next() method and LINQ in C#. We first generated a random index with the Random.Next() method and placed each element at a random index with the OrderBy() method. We then converted the resultant data structure to an array with the ToArray() method.

Shuffle an Array With the RNGCryptoServiceProvider Class in C

The RNGCryptoServiceProvider class in C# generates random numbers. This method is more reliable than the previous approach because the RNGCryptoServiceProvider class is more random than the Random class. The RNGCryptoServiceProvider class is mainly used for encryption, so it is more secure than the Random class. The GetBytes() method of the RNGCryptoServiceProvider class is used to fill an array of bytes with a sequence of random values. We can use the Convert.ToInt32() method to convert this random byte value into an integer. We can then use this random integer as an index for each element. The following code example shows us how we can shuffle an array with the RNGCryptoServiceProvider class in C#.

using System;
using System.Linq;
using System.Security.Cryptography;

namespace randomize_array
{
    class Program
    {
        static int Next(RNGCryptoServiceProvider random)
        {
            byte[] randomInt = new byte[4];
            random.GetBytes(randomInt);
            return Convert.ToInt32(randomInt[0]);
        }
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };
            RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
            arr = arr.OrderBy(x => Next(random)).ToArray();
            foreach (var i in arr)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Output:

5
1
2
4
3

The logic followed in this example is the same as the previous approach. The difference is that here we are using the RNGCryptoServiceProvider class to generate a random index for our array. We defined the method Next() that generates a random integer index using the RNGCryptoServiceProvider class.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Array

  • Difference Between a Jagged Array and a Multi-Dimensional Array in C#
  • Remove Element of an Array in C#