Godot has random functions built in, with nothing to import:
var roll := randi_range(1, 6) # 1, 2, 3, 4, 5 or 6
var speed := randf_range(80.0, 120.0) # any float from 80 to 120
var chance := randf() # a float from 0 to 1
if randf() < 0.1:
print("Critical hit!") # about 10% of the time
randi_range() includes both ends. Godot 4 seeds the generator randomly at start, so every run of the game gives different numbers. You do not need randomize(), which Godot 3 required.
Repeatable Results with seed()
seed(number) restarts the sequence from a fixed point. The same seed always gives the same numbers, which is why the examples on this page print the same thing every time you run them:
4
3
5
2
3
Seeds are how games make a daily challenge or a level that every player gets the same, and how you reproduce a bug that only happens with certain rolls.
Random Items, Shuffling and Positions
[2, 4, 3, 1, 5]
key coin
A random point inside an area, for spawning, is two ranges:
var spawn := Vector2(randf_range(0, 1152), randf_range(0, 648))
var direction := Vector2.RIGHT.rotated(randf_range(0, TAU)) # any direction
RandomNumberGenerator
The functions above share one global generator. A RandomNumberGenerator is a separate generator with its own seed, so one system's rolls do not change another's:
75 70
75 70
Give the level generator its own RNG and the map stays the same for a seed, even when combat rolls use the global one in between.
Weighted Random Drops
rand_weighted() picks an index with a chance in proportion to its weight. This drops common items often and rare ones seldom:
common: 709
rare: 236
legendary: 55
Over 1,000 drops the counts land near 70%, 25% and 5%. The weights do not need to add up to 100.
Bell-Curve Values
randfn(mean, deviation) gives normally distributed floats: most results near the mean, few far away. It suits natural variation such as damage spread or the size of spawned rocks: rng.randfn(10.0, 2.0) is usually between 8 and 12.
Quick Reference
| Task | Code |
|---|---|
| Whole number from a to b | randi_range(a, b) |
| Float from a to b | randf_range(a, b) |
| Float from 0 to 1 | randf() |
| Random array item | array.pick_random() |
| Shuffle an array | array.shuffle() |
| Same numbers every run | seed(1234) |
| A separate generator | RandomNumberGenerator.new() |
| Weighted index | rng.rand_weighted(weights) |
| Normal distribution | randfn(mean, deviation) |
Frequently Asked Questions
How do I get a random number in Godot?
randi_range(1, 6) returns a whole number from 1 to 6, both included. randf_range(0.5, 2.0) returns a float in that range, and randf() a float from 0 to 1.
Do I need to call randomize() in Godot 4?
No. Godot 4 seeds the random generator with a random value when the game starts, so numbers differ on every run without randomize(). Call seed(number) when you want the same sequence every time.
How do I pick a random item from an array in Godot?
Call pick_random(): ["coin", "gem", "key"].pick_random(). It returns null for an empty array. shuffle() randomizes the order of the whole array in place.
How do I make a weighted random choice in Godot?
Use RandomNumberGenerator.rand_weighted() with a PackedFloat32Array of weights. It returns an index: with weights [70, 25, 5], index 0 comes up about 70% of the time.