Create var rng := RandomNumberGenerator.new(), set rng.seed, and roll with rng.randi_range() instead of the global randi_range(). In our Godot 4.7 test a three-reel slot rolled with seed(1124) gave the same 1000 spins twice. Adding a screen shake that called randf_range() twice per spin left only 5 of 1000 spins the same, different from the second spin on. A shake only on three of a kind broke it from the fourth spin. Its own RandomNumberGenerator, seeded 1124, gave 1000 of 1000 the same while the shake and other randi() calls ran.
The code
Save it as run_rng.gd. make() returns a RandomNumberGenerator with your seed, or a random one for 0. daily_seed() turns a date string into a seed, so every player gets the same run that day. save() and load_state() store and restore where the sequence is, so a loaded game rolls exactly what it would have. For example: const RunRng := preload("res://run_rng.gd"), var rng := RunRng.make(RunRng.daily_seed(Time.get_date_string_from_system())), then rng.randi_range(0, 5) for every roll that must repeat.
extends RefCounted
## Give a system (slot machine, loot table, map generator) its own random numbers,
## so a seed gives the same results whatever else in the game uses random.
static func make(seed_value: int = 0) -> RandomNumberGenerator:
var r := RandomNumberGenerator.new()
if seed_value != 0:
r.seed = seed_value
else:
r.randomize() # 0 = a different run every time
return r
static func daily_seed(date_text: String) -> int:
return hash(date_text) # the same text gives the same seed, for example "2026-11-27"
static func save(r: RandomNumberGenerator) -> int:
return r.state # store it with the save file
static func load_state(r: RandomNumberGenerator, saved_state: int) -> void:
r.state = saved_state # the run continues exactly where it was saved
What we measured
A spin was three reels, each a number from 0 to 5. We rolled 1000 spins and compared them spin by spin with a first run. The shake called randf_range() twice, as a screen shake or a particle burst might. Two runs gave the same numbers, apart from the generators made without a seed, which are meant to differ.
- seed(1124) and the global randi_range(), twice: 1000 of 1000 spins the same. The first three were [3, 3, 0], [2, 4, 4], [4, 4, 4].
- The same with a shake after every spin: 5 of 1000 the same, which is about chance for 216 possible spins. The second spin was already different.
- A shake only after three of a kind: the third spin was [4, 4, 4], so the fourth spin was different, and 10 of 1000 matched.
- Its own RandomNumberGenerator seeded 1124, with the shake and an extra global randi() after every spin, and a different global seed: 1000 of 1000 the same as the same generator alone.
- Saving the state after 500 spins and loading it back: the next 500 spins came out the same both times, 500 of 500.
- daily_seed("2026-11-27") gave the same number twice, and the next day a different one. Two generators seeded with it: 1000 of 1000 spins the same.
- Two RandomNumberGenerator.new() without a seed: different first values and different seeds, so leaving the seed alone already gives a new run each time. make(0) did the same.
Why it happens
randi(), randf(), randi_range() and the others all read from one random sequence shared by every script, and seed() only sets where it starts. Each call moves it on, so any other code that uses random between your rolls, a screen shake, a particle color, an enemy's idle timer, takes numbers your rolls would have had, and everything after shifts. A RandomNumberGenerator is a separate sequence that only your code moves.
What to change
- One generator per system that must repeat: rolls, loot, the map. Cosmetic effects can keep using the global functions.
- Weighted rolls with your own generator: rand_weighted() gives the chances you set.
- Bad luck streaks that feel unfair: pseudo-random chance, and a shuffle bag for no repeats.
- Reels that show the roll: stop forward on the result.
A slot machine built this way: Slot Roguelike Kit for Godot 4 rolls from a symbol bag with its own RandomNumberGenerator. Set Rng Seed in the Inspector, or machine.rng.seed in code, for the same results every run, or pass a grid to spin() to force a result for a tutorial or a rigged first spin.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Slot Roguelike Kit for Godot 4 rolls from a symbol bag with its own RandomNumberGenerator: set its seed for daily runs and replays, or force any result.
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene