diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2021-02-07 14:10:19 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2021-02-07 14:10:19 -0800 |
| commit | 86c5d0c1506c5bb8a938dec22fc673d35a449bfc (patch) | |
| tree | 19ee6391a038444da5efa99100df99117e1ff2d3 | |
| parent | 2755cbc869f153f631ad89d003bea811006da485 (diff) | |
Stabilized randomization so it doesn't reshuffle when reloading the same
dir (but it still will if the dir contents change)
| -rw-r--r-- | bb.c | 8 |
1 files changed, 7 insertions, 1 deletions
@@ -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; } |
