Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's the difference of Random.new vs math.random?

My entire life I used math.random, but I found that it is not very reliable and uniform. I just was introduced[today] to the Random.new function, but I'm not sure if it has better or worse reliability than the latter. Can anyone brief me on the contrasts and similarities of these two?

examples:

-- math.random lines

rand = math.random(1, 4)
print(rand)

-- the output would either print 1,2,3, or 4, yes?
-- Random.new lines

rand = Random.new(3) -- just a random "seed", no idea what this is for.
print(rand:NextInteger(1, 4)


-- the output would have the same[similar] results as the above example yea?

1 answer

✓ Accepted answer

Random allows you to have multiple random objects with different seeds. Since computers can't actually generate random numbers, the seed decides the initial point from which the numbers will be generated.

math.random() was modified to use a Random object internally, so the algorithm for both of them is now the same.

"The algorithm used by Random is the XSH-RR variant of the PCG family. In a nutshell, it’s is a fast algorithm with excellent statistical properties." - Roblox

This discussion is preserved as it was. New answers, comments, and votes are closed.