Skip to content

Fix biased random sort by using Fisher-Yates shuffle#1131

Merged
bpatrik merged 1 commit intobpatrik:masterfrom
infinitejones:fix/fisher-yates-shuffle
Feb 8, 2026
Merged

Fix biased random sort by using Fisher-Yates shuffle#1131
bpatrik merged 1 commit intobpatrik:masterfrom
infinitejones:fix/fisher-yates-shuffle

Conversation

@infinitejones
Copy link
Contributor

Summary

  • The random sort option used Array.sort() with a random comparator, which is a well-known antipattern that produces biased results — items tend to stay near their original (alphabetically sorted) position
  • This is especially noticeable when filenames share a common prefix (e.g. France - beach, France - city, Italy - beach, Italy - city), as the alphabetical pre-sort clusters them and the biased shuffle preserves those clusters
  • Replaced with Fisher-Yates shuffle for both media and directories, which guarantees a uniform random permutation
  • The existing seeded PRNG is still used, so the shuffle remains deterministic within a page load

Test plan

  • Select random sorting on a gallery with many similarly-named files and verify items are well-mixed
  • Verify the shuffle is stable within a page load (navigating away and back gives the same order)
  • Verify directory random sorting also works correctly

🤖 Generated with Claude Code

The previous random sorting used a random comparator with Array.sort(),
which is a well-known antipattern that produces biased results. Items
tend to stay near their original (alphabetically sorted) position,
causing media with similar filenames to cluster together rather than
being uniformly shuffled.

Replace with Fisher-Yates shuffle for both media and directories,
which guarantees a uniform random permutation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bpatrik
Copy link
Owner

bpatrik commented Feb 8, 2026

thanks!

@bpatrik bpatrik closed this Feb 8, 2026
@bpatrik bpatrik reopened this Feb 8, 2026
@bpatrik bpatrik merged commit 6f132b5 into bpatrik:master Feb 8, 2026
8 of 10 checks passed
@bpatrik bpatrik added this to the Next (probably v4.0) milestone Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants