Is there a performance difference between these two algorithms for shuffling an IEnumerable?

后端 未结 3 537
借酒劲吻你
借酒劲吻你 2020-12-18 09:02

These two questions give similar algorthims for shuffling an IEnumerable:

  • C#: Is using Random and OrderBy a good shuffle algorithm?
  • Can you enumerate
3条回答
  •  旧巷少年郎
    2020-12-18 09:22

    The first doesn't compile, although it's apparent that you're trying to reify the enumerable, and then implement Fisher-Yates; that's probably the correct approach, and it shouldn't be unclear to anyone who has ever shuffled an array before. The second using RemoveAt is bad for the reasons stated by other commenters.

    EDIT: Your top implementation looks like it's correct now, and it's a good way to do it.

提交回复
热议问题