]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Generate all permutations of the list with equal probability
authorJob Snijders <job@sobornost.net>
Tue, 25 Jun 2024 05:21:39 +0000 (05:21 +0000)
committerJob Snijders <job@sobornost.net>
Tue, 25 Jun 2024 05:21:39 +0000 (05:21 +0000)
@botovq was kind enough to point out that although my earlier
implementation produced random-ish ordering, it strictly speaking
wasn't Fisher-Yates.

We need to ensure `j` is a random number between `i` and `list.count`
see the second example in the 'Modern Algorithm'
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

src/object/manifest.c

index a50e2889d196989ea6f65f033a5f68d699acc60a..7f188e2e9c20acb309c34ea1099a73c1c0829788 100644 (file)
@@ -205,8 +205,8 @@ build_rpp(struct Manifest *mft, struct rpki_uri *notif,
 
        /* Fisher-Yates shuffle with modulo bias */
        srand(time(NULL) ^ getpid());
-       for (i = 0; i < mft->fileList.list.count; i++) {
-               j = rand() % mft->fileList.list.count;
+       for (i = 0; i < mft->fileList.list.count - 1; i++) {
+               j = i + rand() % (mft->fileList.list.count - i);
                tmpfah = mft->fileList.list.array[j];
                mft->fileList.list.array[j] = mft->fileList.list.array[i];
                mft->fileList.list.array[i] = tmpfah;