Up to date
This page is up to date for Godot 4.1
.
If you still find outdated information, please open an issue.
Random number generation¶
Many games rely on randomness to implement core game mechanics. This page guides you through common types of randomness and how to implement them in Godot.
After giving you a brief overview of useful functions that generate random numbers, you will learn how to get random elements from arrays, dictionaries, and how to use a noise generator in GDScript. Lastly, we'll take a look at cryptographically secure random number generation and how it differs from typical random number generation.
Note
Computers cannot generate "true" random numbers. Instead, they rely on pseudorandom number generators (PRNGs).
Godot internally uses the PCG Family of pseudorandom number generators.
Global scope versus RandomNumberGenerator class¶
Godot exposes two ways to generate random numbers: via global scope methods or using the RandomNumberGenerator class.
Global scope methods are easier to set up, but they don't offer as much control.
RandomNumberGenerator requires more code to use, but allows creating multiple instances, each with their own seed and state.
This tutorial uses global scope methods, except when the method only exists in the RandomNumberGenerator class.
The randomize() method¶
Note
Since Godot 4.0, the random seed is automatically set to a random value when
the project starts. This means you don't need to call randomize()
in
_ready()
anymore to ensure that results are random across project runs.
However, you can still use randomize()
if you want to use a specific
seed number, or generate it using a different method.
In global scope, you can find a randomize() method. This method should be called only once when your project starts to initialize the random seed. Calling it multiple times is unnecessary and may impact performance negatively.
Putting it in your main scene script's _ready()
method is a good choice:
func _ready():
randomize()
public override void _Ready()
{
GD.Randomize();
}
You can also set a fixed random seed instead using seed(). Doing so will give you deterministic results across runs:
func _ready():
seed(12345)
# To use a string as a seed, you can hash it to a number.
seed("Hello world".hash())
public override void _Ready()
{
GD.Seed(12345);
GD.Seed("Hello world".Hash());
}
When using the RandomNumberGenerator class, you should call randomize()
on
the instance since it has its own seed:
var random = RandomNumberGenerator.new()
random.randomize()
var random = new RandomNumberGenerator();
random.Randomize();
Getting a random number¶
Let's look at some of the most commonly used functions and methods to generate random numbers in Godot.
The function randi() returns a random
number between 0 and 2^32-1. Since the maximum value is huge, you most likely
want to use the modulo operator (%
) to bound the result between 0 and the
denominator:
# Prints a random integer between 0 and 49.
print(randi() % 50)
# Prints a random integer between 10 and 60.
print(randi() % 51 + 10)
<