Math.random is confusing

Hi, I am trying to make it so you have a 10% chance of a bonus round but my script doesn’t seem to be working.

local BonusRoundSelect = false


-- game loop
local ran = math.random(1,10)
	if ran == 2 or 7 then
		BonusRoundSelect = true
		Status.Value = "BONUS ROUND"
	else
		BonusRoundSelect = false
	end
print(BonusRoundSelect)   -- for testing
print(ran)   -- also for testing

-- after round finishes

BonusRoundSelect = false

what printed in the output after some testing:

11:49:59.639  true  -  Server - MainScript:61
  11:49:59.640  2  -  Server - MainScript:62
  11:51:11.343  true  -  Server - MainScript:61
  11:51:11.343  6  -  Server - MainScript:62
  11:51:54.238  true  -  Server - MainScript:61
  11:51:54.238  9  -  Server - MainScript:62
  11:52:15.690  true  -  Server - MainScript:61
  11:52:15.690  9  -  Server - MainScript:62
  11:52:36.893  true  -  Server - MainScript:61
  11:52:36.894  9  -  Server - MainScript:62
  11:52:59.258  true  -  Server - MainScript:61
  11:52:59.260  2  -  Server - MainScript:62
 11:54:07.492  true  -  Server - MainScript:61
  11:54:07.493  10  -  Server - MainScript:62

for some reason it’s never false…

Try:

local BonusRoundSelect = false

local ran = math.random(1,10)
if ran == 2 or ran == 7 then
	BonusRoundSelect = true
end
print(ran, BonusRoundSelect)

Works for me.

1 Like

works, but is there a more efficient way?

I guess math.random(1, 5) instead?
Not really much you can optimize nor does it have any significant performance changes in the first place.

Smallest you could do is:

if math.random(1, 5) == 1 then
	
end

Ok, Thanks a lot , I might do math.random(1,100) and then if 1,2,3,4,5,6,7,8,9,10, tho I don’t know if it is a good idea

Performance should be the least of your worries.
Roblox can perform and process thousands of actions in a single frame.
You can call math.random() thousands of times without any performance change on any scale.

Btw if I solved your issue if possible mark as it solved.
Thanks and good luck with your project.

1 Like

Thanks a lot, I already marked yours as solved :rofl: