Randomizing between two angles

New releases and general discussions.

Post » Sun Aug 05, 2012 2:50 am

Hi, I'm trying to pick between two angles at random (-180 and 0), I don't wish to have any angles between these two just one or the other randomly picked. Could someone enlighten me on the correct syntax to achieve something like this?

I've attached a link to a cap which demonstrates what I'm trying to achieve - when it's run it populates the ground area with a bunch of rectangles and randomizes their position and height, but they all face to a 0 degree angle and I would like to randomly alternate this between 0 and -180. Any help is much appreciated. Thanks!

http://www.masspanicmusic.com/construct/randomangle.rar
B
46
S
16
G
3
Posts: 133
Reputation: 4,955

Post » Sun Aug 05, 2012 3:23 am

Hey, Signal.

Setting the rectangles' angles to
180 + random(180)
should give you an angle between 180 and 360, that is, between -180 and 0. I cannot yet load the cap you posted due to not having the latest Construct Classic, but I've made a similar model with my version, and it appears to work well enough.

Hope I could be of help.
B
8
S
2
G
6
Posts: 44
Reputation: 3,007

Post » Sun Aug 05, 2012 4:01 am

Hi, thanks for the reply :)
If I set the rectangles angle to 180 + random(180) it still seems to pick any angles between those two values, whereas I just need it to be either -180 or 0 (ignoring all other angles), if you imagine the rectangles like skyscrapers, they all need to stand straight, but face either left or right, but at present with that code they randomize to all angles between those values. I'll see if I can track down and install an older version of cc and do up another example.
B
46
S
16
G
3
Posts: 133
Reputation: 4,955

Post » Sun Aug 05, 2012 4:12 am

If you need two random choices, start by not caring about the actual values. You need a random number that returns exactly one of two numbers. random(n) returns a number between 0 and n-1. So random(2) will return 0 or 1

Now multiply that with the -180 and you get either 0 or -180 (0 * -180 = 0, 1 * -180 = -180

The expression should look somewhat like that:

random(2) * -180

EDIT: I'd say, this thread better belongs to the help section ;)tulamide2012-08-05 04:14:20
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sun Aug 05, 2012 4:19 am

Oops, I didn't realize I was in the discussion forum - I meant for this to go in help - too many tabs open! :P Anyway, thanks - random(2) * -180 worked a treat. Much appreciated.
B
46
S
16
G
3
Posts: 133
Reputation: 4,955


Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 3 guests