score:2

Accepted answer

since the original list can be modified, here is a very simple and efficient solution, based on this answer:

public static ienumerable<t> shuffle<t>(this ilist<t> list, random rng)
{
    for(int i = list.count - 1; i >= 0; i--)
    {
        int swapindex = rng.next(i + 1);
        yield return list[swapindex];
        list[swapindex] = list[i];
    }
}

Related Query

More Query from same tag