# 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()"?

Source: https://flockbay.com/godot/how-to-get-a-random-number-in-godot-4/ · Updated 2026-10-30

## 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
```

## 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.

```
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](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.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [How to get the current date and time in Godot 4: the Time singleton](/godot/how-to-get-the-current-date-and-time-in-godot-4/), [OpenSimplexNoise in Godot 4: it is FastNoiseLite now](/godot/opensimplexnoise-in-godot-4/), [How to parse JSON in Godot 4: JSON.parse_string()](/godot/how-to-parse-json-in-godot-4/), [Function "…()" not found in base self in Godot 4](/godot/function-not-found-in-base-self/), [Every verified Godot 4 answer](/godot/), [How do I connect Claude Code to Godot?](/docs/how-do-i-connect-claude-code-to-godot/)
