Programming‎ > ‎

Random

Random numbers are often needed in games. They are created by pseudo-random generators that create a sequence of apparently non-related numbers. In fact the next number depends on the last and one use a seed value to set the start number. The sequence will always be the same with the same seed. One common way to initialize the seed value is to use the current system time.

 Language  Random function
 Seed function
 C/C++ rand srand
 C# Random.Next  Random(Int32)
 Lua math.random math.randomseed
 Python  random.random random.seed

Number in a range
Its common for random functions (ex rand) to return a value between 0 and a very high positive number. To get a value inside a certain range use modulo operator and the initial value of the range.

rand() % 100 + 1;    // To get a value between 1 to 100.

Uniformly distributions

Gaussian distributions

Filtered randomness

Reference
Counting bits the normal distribution - 2017
https://blog.demofox.org/2017/07/25/counting-bits-the-normal-distribution/

Generating random numbers from a specific distribution by inverting the cdf - 2017
https://blog.demofox.org/2017/08/05/generating-random-numbers-from-a-specific-distribution-by-inverting-the-cdf/

Generating random numbers from a-specific distribution with rejection sampling - 2017
https://blog.demofox.org/2017/08/08/generating-random-numbers-from-a-specific-distribution-with-rejection-sampling/

Comments