Download

Need some clarification on random number generation in C++

Hi, everyone!

I was looking for a way to make some nice random numbers for my game project.
Using FMath::Rand() and it’s counterparts seems to be the easiest way to get random numbers in UE4.

But I looked at the implementation and I found out that what it does is just calling rand() from the standard C library. Well rand() isn’t that bad and it’s OK for many cases (including mine) but it also has limitations. It’s biggest problem is that RAND_MAX constant is usually too small and it’s platform-dependent. Rand is also not the fastest RNG in the world.

Making my own RNG would not be hard, I could use mt19937 from C++11 if I needed to. But I just wonder if there is another way in the engine to create random numbers that uses better approach, or I should just stick with FMath::Rand()? :slight_smile: