# Generating a random number to choose an attack

### » Thu Mar 25, 2010 5:42 am

Hi. Could someone point me to where I could learn about generating random numbers? What I'm basically trying to do is have it so that when an enemy moves in range, it attacks in one of four ways. I figured one way of doing it would be to generate a random number that would trigger a variable and thus determine the attack (like if the number is 1-20 it shoots, 20-40, it punches, etc).

Of course, if you have a better way, I'm all ears. Thanks in advance for any help.
B
2
G
3
Posts: 15
Reputation: 880

### » Thu Mar 25, 2010 5:56 am

You can generate random numbers by typing random(40). This will give you a random number between 0 and 40.
B
2
S
2
G
3
Posts: 118
Reputation: 1,486

### » Thu Mar 25, 2010 6:09 am

Actually, you would want to do Random(40) + 1 because without the + 1 it will return a number 0 through 39, not through 40.
B
2
S
2
G
4
Posts: 156
Reputation: 1,762

### » Fri Mar 26, 2010 6:11 pm

Thank you very much for the quick replies, it's appreciated.

I just realized I might have another small problem and I'm unfortunately away from my machine with Construct temporarily. I'm assuming that I would have the sequence be: when enemy moves within x pixels of player, random number is rolled, compare random number to enemy private variable, if random number is (for example) 1, do kick, 2 do punch, etc. However, I want enemies to do one attack slightly more often than others, like: if random number is 1, kick, 2 or 3, punch, 4, 5, or 6, shoot. Could you tell me how I can set specific ranges for variables as in this example?

Thanks again.
B
2
G
3
Posts: 15
Reputation: 880

### » Fri Mar 26, 2010 6:26 pm

The way to do that is, assuming the random number rolled will be the variable applied to the sprite:

Using events simply make multiple conditions stating the range you want to use.

IE (pseudo code)

(condition)
Sprite1.variable "roll" equals 1
Sprite1.variable "roll" equals 2
Sprite1.variable "roll" equals 3

(event)
Do stuff

-----

(condition)
Sprite1.variable "roll" equals 4
Sprite1.variable "roll" equals 5

(event)
Do different stuff

-----

(condition)
Sprite1.variable "roll" equals 6

(event)
Do some more different stuff

So basically you change the variable of the sprite itself (randomly) to match a list of pre-determined conditions.

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
41
Posts: 3,032
Reputation: 30,999

### » Sun Mar 28, 2010 5:27 am

Thank you very much (again)!
B
2
G
3
Posts: 15
Reputation: 880