Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created February 14, 2024 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiarheiPilat/81ff652cabef10bc4829b50fa73fee6b to your computer and use it in GitHub Desktop.
Save SiarheiPilat/81ff652cabef10bc4829b50fa73fee6b to your computer and use it in GitHub Desktop.
public static T[] Shuffle<T>(T[] array)
{
T[] shuffledArray = new T[array.Length];
Array.Copy(array, shuffledArray, array.Length);
System.Random random = new System.Random();
for (int i = shuffledArray.Length - 1; i > 0; i--)
{
int j = random.Next(0, i + 1);
T temp = shuffledArray[i];
shuffledArray[i] = shuffledArray[j];
shuffledArray[j] = temp;
}
return shuffledArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment