[PATCH] D21218: [LibFuzzer] Avoid using std::random_swap() due to platform differences and implement our own version.

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 10 18:36:35 PDT 2016


delcypher added inline comments.

================
Comment at: lib/Fuzzer/FuzzerInternal.h:145
@@ +144,3 @@
+  //
+  // The algorithm used here will pick a permutation at
+  // random where every permutation has equal probability
----------------
aizatsky wrote:
> Specify algorithm reference.
This algorithm is based on what is in libc++ but the loop iteration goes forwards rather than backwards. 

================
Comment at: lib/Fuzzer/FuzzerInternal.h:152
@@ +151,3 @@
+    N = End - First;
+    for (Offset = 0; Offset < N; ++Offset) {
+      std::swap(First[Offset], First[Offset + (*this)(N - Offset)]);
----------------
aizatsky wrote:
> Let's see that this is literally Fisher-Yates shuffle:
> 
> https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
> 
> Even (N-2) is important to have it produce uniform shuffles. 
> Let's see that this is literally Fisher-Yates shuffle:
> https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

I did not know this algorithm had a name. My version is basically the `Durstenfeld` implementation shuffling from lowest index to highest. This had made me realize there is a mistake in my implementation. My implementation loops one too many time as it will try to swap the last element with itself.

> Even (N-2) is important to have it produce uniform shuffles.

I did not know that. I didn't see that explicitly stated in the wikipedia article. Is that a solution to the modulo bias problem it mentions?


http://reviews.llvm.org/D21218





More information about the llvm-commits mailing list