Random int64 still broken in 5.1

Anything beyond max int32 will return -2147483648
The issue I found is the use of TruncToInt instead of TruncToInt64 inside RandHelper64


int64 Max = MAX_int64;
int64 Random = FMath::RandHelper64(Max);
UE_LOG(LogTemp, Warning, TEXT("%lld"), Random);

If we apply the fix like so the issue is resolved.

//TruncToInt64 used
Random = FMath::Min(FMath::TruncToInt64(FMath::FRand() * (float)Max), Max - 1);
UE_LOG(LogTemp, Warning, TEXT("Fixed %lld"), Random);

You can see the second line is how it should be.
image

I’ve seen pull request been made about this but I don’t know if there’s something else to it.

2 Likes

I’m unreal 5.2 version.

FMath::RandRange function for int64 value also occurs problem like that. This function use RandHelper64 function internally.

These are coded for int64 only, but not work :sweat_smile:

1 Like