UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RandomShuffle.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7
8namespace Algo
9{
15 template <typename RangeType>
16 void RandomShuffle(RangeType&& Range)
17 {
18 auto Data = GetData(Range);
19
20 using SizeType = decltype(GetNum(Range));
21 const SizeType Num = GetNum(Range);
22
23 for (SizeType Index = 0; Index < Num - 1; ++Index)
24 {
25 // Get a random integer in [Index, Num)
26 const SizeType RandomIndex = Index + (SizeType)FMath::RandHelper64(Num - Index);
27 if (RandomIndex != Index)
28 {
29 Swap(Data[Index], Data[RandomIndex]);
30 }
31 }
32 }
33}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
auto GetNum(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
Definition StringConv.h:808
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
Definition ParallelSort.h:13
void RandomShuffle(RangeType &&Range)
Definition RandomShuffle.h:16
U16 Index
Definition radfft.cpp:71
static UE_FORCEINLINE_HINT int64 RandHelper64(int64 A)
Definition UnrealMathUtility.h:281