/V4_12_MyProject_1

Unreal Engine 4 / Plugin / Blueprint nodes / Linear congruential random number generators

Primary LanguageC++

V4_12_MyProject_1

A Linear Congruential Random Number Generator (LCG) generates an integer number n_i using the following formula:

 n_i = (a * n_(i-1) + b) modulo m

For the next call to the LCG, n_i is used in the above formula to generate n_(i+1) and so on... The initial value n_0 used by the LCG to generate n_1 (and so on...) is called the seed of the random number generator.

When b=0, LCGs are called Multiplicative LCGs (MLCGs).

This project is an Unreal Engine 4.12.5 basic C++ project where a new Blueprint C++ Plugin is created; this Plugin provides the following Linear Congruential Random Number Generators blueprint nodes:

  1. Derrick Henry Lehmer's LCG (1949): a=23, b=0, and m=10^8 + 1,
  2. Standard minimal LCG (from NASA, 1982): a=16807, b=0 and m=2147483647,
  3. A much better LCG (from L'Ecuyer, 1988): a=40692, b=0, and m=2147483399,
  4. A combined LCG (from L'Ecuyer, 1988): a1=40014, b1=0, m1=2147483563, a2=40692, b2= 0, and m2=2147483399,
  5. C88's LCG: a=1103515245, b=12345 and m=32768,
  6. Visual C/C++ LCG: a=214013, b=2531011 and m=2147483648.

LCG 2. is available from the C++11 Standard Library as std::minstd_rand0() in <random>.

Use 3. if you want better randomness than 1., 2., and even 5. and 6.

Use 4. if you want an even better randomness then 3. (bottom line: 4. is the best RNG provided by this Plugin).

LCGs 5. and 6. exactly implement the rand() function from C88 and Visual C/C++, respectively.

The Math/Random blueprint nodes provided by the UE4 uses 6.

Random seeds are automatically generated from time/1; but nodes are provided so that you can seed the LCGs yourself.

Seeds of MLCGs should not be equal to 0 as (0 modulo m) returns 0, thus generating an endless series of 0.

But couldn't be 0 generated at some point, even when the seed isn't equal to 0? No because a and m are carefully chosen so that this can never happen (i.e. chosen such that there is no integer k such that a * x == k * m with x in the closed range [1,m-1]).

The current implementation of the LCGs is trivial as it is meant to be pedagogical; a further branch will provide more efficient implementations.

In the Blueprint editor window of UE4, move the mouse over the blueprint nodes to get the documentation.

All Plugin files, including sources, are provided. Nothing has been removed from the Unreal Project. Open the Blueprint level to find an example and all the nodes provided by the Plugin.

Do anything you want with it and, if you have time, please keep me posted. 😃

You can use this Plugin in a specific project (copy the Plugin in the "Plugins" directory of the specific project) or as an UE4 Plugin for any of your projects (copy the Plugin in UE4's "Plugins" directory).

If you can read french, step by step description of the making of this Plugin with both UE4 and Visual Studio 2015 can be found here. This step by step description starts with the creation of a blank C++ UE4 project, continues with the creation of a blank Blueprint Library Plugin and then extends the C++ code of the Blueprint Library Plugin automatically generated by UE4 until it provides the random number generators listed above.

If you cannot read french, sorry then, but an english translation will be available soon.

-- The AI Hedgehog.

Img