Start Debugging
2023-10-26 Updated 2023-11-01 csharpdotnetdotnet-8

C# – How to shuffle an array?

The easiest way to shuffle an array in C# is using Random.Shuffle, introduced in .NET 8. It works in-place on both arrays and spans.

The easiest way to shuffle an array in C# is using Random.Shuffle. This method has been introduced in .NET 8 and works both with arrays and spans.

The shuffling is done in-place (the existing array/span is modified, as opposed to creating a new one and leaving the source unchanged).

In terms of signatures, we’ve got:

public void Shuffle<T> (Span<T> values);
public void Shuffle<T> (T[] values);

And for a simple usage example:

int[] foo = [1, 2, 3];
Random.Shared.Shuffle(foo); // [2, 1, 3]
< Back