aboutsummaryrefslogtreecommitdiff
path: root/bb.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-02-07 14:10:19 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-02-07 14:10:19 -0800
commit86c5d0c1506c5bb8a938dec22fc673d35a449bfc (patch)
tree19ee6391a038444da5efa99100df99117e1ff2d3 /bb.c
parent2755cbc869f153f631ad89d003bea811006da485 (diff)
Stabilized randomization so it doesn't reshuffle when reloading the same
dir (but it still will if the dir contents change)
Diffstat (limited to 'bb.c')
-rw-r--r--bb.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/bb.c b/bb.c
index da5f423..24b9515 100644
--- a/bb.c
+++ b/bb.c
@@ -533,8 +533,14 @@ static int populate_files(bb_t *bb, const char *path)
globfree(&globbuf);
}
+ // RNG is seeded with a hash of all the inodes in the current dir
+ // This hash algorithm is based on Python's frozenset hashing
+ unsigned long seed = (unsigned long)bb->nfiles * 1927868237UL;
+ for (int i = 0; i < bb->nfiles; i++)
+ seed ^= ((bb->files[i]->info.st_ino ^ 89869747UL) ^ (bb->files[i]->info.st_ino << 16)) * 3644798167UL;
+ srand((unsigned int)seed);
for (int i = 0; i < bb->nfiles; i++) {
- int j = rand() / (RAND_MAX / (i + 1)); // This is not optimal, but doesn't need to be
+ int j = rand() % (i+1); // This introduces some RNG bias, but it's not important here
bb->files[i]->shufflepos = bb->files[j]->shufflepos;
bb->files[j]->shufflepos = i;
}