Using C++ shuffle

Shuffling an array:


void AMyActor::ShuffleArray(TArray<FName>& myArray)
{
     if (myArray.Num() > 0)
     {
          int32 LastIndex = myArray.Num() - 1;
          for (int32 i = 0; i <= LastIndex; ++i)
          {
               int32 Index = FMath::RandRange(i, LastIndex);
               if (i != Index)
               {
                    myArray.Swap(i, Index);
               }
          }
     }
}

hope this helps

1 Like