Start each try at a small chance and add the same small step after every failure, resetting on success, with the step worked out so the average stays what you wanted. randf() < 0.25 is fair on average but has no memory: in our Godot 4.7 test it failed 42 times in a row within 100,000 tries. The script also succeeded 25.02% of the time, and its longest losing streak was 10.
The code
Keep one of these per thing the player retries, such as a breakthrough, a crafting upgrade or a critical hit. Set chance to the average rate you want and call roll() for each try. Save fails with the game so a reload does not reset the streak.
extends RefCounted
## A chance roll with the same average success rate as randf() < chance, without the long losing streaks:
## each failure makes the next try a little more likely, and a success starts again (pseudo-random distribution).
var chance := 0.25: # the average success rate you want, 0.25 = 1 in 4
set(v):
chance = v
_step = step_for(v)
var fails := 0
var rng := RandomNumberGenerator.new()
var _step := step_for(0.25)
func roll() -> bool:
if rng.randf() < _step * (fails + 1): # try n after a success has chance step x n
fails = 0
return true
fails += 1
return false
## The chance added per try that gives this average success rate.
static func step_for(rate: float) -> float:
var lo := 0.0
var hi := rate
for i in 40:
var c := (lo + hi) * 0.5
if _average(c) > rate:
hi = c
else:
lo = c
return (lo + hi) * 0.5
static func _average(c: float) -> float:
var tries := 0.0
var still_failing := 1.0
var n := 1
while still_failing > 0.0:
var p := minf(1.0, c * n)
tries += n * still_failing * p
still_failing *= 1.0 - p
n += 1
return 1.0 / tries
What we measured
100,000 tries each, the same random seed for every method. Two runs gave the same numbers.
- 25%:
randf()succeeded 24.95% of the time, failed up to 42 times in a row, and 1 success in 100 came after 15 or more failures. The script: 25.02%, at most 10 in a row, 1 in 100 after 8 or more. - 10%:
randf()10.17%, up to 80 failures in a row, 1 in 100 after 42 or more. The script: 10.04%, at most 33, 1 in 100 after 22 or more. - The usual pity fix, adding 10% to the chance after every failure: the streaks got short, at most 8 at 25% and 9 at 10%, but the real success rate went up to 37.32% and 27.33%. The game got easier without anyone deciding it should.
Why plain random feels unfair
Each roll of a random number knows nothing about the rolls before it, so a 25% chance fails 10 times in a row about 1 time in 18 on its own, and over thousands of players somebody sees 40. Players remember the streaks, not the average. Growing the chance after each failure ends a streak early, and starting lower makes up for it: at 25% each try adds 0.0847, so try 12 after a success is certain, and the average lands back on 25%. Dota 2 rolls critical hits this way.
What to change
chance: the average rate. The step is worked out again when you change it.- A pill or item that helps one try: add its bonus to the chance of that try only, for example by checking
rng.randf() < _step * (fails + 1) + bonusin your own copy ofroll().
Breakthrough rules to build on: Xianxia Cultivation System for Godot 4 has realms and layers with entry chances that fall realm by realm, down to 10%, pills that add to the next breakthrough try, spirit roots, techniques and save and load.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia Cultivation System for Godot 4 has realms, layers and breakthroughs with entry chances, tribulations, pills, spirit roots, techniques and save/load.
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
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- 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