From 6dd9daf07417dc4a6e221d448d319b6d2058cb58 Mon Sep 17 00:00:00 2001 From: user1823 <92206575+user1823@users.noreply.github.com> Date: Mon, 1 Sep 2025 09:52:27 +0530 Subject: [PATCH] Increase randomness in random sorting of new cards (#4286) * Increase randomness in random sorting of new cards Currently, the new cards appear roughly in the same order on consecutive days (if they are skipped by burying). This change aims to increase randomness by spreading out the salt across the hash space. * Fix errors --- .../src/scheduler/queue/builder/gathering.rs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/rslib/src/scheduler/queue/builder/gathering.rs b/rslib/src/scheduler/queue/builder/gathering.rs index fb6274de5..293b50dc4 100644 --- a/rslib/src/scheduler/queue/builder/gathering.rs +++ b/rslib/src/scheduler/queue/builder/gathering.rs @@ -61,28 +61,26 @@ impl QueueBuilder { } fn gather_new_cards(&mut self, col: &mut Collection) -> Result<()> { + let salt = Self::knuth_salt(self.context.timing.days_elapsed); match self.context.sort_options.new_gather_priority { NewCardGatherPriority::Deck => { self.gather_new_cards_by_deck(col, NewCardSorting::LowestPosition) } - NewCardGatherPriority::DeckThenRandomNotes => self.gather_new_cards_by_deck( - col, - NewCardSorting::RandomNotes(self.context.timing.days_elapsed), - ), + NewCardGatherPriority::DeckThenRandomNotes => { + self.gather_new_cards_by_deck(col, NewCardSorting::RandomNotes(salt)) + } NewCardGatherPriority::LowestPosition => { self.gather_new_cards_sorted(col, NewCardSorting::LowestPosition) } NewCardGatherPriority::HighestPosition => { self.gather_new_cards_sorted(col, NewCardSorting::HighestPosition) } - NewCardGatherPriority::RandomNotes => self.gather_new_cards_sorted( - col, - NewCardSorting::RandomNotes(self.context.timing.days_elapsed), - ), - NewCardGatherPriority::RandomCards => self.gather_new_cards_sorted( - col, - NewCardSorting::RandomCards(self.context.timing.days_elapsed), - ), + NewCardGatherPriority::RandomNotes => { + self.gather_new_cards_sorted(col, NewCardSorting::RandomNotes(salt)) + } + NewCardGatherPriority::RandomCards => { + self.gather_new_cards_sorted(col, NewCardSorting::RandomCards(salt)) + } } } @@ -169,4 +167,10 @@ impl QueueBuilder { true } } + + // Generates a salt for use with fnvhash. Useful to increase randomness + // when the base salt is a small integer. + fn knuth_salt(base_salt: u32) -> u32 { + base_salt.wrapping_mul(2654435761) + } }