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
need help !
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 134
- Joined: Tue Mar 29, 2011 11:05 pm
Re: need help !
Looks fine to me. You must be modifying it somewhere else in your code.
Re: need help !
Looks fine.
Note that you can get rid of the conditional check:
If you prefer using conditionals:
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)
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)
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: need help !
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.
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 True 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.
Re: need help !
Yep, good catch by zorg there.
-
- Party member
- Posts: 134
- Joined: Tue Mar 29, 2011 11:05 pm
Re: need help !
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: need help !
He asked about the "math.random(2) == 1" part though, from what i could tell.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.
Me and my stuff True 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.
-
- Party member
- Posts: 134
- Joined: Tue Mar 29, 2011 11:05 pm
Re: need help !
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.
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 !
Hi, welcome to the forums. It sounds like you have a very usual problem.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.
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).
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests