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.coffee wrote: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
Randomly choose negative or positive while excluding zero
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Randomly choose negative or positive while excluding zer
Re: Randomly choose negative or positive while excluding zer
Code: Select all
local num = math.pow(-1,math.random())
- 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
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
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Randomly choose negative or positive while excluding zer
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)
That should work, but probably still slower than others.
Also is " ^ " allowed? And if so why should it be faster than math.pow?
Code: Select all
local num = math.pow(-1,math.random(0,1))
Also is " ^ " allowed? And if so why should it be faster than math.pow?
Re: Randomly choose negative or positive while excluding zer
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?
One line and on every call it returns -1 or 1, nothing other.
Or?
"Docendo discimus" - Lucius Annaeus Seneca
Re: Randomly choose negative or positive while excluding zer
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.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?
Re: Randomly choose negative or positive while excluding zer
"^" is not allowed, just checked... Why do I remember that I used it then?
EDIT: Lua doesn't accept it, LÖVE does
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Randomly choose negative or positive while excluding zer
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.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
^ is faster because math.pow involves a table lookup and a function call.And if so why should it be faster than math.pow?
Shallow indentations.
Re: Randomly choose negative or positive while excluding zer
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?Boolsheet wrote:^ is faster because math.pow involves a table lookup and a function call.
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests