Files

26 lines
600 B
GDScript

class_name DeterministicRng
extends RefCounted
const MODULUS := 2147483647
const MULTIPLIER := 48271
var _state: int
static func from_seed(seed: String) -> DeterministicRng:
var seed_value := 0
for index in range(seed.length()):
seed_value = (seed_value * 31 + seed.unicode_at(index)) % MODULUS
if seed_value == 0:
seed_value = 1
return DeterministicRng.new(seed_value)
func _init(initial_state: int) -> void:
_state = initial_state
func next_int() -> int:
_state = (_state * MULTIPLIER) % MODULUS
return _state
func next_float() -> float:
return float(next_int()) / float(MODULUS)