This crashes the editor like 8 times out of 10
int32 UFL::RandomIntFromTable(TMap<int32, int32> Table)
{
if (Table.Num() < 2) return *Table.Find(0);
int32 totalRand = 0;
TMap<int32, int32> LowerWeights;
for (auto &Elem : Table)
{
LowerWeights.Add(Elem.Key,totalRand);
totalRand += Elem.Value;
}
int32 rand = FMath::RandRange(0, totalRand);
int32 top = 0;
for (auto &Elem : LowerWeights)
{
top += *Table.Find(Elem.Key);
if (rand < top)
return Elem.Key;
}
return 0;
}
How do I fix this?