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 Windows

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.

extends Node

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

What Godot 4.7.2 prints

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.

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

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

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

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](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L3767), [randi_range and randf_range in the @GlobalScope class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/@GlobalScope.xml).

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.