1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-24 17:14:23 +00:00
hella-farm/random.gd

25 lines
535 B
GDScript3
Raw Normal View History

class_name Random
## Random number generator utilities
## Lazy init [RandomNumberGenerator].
static var rng: RandomNumberGenerator = null:
get:
if rng == null:
rng = RandomNumberGenerator.new()
return rng
set(value):
rng = value
2024-04-16 00:18:47 +00:00
static func sample(array: Array[Variant], imin = null, imax = null) -> Variant:
if len(array) == 0:
Log.w(null, "Sampling from an empty array.")
return null
2024-04-16 00:18:47 +00:00
if imin == null:
imin = 0
if imax == null:
imax = len(array) - 1
var idx = rng.randi_range(imin, imax)
return array[idx]