Skip to content

Engine: Make FisherYatesShuffle actually do a Fisher-Yates shuffle#32

Merged
Fayti1703 merged 2 commits intotrunkfrom
correctness/shuffle
Feb 20, 2025
Merged

Engine: Make FisherYatesShuffle actually do a Fisher-Yates shuffle#32
Fayti1703 merged 2 commits intotrunkfrom
correctness/shuffle

Conversation

@Fayti1703
Copy link
Owner

To quote Wikipedia page on the Fisher-Yates shuffle1:

For example, a common off-by-one error would be choosing the index j of the entry to swap […] to be always strictly less than the index i of the entry it will be swapped with. This turns the Fisher–Yates shuffle into Sattolo's algorithm, which produces only permutations consisting of a single cycle involving all elements: in particular, with this modification, no element of the array can ever end up in its original position.

rng.Next(i) only produces values up to i exclusive, so the previous code runs into precisely the situation described above.

With thanks to @samualtnorman for helping discover this.

Footnotes

  1. Wikipedia contributors. Accessed 2025-02-20 UTC.

@Fayti1703 Fayti1703 added correctness incorrect behavior z-project: engine something in the engine itself z-project: tests something in the test suite labels Feb 20, 2025
@Fayti1703 Fayti1703 merged commit fe748da into trunk Feb 20, 2025
1 check passed
@Fayti1703 Fayti1703 deleted the correctness/shuffle branch February 20, 2025 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

correctness incorrect behavior z-project: engine something in the engine itself z-project: tests something in the test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant