need help !

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.
yawerali
Prole
Posts: 1
Joined: Wed May 16, 2018 9:08 am

need help !

Post by yawerali » Wed May 16, 2018 9:11 am

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

User avatar
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: need help !

Post by drunken_munki » Wed May 16, 2018 10:33 am

Looks fine to me. You must be modifying it somewhere else in your code.

User avatar
ivan
Party member
Posts: 1315
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: need help !

Post by ivan » Wed May 16, 2018 11:03 am

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)

User avatar
zorg
Party member
Posts: 2368
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: need help !

Post by zorg » Wed May 16, 2018 11:27 am

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
ivan
Party member
Posts: 1315
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: need help !

Post by ivan » Wed May 16, 2018 11:55 am

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. :)

User avatar
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: need help !

Post by drunken_munki » 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.

User avatar
zorg
Party member
Posts: 2368
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: need help !

Post by zorg » Thu May 17, 2018 8:46 am

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: need help !

Post by drunken_munki » Thu May 17, 2018 10:08 am

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

neku
Prole
Posts: 3
Joined: Thu May 17, 2018 3:07 pm

Re: need help !

Post by neku » 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.

User avatar
pgimeno
Party member
Posts: 1232
Joined: Sun Oct 18, 2015 2:58 pm

Re: need help !

Post by pgimeno » Thu May 17, 2018 6:04 pm

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).
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests