How does UE4 generate random numbers in C++ & Blueprints?

Hey Guys,

Hope someone can help me out here. How is UE4 Generating it’s random numbers? Is it using C++11-style generators as standard or is it using custom number generator code from Epic?

Im also guessing that random number generation in blueprints is just interfaced to C++ code, Is this correct. Also where in the source code IS UE4’s random number generator code? I need to be able to find and replace UE4’s number generator code and methods with my own PCG random number generation code.

Cheers

James

It’s not hard to find :stuck_out_tongue:

rand.c has the pseudo random number generator in it.

and FMath both have random number functions (Rand(), RandRange(), etc.).

Probably a better bet than using rand.c in Unreal Games.

Unreal engine’s Rand(); uses rand(); so there is no difference in using rand() or Rand(), and for that matter all of the Random number functions use Rand() some where so, yes they all use c++ standard rand();

True, but there’s no guarantee that that will always be the case. For sake of portability and to take advantage of any improvements Epic might decide to make in the future (say, swapping in a better PRNG rand(), or auto-selecting a faster one when you build for mobile), I’d still argue that it makes sense to use the UE4 engine functions rather than standard library calls. That would also give you the option to change how you calculate random numbers in your game by simply patching one method in the engine rather than having to chase down every place you’ve called random in your app (which is, potentially, a lot of places).

Hey Guys, Yeah i looked through source/core/and found randomstream.h which uses FMath Rand (which im guessing is used with the blueprint RNG stream node, have to look closer). Now just need to track down where unreal’s code that defines epic Rand is rand standard C++11 si i can tie into the PCG random generator code. I think that’s the only way not to have to go through all code that references Rand and change to PCG tie in.

Found this great bit of code for High quality random number generation that TRULY is random unlike the current random generator that is no way random. The new PCG code is also about 25% faster and more memory efficient. If you want it goto http://www.pcg-random.org/ and download the C++ library. If anyone wants to help replace the current random number generator code with this keep posting your results on this thread. Cheers

James

Yep. All random numbers in UE4 use rand() by default, which is defined in rand.c :slight_smile:

I’ve been using the FRandomStream class.

FRandomStream also uses rand()


	/**
	 * Generates a new random seed.
	 */
	void GenerateNewSeed( )
	{
		Initialize(FMath::Rand());
	}