Should I use Random.new() or math.random() to determine if a random event happens?

So to my understanding math.random() generates a pseudo random number, but if you use math.randomseed(tick()) you can simulate “true” random. I’ve heard that math.random() is either deprecated or is going to be in the near future. I’ve also heard that math.random() was updated to use the same algorithm as Random.new().

It probably doesn’t matter which method I use in all honesty but I want my project to be as optimized as possible.

What I’m looking for:

I need to have a system that generates random events. It’s crucial that the chances are as random as possible for gameplay reasons.

Which method should I go with?


math.random() Method:

math.randomseed(tick())

local randomNumber = math.random(1,100)

if randomNumber >= 50 then
	-- Do the thing
else 
	-- Don't do the thing
end

-- 50% chance of doing the thing

Random.new() Method:

local randomNumber = Random.new():NextNumber(1,100)

if randomNumber >= 50 then
	-- Do the thing
else 
	-- Don't do the thing
end

-- 50% chance of doing the thing
3 Likes

You don’t need to use math.randomseed, math.random() is perfectly good on its own. Use whatever makes more readable code, sometimes it’s math.random(), sometimes it is the Random API.

For your example code, I would personally use math.random() > 0.5.

5 Likes