Ayukawa Ayukawa -4 years ago 44
C# Question

Randomly shuffling an array

What I need to do is randomly shuffle an array of 25 numbers

int[] arr = Enumerable.Range(0, 24).ToArray();


So that it still has all the numbers from 0 to 24 but in a random order.
What would be the simplest way to do it?

@edit;

namespace WindowsFormsApp1


{

public partial class Form1 : Form
{
Random rnd = new Random();
int[] arr = Enumerable.Range(0, 24).OrderBy(c => rnd.Next()).ToArray();


public Form1()
{
InitializeComponent();
}
}
}

Answer Source

You can OrderBy(c => rnd.Next()) like this

Random rnd = new Random();
int[] arr = Enumerable.Range(0, 24).OrderBy(c => rnd.Next()).ToArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download