Page 2 of 3

Re: Randomly choose negative or positive while excluding zer

Posted: Sat May 26, 2012 11:52 pm
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.

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 8:51 am
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.

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 8:59 am
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.

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 9:00 am
by Nixola
I'd suggest using "^", I think it's faster

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 9:15 am
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?

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 9:36 am
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?

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 10:06 am
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.

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 11:10 am
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

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 12:18 pm
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.

Re: Randomly choose negative or positive while excluding zer

Posted: Sun May 27, 2012 6:24 pm
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?