# GenericArray\_Shuffle is biased

**URL:** <https://forums.unrealengine.com/t/genericarray-shuffle-is-biased/401658>\
**Category:** C++\
**Tags:** unreal-engine, bug-report, question, CPP\
**Created:** [August 14, 2017, 9:16pm UTC](https://forums.unrealengine.com/t/genericarray-shuffle-is-biased/401658 "2017-08-14T21:16:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wakebrdkid](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wakebrdkid](https://forums.unrealengine.com/u/wakebrdkid)\
**Post date:** [August 14, 2017, 9:16pm UTC](https://forums.unrealengine.com/t/genericarray-shuffle-is-biased/401658/1 "2017-08-14T21:16:52Z")

</div>

The code in [GenericArray\_Shuffle()](https://github.com/EpicGames/UnrealEngine/blob/1d2c1e48bf49836a4fee1465be87ab3f27d5ae3a/Engine/Source/Runtime/Engine/Private/KismetArrayLibrary.cpp#L176) contains a common implementation error for the [Fisher–Yates shuffle algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). 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](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#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](https://trinket.io/python/82fc34ded9)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [August 15, 2017, 2:40pm UTC](https://forums.unrealengine.com/t/genericarray-shuffle-is-biased/401658/2 "2017-08-15T14:40:19Z")

</div>

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)](https://issues.unrealengine.com/issue/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

---

<div class="post-metadata">

**Author:** ![wakebrdkid](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wakebrdkid](https://forums.unrealengine.com/u/wakebrdkid)\
**Post date:** [August 15, 2017, 2:57pm UTC](https://forums.unrealengine.com/t/genericarray-shuffle-is-biased/401658/3 "2017-08-15T14:57:38Z")

</div>

Cool. Thanks.
