# Another question about using 'random'

Get help using Construct 2

### » Sat Apr 27, 2013 1:08 am

Good afternoon. Just bought the personal version of Construct 2 and couldn't be happier :)

I am having trouble getting a random function to work. I am attempting to create a turn based game, where the enemy has two options each turn and picks a random one of those two each time. This is what I have.

----------------------------
Global number 'Target' = 0
Global number 'Turn' = 0

System / Turn = 2            - Set 'Target' to floor(random(1))

System / Turn = 2            - Play 'Animation1'
System / Target = 0

System / Turn = 2            - Play 'Animation2'
System / Target = 1

On 'Animation1' finished    - Set 'Turn' to 3
OR
On 'Animation2' finished

----------------------------

Right now animation1 plays every time. If I disable animation1 then nothing happens. Please help me learn how to use random properly.

danielbishop562013-04-27 01:11:33
B
9
S
2
Posts: 54
Reputation: 664

### » Sat Apr 27, 2013 1:21 am

Manual: Generate a random float from 0 to x, not including x.

Try random(2).
B
64
S
19
G
105
Posts: 3,098
Reputation: 64,561

### » Sat Apr 27, 2013 1:32 am

Unfortunately when I try random(2) -or- floor(random(2)) nothing happens at all...
B
9
S
2
Posts: 54
Reputation: 664

### » Sat Apr 27, 2013 1:53 am

One other common mistake i've made myself is this (pseudocode):

[
randChoice=random(2)

if randChoice==0 then say "0"
if randChoice==1 then say "1"
]

You'd think this would work, but it will only work an incredibly small amount of the time. Why? You're not converting the random number into an integer. So it rolls numbers like 1.023882 or 0.47366 so it rarely ever hits exactly 0 or 1.

int(random(2))

And of course, remember random is zero based. So, int(random(2)) will give you either 0 or 1.

*Sigh, just saw the post above after I wrote this. You use floor, I use int.
Venivididormi2013-04-27 02:56:30
B
8
S
2
Posts: 63
Reputation: 1,223

### » Sat Apr 27, 2013 1:55 am

Try a 'system: trigger once' after the 'turn == 2' condition so you only set target to a random value once otherwise you'll be switching animations continuously.
B
53
S
28
G
18
Posts: 1,520
Reputation: 24,905

### » Sat Apr 27, 2013 2:48 am

You guys are amazing, totally solved it. Thank you!

I will have a Kickstarter up in a few weeks for my video game, using Construct 2 to make my prototype. I will give the program and the community plenty of credit.

If you want to check out my game so far it is at-
www.necromancergame.com
B
9
S
2
Posts: 54
Reputation: 664

### » Sat Apr 27, 2013 2:59 am

Good luck! Looks like you've got a lot of it designed already.

I'm curious what you'll use if you get the funding. Continue with C2 or make an engine in a C variant?
B
8
S
2
Posts: 63
Reputation: 1,223

### » Sat Apr 27, 2013 3:12 am

The purpose of the Kickstarter will be to hire a programmer, so I can take care of pretty much everything else (art/writing/design/mechanics e.t.c)

Hopefully I will find a very experienced programmer and so I will basically let them decide what engine/program/language to use that will best suit the game.

Although some friends of mine have said that I should use the Kickstarter funds to hire a digital artist instead, let them re-do all of the art work, and learn to program myself.

Either path is pretty daunting...
B
9
S
2
Posts: 54
Reputation: 664