Is math.random enough for a % system?

I was thinking if math.random is good enough for a % system. Example:

local chance = math.random(1,100)
if chance <=30 then
print("30% done!")
else
print("oops, 70% done.")
end
1 Like

If you want to take a random percentage of something I don’t see why not.

it would be completely fine for a percent system as far as I know

It’s a slightly minor nit-pick but you can use it as such:

if math.random() <= 0.3 then
    print('uh 30%')
else
    print('jk more like 70%')
end

Since without parameters random returns a decimal in the range [0, 1).

2 Likes

Yeah I would definitely recommend the use of math.random() without parameters if you are trying to get a random percentage of an existing value. That would look something like:

local Num = 50
local percentOfNum = math.random()*Num --will return something from 0 up to but not including 50
1 Like

That would give you accuracy up to one one hundredth. As others suggested you can treat math.random as a decimal percent value and get full accuracy. Additionally you can use Random.new (this is my personal preference):

local random = Random.new(seed) -- Seed doesn't have to be specified

local chance = random:NextNumber(0, 100) -- Random decimal between 0 and 100 (For percent). Equivalent to your solution above would be random:NextInteger(1, 100).
if chance <= 30 then
	print("Yep! 30%")
else
	print("Nope, > 30%")
end

So that will make 0,1 0,2, etc. Chances?

Your percentage would be from 0 to 1. 1 being 100%, 0 being 0%, so just take your desired % and divide by 100.

Math.random is good, but I have had instances where it generated the same number multiple times when it was done at the same time. I would rather use local random = Random.new() random:NextInteger(1,100), but I guess it’s opinion base. Whatever works for you.

I am just saying that I have had a bad experience with math.random, as it generated the same number multiple times. Again I mentioned that it is based on opinion, so do what fits you.

If you’re worried about getting the same sequence you could change the seed of the RNG

math.randomseed(os.time())

Called once at the beginning of the script.

Pretty sure shortly after the Random object was released Roblox also modified math.random to use the same backend. If this is the case then there is no difference now between the two besides where the state is stored.

1 Like

Seems like a documentation issue if that’s on the Roblox devhub. The announcement post states under the Technical Details that it would change by early 2018.

Scripts that set math.randomseed are precisely why math.random is not viable for reliable random number generation. Any script can set the global seed, causing other scripts to have duplicate random values. You can do some research and implement your own solution too. Linear congruential generators are blazing fast for simple use cases, but the built in Random.new is a great reliable solution.

If you do go with LGC, be careful when picking the multiply, increment, and modulus values. seed = (seed * multiplier + increment) % modulus is fast, but Lua uses doubles, which only support integer precision up to 2^53 and can’t overflow, so you don’t want (modulus - 1) * multiplier + increment to exceed that. Values with a modulus of 2^32 or 2^31 are pretty safe, and you can find reliable ones online.

3 Likes