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
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 »

coffee wrote:
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?
Hmm. Good point. It it silly to be stuck in a loop if you don't get what you want, and there's the offchance that you could get consecutive 0's ten times in a row and waste cpu cycles. My example would be something NOT to do then.
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Randomly choose negative or positive while excluding zer

Post by timmeh42 »

Code: Select all

local num = math.pow(-1,math.random())
Maybe this? Least hackish, as it doesn't involve any other number than -1, but the math.pow might be slow.
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 »

Let's not forget it doesn't work, seeing as math.random() returns a number between 0 and 1, so you can get fractions. Fixing that however would work.. but it looks pretty damned hackish.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Randomly choose negative or positive while excluding zer

Post by Nixola »

I'd suggest using "^", I think it's faster
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Randomly choose negative or positive while excluding zer

Post by timmeh42 »

Ah damn, i thought it was integers only. (and that was a very silly thing to think, too, considering I was reading straight out of the manual)

Code: Select all

local num = math.pow(-1,math.random(0,1))
That should work, but probably still slower than others.

Also is " ^ " allowed? And if so why should it be faster than math.pow?
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Randomly choose negative or positive while excluding zer

Post by Petunien »

I'm not experienced and at the beginning, though, but for me bartbes' solution looks as fastest as can be.

One line and on every call it returns -1 or 1, nothing other.

Or?
"Docendo discimus" - Lucius Annaeus Seneca
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 »

Petunien wrote:I'm not experienced and at the beginning, though, but for me bartbes' solution looks as fastest as can be.

One line and on every call it returns -1 or 1, nothing other.

Or?
Could be. However boolsheet consider that his method could be faster than bartbes/robin one. I as hobby coder can't tell wich one is executed first including yours. Just to add that speed isn't all. Jasoco was worried about the "hack" factor. I too also consider that sometimes loose a bit of speed for clean code readability isn't really a loss. However major both points is the ideal.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Randomly choose negative or positive while excluding zer

Post by Nixola »

"^" is not allowed, just checked... Why do I remember that I used it then?
EDIT: Lua doesn't accept it, LÖVE does
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
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 »

Nixola wrote:"^" is not allowed, just checked... Why do I remember that I used it then?
EDIT: Lua doesn't accept it, LÖVE does
LÖVE does not modify Lua in any way and exponention with ^ is in Lua's reference manual since at least version 2.1. There must be an error in your test or you are looking for something else.
And if so why should it be faster than math.pow?
^ is faster because math.pow involves a table lookup and a function call.
Shallow indentations.
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Randomly choose negative or positive while excluding zer

Post by timmeh42 »

Boolsheet wrote:^ is faster because math.pow involves a table lookup and a function call.
And " ^ " isn't just a reference call to math.pow? And if not, why does math.pow use a slow method, when lua is supposedly a fast language?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 235 guests