Unequal chance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lapsyy

Hi, in my game i have an enemy spawner which has three types of enemies, i want two of them to have equal 40% chance while the other has only 10% chance of spawning, how do i do this?

What do you want to happen the other 10%?

Ruckus T-Boom | 2019-03-28 18:45

:bust_in_silhouette: Reply From: volzhs

enemy 1 and 2 has 40% chance of spawn and enemy 3 has 10% chance then…
it can be easily done like below.

var chance = rand_range(0, 90)
if chance <= 40:
    spawn(1)
elif chance <= 80:
    spawn(2)
else:
    spawn(3)

This would allow enemy 3 to have 20% chance, but i think that’s what OP actually mean to ask

thebspin | 2019-03-28 18:38

Actually, it would be 44.444% for 1 and 2, and a 11.111% chance for 3.

Ruckus T-Boom | 2019-03-28 18:45

:bust_in_silhouette: Reply From: Ruckus T-Boom

Assuming you did your math correctly and want nothing to happen 10% of the time, you would use:

var chance = rand_range(0, 100)
if chance < 40:
    spawn(1)
elif chance < 80:
    spawn(2)
elif chance < 90:
    spawn(3)
else:
    pass # Do something else?

If you do want to always spawn enemies, you should recheck your numbers (they should add up to 100%) and use similar logic at the appropriate break points.