Skip to content

How to get a random number in Godot 4: randi_range() and randf_range()

By Eduardo Orellana · Updated

randi_range(1, 6) gives a random int from 1 to 6, both included, and randf_range(0.0, 1.0) a random float. Godot 3's rand_range() was renamed: it fails to parse with Function "rand_range()" not found in base self. Did you mean to use "randf_range()"?

AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.

Intel or AMD Chromebook, with Linux turned on.

Download for macOSDownload for WindowsDownload for Linux

What Godot is checking

Godot 4 seeds the global random generator at startup, so randi_range() differs every run without a randomize() call. Call seed(n) to make a sequence repeatable, as the fix does so its output stays the same.

For an independent stream, such as level generation that must not shift when gameplay rolls dice, make a RandomNumberGenerator and set its seed.

Reproduce it

A Godot 3 dice roll, in a Godot 4 project.

GDScript
extends Node

func _ready():
	print(rand_range(1, 6))

What Godot 4.7.2 prints

Output
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

SCRIPT ERROR: Parse Error: Function "rand_range()" not found in base self. Did you mean to use "randf_range()"?
   at: GDScript::reload (res://answers/how-to-get-a-random-number-in-godot-4/repro.gd:4)
ERROR: Failed to load script "res://answers/how-to-get-a-random-number-in-godot-4/repro.gd" with error "Parse error".
   at: load (modules/gdscript/gdscript_resource_format.cpp:46)

Fix it

Seeded rolls, so this run prints the same numbers every time, and a separate generator.

GDScript
extends Node

func _ready() -> void:
	seed(42)
	var rolls: Array[int] = []
	for i in 5:
		rolls.append(randi_range(1, 6))
	print("dice: ", rolls)
	var rng := RandomNumberGenerator.new()
	rng.seed = 7
	print("float: ", snappedf(rng.randf_range(0.0, 1.0), 0.001))
	print("pick: ", ["sword", "bow", "staff"][rng.randi() % 3])

What the fixed script prints

Output
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

dice: [6, 5, 3, 2, 5]
float: 0.432
pick: bow

Run this fix with your agent

Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.

Prompt
How to get a random number in Godot 4: randi_range() and randf_range() randi_range(1, 6) gives a random int from 1 to 6, both included, and randf_range(0.0, 1.0) a random float. Godot 3's rand_range() was renamed: it fails to parse with Function "rand_range()" not found in base self. Did you mean to use "randf_range()"? Where this calls for a change in my Godot 4 project, make it, then run the game headless and check Godot's output for errors.

Verified on Godot 4.7.2

Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.

Where the check lives in Godot's source: gdscript_analyzer.cpp line 3767, the message with its hint, randi_range and randf_range in the @GlobalScope class reference.

Questions

Do I still need randomize()?
No. Godot 4 seeds the generator randomly at startup. randomize() still exists to reseed it.
How do I pick a random element from an array?
array.pick_random(). It uses the global generator.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free