i didn’t really knew in wich category to put this but i’ve been searching for a response to how math.random works because i tried not to use it because of the gaussian distribution, but i just kept thinking that i never found the answer to this, and i tried reading about Lua and Luau but i didn’t really found nothing about this, also chatGPT said that math.random doesn’t do it but… yeah i don’t trust chat gpt info
math.randomseed sets seed for math.random.
You can use Random.new() if you want to handle multiple seeds at once+more flexability.
math.random() gives a number between the two values provided, so.
math.random(1, 3)
would return a value of 1, 2, or 3 at random. You can then add an if statement to check what the number is for making a chance system.
According to the documentation, the result is a “uniform pseudo-random” number or integer (depending on the arguments passed in) and “uses a 32-bit PCG (Permuted Congruential Generator) which achieves excellent statistical performance and makes its output hard to predict.”
math.random() does NOT use gaussian distribution.
Computers never do anything random, they rely on dynamic values.
math.random uses math.randomseed in it’s own randomness calculation to give “random” results.
IIRC, Roblox uses current time as randomseed, so everytime you use math.random, the seed resets to current time giving you fresh result next time
Hi! To add on to what @East98 said above, the Luau source shows that the output of math.random follows a uniform (technically pseudo) random distribution for floating point numbers in [0, 1). If you specify the bounds, its output is uniformly distributed on the integers within, and including, the bounds. If one bound argument is provided, the default lower bound of 1 is set.
The use of the pcg32_random function in this source code seems to follow the XSH-RR variant of PCGs outlined in its introductory paper (page 43). This outputs a random 32-bit integer.
Here’s how Luau utilizes that same function to find a random double between [0, 1). To create a double, they’d need to collect two random outputs from the 32-bit pcg32_random function, hence the use of lh and rh. After that, they just follow the random64 technique displayed in the following link:
http://mumble.net/~campbell/tmp/random_real.c
#include <math.h>
#include <stdint.h>
uint64_t random64(void);
/*
* random_real_64: Pick an integer in {0, 1, ..., 2^64 - 1} uniformly
* at random, convert it to double, and divide it by 2^64. Values in
* [2^-11, 1] are overrepresented, small exponents have low precision,
* and exponents below -64 are not possible.
*/
double
random_real_64(void)
{
return ldexp((double)random64(), -64);
}
Edit: I just noticed that the DevForum doesn’t have C syntax highlighting lol
Also, if you’ve decided to try and implement some distribution given math.random and you can’t find a library or implementation that exists on it, you could take a look at shaping a uniformly random variable to fit another distribution.