From 2286009079ba1ff510b30e22f6df796ee912e0e7 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Apr 2024 02:18:47 +0200 Subject: [PATCH] Add `Random.sample` function --- random.gd | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/random.gd b/random.gd index 11f0915..a0a37a0 100644 --- a/random.gd +++ b/random.gd @@ -10,3 +10,12 @@ static var rng: RandomNumberGenerator = null: return rng set(value): rng = value + + +static func sample(array: Array[Variant], imin = null, imax = null) -> Variant: + if imin == null: + imin = 0 + if imax == null: + imax = len(array) - 1 + var idx = rng.randi_range(imin, imax) + return array[idx]