I have this piece of code that gets a random number from the count of a list (-1) and then gets the element at that index. I then remove the object from that list and the code is called until all objects are gone from that list.
I'm a bit confused as to how there can be 13 indexes (0-12) and the integer I'm using to get the element at is 11. How can this be out of range of the valid values?
private Player GetRandomPlayer(List<Player> entries)
{
var rIdx = rnd.Next(entries.Count - 1);
var player = entries.Where(i => i.Seed == null).ElementAt(rIdx);
entries.Remove(player);
return player;
}
Images of exceptions and test case:
List with 13 values: https://ibb.co/wQdq1q4
Exception including int used to get a value:https://ibb.co/mJXwMFh