You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many games need randomly generated numbers. There are many GML scripts around for generating random numbers, but none have been added to this toolbox so far. This issue suggests to add two of them:
scr_random_exponential_dist
scr_random_normal_dist
Sample implementations (based on gmlscripts.com) are below:
/// @param {real} mean mean of the exponential distribution (default: 1)
///
/// @description Returns a pseudo-random number from an exponential distribution
/// @date 2024-04-08
/// @copyright GMLscripts.com
function scr_random_exponential_dist(_mean = 1)
{
if (_mean > 0)
{
return -_mean * ln(1-random(1));
}
else
{
return undefined;
}
}```
```/// @function scr_random_normal_dist([mean, std])
/// @param {real} mean mean of the Gaussian (normal) distribution (default: 0)
/// @param {real} std standard deviation of the Gaussian (normal) distribution (default: 1)
///
/// @description Returns a pseudo-random number from a Gaussian (or normal) distribution
/// @date 2024-04-08
/// @copyright GMLscripts.com
function scr_random_normal_dist(_mean = 0, _std = 1)
{
var x1, x2, w;
do {
x1 = random(2) - 1;
x2 = random(2) - 1;
w = x1 * x1 + x2 * x2;
} until (0 < w && w < 1);
w = sqrt(-2 * ln(w) / w);
return _mean + _std * x1 * w;
}```
The text was updated successfully, but these errors were encountered:
Appsurdgames
changed the title
Random number generator
Draw random numbers from a distribution
Dec 13, 2024
I agree with the general notion of having non-linear random functions.
An option to consider would be having functions that don't return a random value within themselves, but rather return the distribution result. E.g. you make a random call, you receive 0.37 and then you look for "the value X such that 37% of values are lower on an exponential distribution with mean A" or "the value Y such that 37% of values are lower on a normal distribution with mean B and standard deviation C". I think those functions would be much more versatile, and it'd be easy to produce skewed randoms from these.
Also also, I'm not too fond of scr_random_normal_dist having a while loop in order to find an appropriate random value. Going by the code, it seems to be "get a random point in square from -1 to 1 X/Y, if it's outside the circle, keep looking". But maybe there's a way to reliably get the value on the first try, with some clever vector/geometry transforms...?
(though also if we go for the distribution mappers route, rather than random distributions, maybe the way to calculate these will be free of those problems altogether)
Many games need randomly generated numbers. There are many GML scripts around for generating random numbers, but none have been added to this toolbox so far. This issue suggests to add two of them:
Sample implementations (based on gmlscripts.com) are below:
The text was updated successfully, but these errors were encountered: