Okay but what really is math.random()?

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).

4 Likes
math.random

returns an integer from 2 intergers, the first must be < than the second. It essentially picks 1 random number between what you inputted. Example:

local Num = math.random(4, 10)
print(Num)

This will return a random selected number between 4 and 10. However, if you make the first number > the second number, it will error.

math.random isn’t all about numbers, you can do it with tables, picking 1 random item out of a folder and so forth.

1 Like

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:

it can take 2, 3, 4, 5

1 Like

math.Random() picks a random number listed in parentheses

An example of this would be:

local Math = math.random(0,9)

print("Math Chosen: ".. Math)
2 Likes

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

6 Likes

Hi!

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.

Here is a Wikipedia article about randomness (Wikipedia - Randomness, 23/12/2020)

12 Likes

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.

1 Like

this is false, all instances of Lua are seeded with 0 by default.

2 Likes

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.

math.random can select a random item out of a table.

local choose_item = {apple,1,thing,20}
wait()
local rand = choose_item[math.random(1,#choose_item)]
print(rand)
1 Like

Then I could be mistaken, as Roblox doesn’t use the vanilla math.random – it uses the same algorithm as Random

1 Like

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.

3 Likes

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
2 Likes

Ive come to the conclusion after your guys thoughts that math.random() is a permutation based pure function

2 Likes

It is crazy how the random function you use is really close to a cipher algorithm. Just change

Seed = (248243 * Seed) % 20000

to

Seed = 248243^Seed % 20000