How do I add probability to pick_random()

var my_var

func _ready():
      my_var = [1, 2, 3].pick_random()
      # I want my_var to be either 1, 2, or 3, but I want 3 to have a 50% chance of being picked, and the rest 25% (each)


I don’t think there is anything built-in to do that easily.
The easiest solution would be to just put “3” twice into the array to increase it’s chance of being picked.
my_var = [1, 2, 3, 3].pick_random()
Let me know if that works for you, because any other solutions would require some more elaborate coding.

3 Likes

You could pick a number between 1 and 100 to simulate the odds. Then, use if, elif, and else statements to trigger what you want.

Here’s some pseudocode:

func your_func():
     var rand_var
     var result_var

     rand_var = randi_range(1, 100)
     
     if rand_var < 24: #Less than 25
           result_var = 1
     elif rand_var >= 25 and <= 49 : #Greater than or equal to 24 and less than or equal to 49
           result_var = 2
     else: #If the random integer rolls 50-100
           result_var = 3

You can use the general idea of this code for your randomization needs.

2 Likes