## need help !

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
yawerali
Prole
Posts: 1
Joined: Wed May 16, 2018 9:08 am

### need help !

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

drunken_munki
Party member
Posts: 125
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.

ivan
Party member
Posts: 1265
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: need help !

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)

zorg
Party member
Posts: 2247
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.
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.

ivan
Party member
Posts: 1265
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: need help !

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.

drunken_munki
Party member
Posts: 125
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: 2247
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: need help !

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

drunken_munki
Party member
Posts: 125
Joined: Tue Mar 29, 2011 11:05 pm

### Re: need help !

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

neku
Prole
Posts: 1
Joined: Thu May 17, 2018 3:07 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.

pgimeno
Party member
Posts: 1060
Joined: Sun Oct 18, 2015 2:58 pm

### Re: need help !

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.