I want to choose a random number between -5 and 5, but I don’t want -1 to 1 to be chosen. How do I set a limit, if that’s even possible?
This is one way you could do it:
local var1 = math.random(-5,-1) local var2 = math.random(1,5) local var3 = math.random(1,2) local finalVar = 0 if var3 == 1 then finalVar = var1 elseif var3 == 2 then finalVar = var2 end
or you could do it this way:
function chooseVar() finalVar = math.random(-5,-1) return finalVar end repeat if finalVar >= -1 and finalVar<= 1 then chooseVar() end until finalVar >= 1 or finalVar<= -1 print(finalVar) --just to make sure this worked correctly
you can set limits to math.random doing this
math.random(-5,5)
the first value is the minimum number and the second one is the maximum
local number = math.random(1,5)*((math.random(0,1)*2)-1)
This is probably the most simple way to do it, one line and no if statements