# Random number..percentage?

Get help using Construct 2

### » Tue Jun 04, 2013 6:01 am

Hi guys, weird question here that I'm hoping someone might be able to help me with.

Is there a way to assign a random number...with a certain number having a higher percentage of being assigned than other numbers?

Here's an example:

I have a player sprite, I also have an enemy sprite. When the enemy sprite collides with the player (physics objects btw) I want to subtract say, 20 from the player's health variable.

That's easy enough to do, but I would LIKE to have the damage be more randomized than that. Anywhere from having 10-20 health subtracted...but for the most part I want -20 health to be assigned on average with the 10-19 range only showing up on occasion.

Oi, did I explain that well enough? =\

Maybe something like:

Enemy >
On collision with player >
Subtract 20 from Healthvar...but sometimes 10-19 :P

Thanks everyone.
B
19
S
7
G
10
Posts: 82
Reputation: 4,996

### » Tue Jun 04, 2013 6:52 am

Global Variable DAMAGE

On collision with player: Set DAMAGE to int(random(100))
If DAMAGE > 0 and DAMAGE <= 80 then - 20 from health.
If DAMAGE > 81 then - int(random(10,20)

80 percent chance to get 20
20 percent chance to get 10-19
B
101
S
32
G
12
Posts: 1,549
Reputation: 22,103

### » Tue Jun 04, 2013 7:46 am

Brilliant!

Works perfectly, thanks a ton! :)
B
19
S
7
G
10
Posts: 82
Reputation: 4,996

### » Tue Jun 04, 2013 5:17 pm

Using @ArcadEd logic, you could fit all of it in a single expression.[code]On collision with player: Set DAMAGE to (random(100) <= 80) ? 20 : int(random(10,20))[/code]
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143