How to pick a random integer from an Array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
var my_array = [1,2,3,4]

How can I pick a random value from this array?

In Godot 4 you can use array.pick_random().

var my_array = [1,2,3,4]
var rand_value = my_array.pick_random()

If what you want is generating a random integer you can do so with randi() and randi_range(from, to):

randi()           # Returns random integer between 0 and 2^32 - 1

Or using a range

randi_range(0, 5)      # Returns an integer between 0 and 5
randi_range(-10, 1000) # Returns random integer between -10 and 1000
5 Likes