One way of doing it
Code: Select all
local num = math.random(0,1)
if num == 0 then num = -1 end
Code: Select all
local neg = { -1, 1 }
num = neg[math.random(1,2)]
Code: Select all
local num = math.random(0,1)
if num == 0 then num = -1 end
Code: Select all
local neg = { -1, 1 }
num = neg[math.random(1,2)]
Code: Select all
num = math.random() >= 0.5 and 1 or -1
Yeah, I'd do that too.bartbes wrote:I used to do stuff like math.random(0, 1)*2-1.
The only way to enjoy the quirks of programming languages.Jasoco wrote:"You're coding the silly way"
Code: Select all
local c
if math.random() >= 0.5 then -- Instead of math.random there would be a local variable pointing to the function.
c = 1
else
c = -1
end
Code: Select all
rnd = choose({-1,1})
function choose(t)
return t[math.random(1,2)]
end
Code: Select all
function choose(t)
if t[1] then return t[math.random(1,#t)]
end
Faster to write maybe, but not faster in execution.coffee wrote:faster
Code: Select all
local num = math.random(-1,1)
while num == 0 do
num = math.random(-1,1)
end
Don't seem good code spite of work. So the program will hang in loop till haven't the luck of not be a zero?dreadkillz wrote:I was thinking of using a while loop:
Code: Select all
local num = math.random(-1,1) while num == 0 do num = math.random(-1,1) end
Users browsing this forum: Google [Bot] and 6 guests