Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KaosSpectrum/520b1521c898801c21f7ba3f90d70fcc to your computer and use it in GitHub Desktop.
Save KaosSpectrum/520b1521c898801c21f7ba3f90d70fcc to your computer and use it in GitHub Desktop.
template <class T>
void ShuffleArray(T& InArray)
{
const int32 LastIndex = InArray.Num() - 1;
for (int32 i = 0; i < LastIndex; ++i)
{
int32 Index = FMath::RandRange(0, LastIndex);
if (i != Index)
{
InArray.Swap(i, Index);
}
}
}
template <class T>
void ShuffleArray(T& InArray, const FRandomStream& InRandomStream)
{
const int32 LastIndex = InArray.Num() - 1;
for (int32 i = 0; i < LastIndex; ++i)
{
int32 Index = InRandomStream.RandRange(0, LastIndex);
if (i != Index)
{
InArray.Swap(i, Index);
}
}
}
template <class T>
void ShuffleArray(T& InArray, int32 Seed)
{
const FRandomStream Stream{Seed};
const int32 LastIndex = InArray.Num() - 1;
for (int32 i = 0; i < LastIndex; ++i)
{
int32 Index = Stream.RandRange(0, LastIndex);
if (i != Index)
{
InArray.Swap(i, Index);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment