How do you make something happen? But there is a percentage that the event happens.

+4 votes

Hi,

You can use math random methods like `randf()`

, see gdscript-randf

```
var percent = randf()
if (percent > 0.8):
# some coode
```

+3 votes

The common way to do it is using a random number generator, in the case of GDScript, "randf()" is the best for this.

With that function you will get a random number between 0 and 1, you can take 0 as 0% and 1 as 100%.

That way, dividing the range 0 to 1 in percentile-like fashion you can make decisions according the result of the generator.

Example: if you have 30% chances, check if the random number is `<= 0.3`

(or `> 0.7`

) .

*There is a probability of 30% that the randf result in a number <= 0.3 (or > 0.7).*

ps: remember to use `randomize()`

somewhere before the use of random number generators.

- All categories
- Engine 32,318