From: Job Snijders Date: Tue, 25 Jun 2024 05:21:39 +0000 (+0000) Subject: Generate all permutations of the list with equal probability X-Git-Tag: 1.6.3~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b72fe8a2bddc5134c821bbe58ef35e50a22cf1e;p=thirdparty%2FFORT-validator.git Generate all permutations of the list with equal probability @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 --- diff --git a/src/object/manifest.c b/src/object/manifest.c index a50e2889..7f188e2e 100644 --- a/src/object/manifest.c +++ b/src/object/manifest.c @@ -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;