GenericArray_Shuffle is biased

The code in GenericArray_Shuffle() contains a common implementation error for the Fisher–Yates shuffle algorithm. This leads to some orderings being returned more frequently than others. It’s actually described in the Wikipedia article in paragraph two of the section titled Implementation errors. It’s very easy to fix. Just change the line:

int32 Index = FMath::RandRange(0, LastIndex);

to:

int32 Index = FMath::RandRange(i, LastIndex);

With the current implementation, if you are shuffling the list {1, 2, 3, 4, 5}, then the result {2, 1, 4, 5, 3} will be returned about three times as often as {5, 1, 2, 3, 4}, for example. Here is an in-browser Python snippet if you want to quickly verify or experiment with the logic: Your Python Trinket

Cool. Thanks.

Hey -

Thank you for submitting a bug report. I have reproduced this issue and logged a report for it here Unreal Engine Issues and Bug Tracker (UE-48432) . You can track the report’s status as the issue is reviewed by our development staff. Please be aware that this issue may not be prioritized or fixed soon.

Cheer