← Pixel Dreamer Studio

A chance roll without the long bad-luck streaks

Raise the chance a little after every failure and reset on success, with the step picked to keep the same average.

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.

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

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