Page 1 of 2

need help !

Posted: Wed May 16, 2018 9:11 am
by yawerali
Can anybody help in telling me the difference between these two commands ?


ballDx = math.random(2) == 1 and 100 or -100
why its equal to 1 ?
ballDy = math.random(-50, 50)
and here it's just two numbers.
Help will be appreciated. Thanks

Re: need help !

Posted: Wed May 16, 2018 10:33 am
by drunken_munki
Looks fine to me. You must be modifying it somewhere else in your code.

Re: need help !

Posted: Wed May 16, 2018 11:03 am
by ivan
Looks fine.
Note that you can get rid of the conditional check:

Code: Select all

function math.randsign(n)
  return (math.random(0,1)*2 - 1)*n
end

ballDx = math.randsign(100)
ballDx = math.randsign(50)
If you prefer using conditionals:

Code: Select all

function math.randsign(n)
  return math.random(2) == 1 and n or -n
end

ballDx = math.randsign(100)
ballDx = math.randsign(50)

Re: need help !

Posted: Wed May 16, 2018 11:27 am
by zorg
I like how no one actually answered his question;

math.random can take between zero and two arguments,
math.random(x) returns a natural (whole, non-negative) number between 0 and x,
math.random(x,y) returns an integer (whole number) between x and y (not sure if it swaps the two parameters if the second is less than the first),
math.random() returns a real number between 0 inclusive and i think 1 exclusive, or [0,1) in the usual math notation.

All that said, you should not use the lua math.random in löve, for many reasons (including it not being seeded at startup anymore); use love.math.random instead, that will ensure uniformity across platforms, and you won't get the same numbers each startup. It works the same way.

Also, math.random(2) == 1 is just shorthand for if a random number is even, or odd; lua can do (condition) and ifTrue or ifFalse type branches; it will either return -100 or 100; the second one returns a number between -50 and 50.

Re: need help !

Posted: Wed May 16, 2018 11:55 am
by ivan
zorg wrote:
Wed May 16, 2018 11:27 am
will either return -100 or 100; the second one returns a number between -50 and 50.
Yep, good catch by zorg there. :)

Re: need help !

Posted: Thu May 17, 2018 7:44 am
by drunken_munki
But his question is "why its equal to 1?" The tests I ran gave me -100 or 100, so I don't know why he is getting '1' unless he is modiying the var somewhere else.

Re: need help !

Posted: Thu May 17, 2018 8:46 am
by zorg
drunken_munki wrote:
Thu May 17, 2018 7:44 am
But his question is "why its equal to 1?" The tests I ran gave me -100 or 100, so I don't know why he is getting '1' unless he is modiying the var somewhere else.
He asked about the "math.random(2) == 1" part though, from what i could tell.

Re: need help !

Posted: Thu May 17, 2018 10:08 am
by drunken_munki
zorg wrote:
Thu May 17, 2018 8:46 am
He asked about the "math.random(2) == 1" part though, from what i could tell.
Oh I see. Heh ok :D

Re: need help !

Posted: Thu May 17, 2018 3:17 pm
by neku
hi all
I've been looking for a way to write (and test!) small programs (ala knight's tour) for android on the pc. it looks like love2d can really work for me. But how can you control the flow (speed) rate? I would like to show the individual jumps visually (step by step). can anybody help me please? I have found no useful tips.
thanks for the answer in advance.

Re: need help !

Posted: Thu May 17, 2018 6:04 pm
by pgimeno
neku wrote:
Thu May 17, 2018 3:17 pm
hi all
I've been looking for a way to write (and test!) small programs (ala knight's tour) for android on the pc. it looks like love2d can really work for me. But how can you control the flow (speed) rate? I would like to show the individual jumps visually (step by step). can anybody help me please? I have found no useful tips.
thanks for the answer in advance.
Hi, welcome to the forums. It sounds like you have a very usual problem.

There are basically two ways to perform such a task; one is inverting control flow (so your routine returns at every step, and keeps a state for the next step in order to resume at the same point where it was) and the other way is using coroutines, which are like special threads that would let the display events run in the middle of your routine.

Perhaps the hump.timer library helps, see http://hump.readthedocs.io/en/latest/ti ... mer.script (it uses coroutines internally).