The current knowledge I have on math.random() is it “magically” receives a random number from a number range. But what really makes, math.random(); well, random? I mean the question is really a good one, we are taught in school that the fundamentals of the world are described as pure functions. Even for simulations that rely on functions that are random (like quantum tunneling analysis) how can a computer know if it will be random or not? Can’t any equation simply just be derived by some inverse operations? Think about it, really. I would love a proof.
I suppose we can compare prime numbers or hash values however they still derive from pure functions. What I could think is the digits of pi being random, or even the logarithmic of 2 (what power of base 10 would be equal to 2).
idk about you but it pick the first argument which should be a int64 type value and the other a different int64, it picks a different value between those numbers, for example, 2 - 5:
Guys, I know how math.random() works in a sense I know how to use it but what does it do to produce random values? Like I said the only things random that I can think of on the top of my head are pi and the logarithmic of base 10 to 2
I see what you mean. While I can’t certainly confirm the way Roblox math.random works, I can tell you that there are two different ways to get computer generated “random” number. First are called true random numbers, which are real, unpredictable and reliably random, but which a computer can’t generate only by itself inside the program. A computer takes values that are not a consequence of past returned results and are not calculated, but rather uses something like exact user time input, or something external, outside the computer, which can be an valid source of unpredictable.
Other are pseudorandom numbers. Those can be predicted at least to some extent, because they are calculated by an algorithm, resulting in a sense of complete randomness, which is adequate way and is more then enough for Roblox and other games and for example dice games. However, that approach still represents a high vulnerability in cryptography, which is a critical area when it comes to random numbers.
True random can be received from different sources with random events. A source is usually something bound to time and timing, but it can be anything, somehow you could even use temperature, sound etc.
Yes, this is exactly how it is.
If you use math.randomseed() to put in a seed and then use math.random() to find a random value between x and y, it will return the same z everytime until you change the seed again.
EDIT: if you don’t use math.randomseed(), it probably uses a formula using date, time and different variables of in-game objects to give a sense of randomness.
I have run some tests on math.random() and the first returned number without setting the seed is always different, but if you set the seed to 15 then it will return: 0.11269320460541.
Lua is built on C, and AFAIK so is the math.random library function. It’s based on rand from the C standard library. Different compilers / C implementations can use different implementations of the standard library. glibc is one implementation if you want to read the actual source code, and here’s a stackoverflow post that talks about the types of RNGs used.
Random does not exist, computer can’t just pick random number, impossible, this is why functions like rand and math.random use seed. Random will generate random numbers based off of the given number, seed in this case, in roblox seed refreshes every time you run the game, usually seed is generated based off of tick(), now when you have the seed you just multiply it with random numbers and you have a random function, here i wrote a simple one
local Seed = tick()
local function random()
-- 248243 is just a random math operation to generate "random" number
-- 20000 is max number, modulus operator prevents it from going higher
Seed = (248243 * Seed) % 20000
return math.floor(Seed)
end
for i = 1, 100
do
print( random() )
end
It’s as simple as that, nothing complicated. But in Lua math.random offers you to have min and max value, that is not a problem to implement, too! You just need few more values and it’s done
local Seed = tick()
local Fraction = 1 / (20000)
local function random(min, max)
Seed = (248243 * Seed) % 20000
return math.floor(min + ((max - min + 1) * (Seed * Fraction)))
end
for i = 1, 500
do
print( random(1, 100) )
end