Randomly choose negative or positive while excluding zero

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Randomly choose negative or positive while excluding zero

Post by Jasoco »

Anyone have a better easier way to randomly choose between -1 and 1 (Positive and negative) than what I've come up with so far?

One way of doing it

Code: Select all

local num = math.random(0,1)
if num == 0 then num = -1 end
One I just came up with

Code: Select all

local neg = { -1, 1 }
num = neg[math.random(1,2)]
Surely there's a different way, or is this the best way?
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Randomly choose negative or positive while excluding zer

Post by Boolsheet »

There's also:

Code: Select all

num = math.random() >= 0.5 and 1 or -1
They all perform about the same.

What do you mean by 'the best way'? Best way to do what?
Shallow indentations.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Randomly choose negative or positive while excluding zer

Post by Jasoco »

I dunno. Most efficient and least "hacky". There's always someone who will say "You're coding the silly way" when looking at another person's code.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Randomly choose negative or positive while excluding zer

Post by bartbes »

I used to do stuff like math.random(0, 1)*2-1.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Randomly choose negative or positive while excluding zer

Post by Robin »

bartbes wrote:I used to do stuff like math.random(0, 1)*2-1.
Yeah, I'd do that too.
Help us help you: attach a .love.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Randomly choose negative or positive while excluding zer

Post by Boolsheet »

Jasoco wrote:"You're coding the silly way"
The only way to enjoy the quirks of programming languages. ;)
In this case, the winner is probably what's most readable to you.

Using a table uses up some memory and it has an average speed.
If you want speed, then something like this wins (not by much):

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
Specifying a range for math.random makes Lua do more work and 'if .. then .. end' is faster than the ternary-style '... and ... or ...'.
Shallow indentations.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Randomly choose negative or positive while excluding zer

Post by coffee »

Well, without doubt bartbes and Robin's way is much more clever, short and faster. But I end to prefer a more long, "legible" as the second method from Jasoco. Don't know, feels like the correct cleaner (but not the efficient coder) way to do.

Code: Select all

rnd = choose({-1,1})
function choose(t)
    return t[math.random(1,2)]
end
EDITED:
Thank you Jasoco, at the end I made a handy table random element selector.

Code: Select all

function choose(t)
    if t[1] then return t[math.random(1,#t)]
end
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Randomly choose negative or positive while excluding zer

Post by Boolsheet »

coffee wrote:faster
Faster to write maybe, but not faster in execution. ;)
Shallow indentations.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Randomly choose negative or positive while excluding zer

Post by dreadkillz »

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
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Randomly choose negative or positive while excluding zer

Post by coffee »

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
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?
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 1 guest