# Random number..percentage?

0 favourites
• 4 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
• 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.

• 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

• Brilliant!

Works perfectly, thanks a ton! :)

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Using ArcadEd logic, you could fit all of it in a single expression.

``On collision with player: Set DAMAGE to (random(100) <= 80) ? 20 : int(random(10,20))``
• 4 posts