Was looking for shuffling an array from within Unity, remembered that .NET 8 has Random.Shuffle.
It turns out that the implementation is the Fisher-Yates algorithm.
But when looking at it and at Wikipedia page, they're not 100% identical.
public void Shuffle<T>(Span<T> values)
{
int n = values.Length;
for (int i = 0; i < n - 1; i++)
{
int j = Next(i, n);
if (j != i)
{
T temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
Wikipedia, "inside-out" variant:
To initialize an array a of n elements to a randomly shuffled copy of source, both 0-based:
for i from 0 to n − 1 do
j ← random integer such that 0 ≤ j ≤ i
if j ≠ i
a[i] ← a[j]
a[j] ← source[i]
The .NET version does everything within the if block, not Wikipedia's.
Question:
Which one of these is right and can you explain why it is?
<not<=.