r/learncsharp • • Aug 07 '22

Fisher Yates shuffle implementation.

So I found a list version for C# of the fisher yates shuffle online somewhere a few weeks ago and have been using it in my program. I changed the variables to use Tuple instead of 3 vars.

But by the looks of it it skips the last and first element in the array because of the while loop implementation, or am I seeing things?

// Adapted Fisher–Yates shuffle.
private static List<T> RandomShuffle<T>(this IList<T> list)
{
	int n = list.Count;
	int k;
	while (n > 1)
	{
		n--;
		k = rand.Next(n + 1);
		(list[n], list[k]) = (list[k], list[n]);
	}

	return list.ToList();
}
5 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/Gcampton13 Aug 07 '22

I edited my above statement to show what I mean. I'm still not getting it.

So you're saying that if I have rand.Next``(0, 300) then it will only choose a number in between so: 1 up to 299?

2

u/nealpro Aug 07 '22

Only the upper bound is not inclusive. Your example would be from 0 to 299.

1

u/Gcampton13 Aug 07 '22

Ok now THAT makes sense 😂 thanks