Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw random numbers from a distribution #108

Open
Appsurdgames opened this issue Dec 13, 2024 · 1 comment
Open

Draw random numbers from a distribution #108

Appsurdgames opened this issue Dec 13, 2024 · 1 comment
Labels
feature ✨ For feature requests and implementations

Comments

@Appsurdgames
Copy link

Appsurdgames commented Dec 13, 2024

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;
}```
@Appsurdgames Appsurdgames changed the title Random number generator Draw random numbers from a distribution Dec 13, 2024
@Alphish
Copy link
Owner

Alphish commented 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)

@Alphish Alphish added the feature ✨ For feature requests and implementations label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ✨ For feature requests and implementations
Projects
None yet
Development

No branches or pull requests

2 participants