How to shuffle an array ?

Sofi0813

Member
Joined
Jul 25, 2021
Messages
7
Programming Experience
1-3
// I need help C# VS 2019. I am really new!!!
// I want to shuffle the array to get (for example; 0,3,2,1 or 1,3,0,2 etc...) and use the value on my labels, when I click on ButtonNext.
//How to shuffle an array? Lets say the updated array is array2: This means every time I click Next button I am getting four integers in different labels.
C#:
            int[] array = { 0, 1, 2, 3};  //List of 4 integers.
        
           
            private void ButtonNext_Click(object sender, EventArgs e) // my next button in Windows Form Application.
                {
                    lblAns1.Text = array2[0]; //This label should get the first position value from the shuffled-array
                    lblAns2.Text = array2[1]; //This label should get the second position value from the shuffled-array
                    lblAns3.Text = array2[2]; //This label should get the 3rd position value from the shuffled-array
                    lblAns4.Text = array2[3]; //This label should get the four position value from the shuffled-array
                }
 
Last edited by a moderator:
This is the simplest option, as long as the level of randomness produced is sufficient for your purposes:
C#:
var rng = new Random();

var shuffledArray = originalArray.OrderBy(e => rng.NextDouble()).ToArray();
If you want to shuffle the existing array in place then you can do this:
C#:
var rng = new Random();
var keys = originalArray.Select(e => rng.NextDouble()).ToArray();

Array.Sort(keys, originalArray);
 
// I need help C# VS 2019. I am really new!!!
// I want to shuffle the array to get (for example; 0,3,2,1 or 1,3,0,2 etc...) and use the value on my labels, when I click on ButtonNext.
//How to shuffle an array? Lets say the updated array is array2: This means every time I click Next button I am getting four integers in different labels.
C#:
            int[] array = { 0, 1, 2, 3};  //List of 4 integers.
       
          
            private void ButtonNext_Click(object sender, EventArgs e) // my next button in Windows Form Application.
                {
                    lblAns1.Text = array2[0]; //This label should get the first position value from the shuffled-array
                    lblAns2.Text = array2[1]; //This label should get the second position value from the shuffled-array
                    lblAns3.Text = array2[2]; //This label should get the 3rd position value from the shuffled-array
                    lblAns4.Text = array2[3]; //This label should get the four position value from the shuffled-array
                }
You can use this logic:


Array Shuffle in C#:
using System;

public class Program
{
    public static void Main()
    { 
        string[] list = { "1", "2", "9", "4", "5" };
        
        /* You can use either for loop for while loop */
        for (int i = list.Length - 1; i > 0; i--)
        {
            Random random = new Random();
             int randomIndex = random.Next(0, i + 1);
             string temp = list[i];
             list[i] = list[randomIndex];
             list[randomIndex] = temp;
        }
        
        /* Below lines is just to print to console */
        var result = string.Join(",", list);
        Console.WriteLine(result);
    }
}

Source : C# Array Shuffle
 
Yes, that is the Fisher-Yates shuffling algorithm.

There is an issue with the code where a new random number generator is created each time through the loop. But the loop will run fast enough that the generator will always use the same seed number. The only reason why it looks random is because the i + 1 reduces each iteration so the generated number modulo i + 1 becomes a different index. The best thing to do is to move line 12 to before line 10.
 
Back
Top Bottom