Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
RandomNumberGenerator
Hérite de : RefCounted < Object
Fournit des méthodes pour générer des nombres pseudo-aléatoires.
Description
RandomNumberGenerator est une classe pour générer des nombres pseudo-aléatoires. Elle utilise actuellement l'algorithme PCG32.
Note : L'implémentation de l'algorithme est un détail d'implémentation, sur lequel il ne faudrait pas se baser.
Pour générer un flottant aléatoire (dans un intervalle donné) basé sur une graine dépendante du temps :
var rng = RandomNumberGenerator.new()
func _ready():
var mon_nombre_aleatoire = rng.randf_range(-10.0, 10.0)
Tutoriels
Propriétés
|
||
|
Méthodes
rand_weighted(weights: PackedFloat32Array) |
|
randf() |
|
randf_range(from: float, to: float) |
|
randi() |
|
randi_range(from: int, to: int) |
|
void |
Descriptions des propriétés
Initializes the random number generator state based on the given seed value. A given seed will give a reproducible sequence of pseudo-random numbers.
Note: The RNG does not have an avalanche effect, and can output similar random streams given similar seeds. Consider using a hash function to improve your seed quality if they're sourced externally.
Note: The default value of this property is pseudo-random, and changes when calling randomize(). The 0 value documented here is a placeholder, and not the actual default seed.
Note: Setting this property produces a side effect of changing the internal state, so make sure to initialize the seed before modifying the state:
var rng = RandomNumberGenerator.new()
rng.seed = hash("Godot")
rng.state = 100 # Restore to some previously saved state.
L'état actuel du générateur de nombres aléatoires. Enregistrez puis restaurez cette propriété pour restorer l'état du générateur à un état précédent :
var rng = RandomNumberGenerator.new()
print(rng.randf())
var etat_sauvegarde = rng.state # Enregistre l'état actuel.
print(rng.randf()) # Avance l'état interne.
rng.state = etat_sauvegarde # Restaure l'état.
print(rng.randf()) # Affiche la même valeur que précédemment.
Note : Ne définissez pas l'état sauvegardé à des valeurs arbitraires, car le générateur de nombre aléatoires a besoin que l'état ait des propriétés particulières pour se comporter correctement. Cet état ne devrait être défini qu'à partir de valeurs qui proviennent de la propriété de l'état lui-même. Pour initialiser le générateur avec une entrée arbitraire, utilisez plutôt seed.
Note : La valeur par défaut de cette propriété est pseudo-aléatoire, et change lors de l'appel à randomize(). La valeur de 0 documentée ici est un placeholder, et non l'état par défaut réel.
Descriptions des méthodes
int rand_weighted(weights: PackedFloat32Array) 🔗
Returns a random integer between 0 and the size of the array that is passed as a parameter. Each value in the array should be a floating-point number that represents the relative likelihood that it will be returned as an index. A higher value means the value is more likely to be returned as an index, while a value of 0 means it will never be returned as an index.
For example, if [0.5, 1, 1, 2] is passed as a parameter, then the method is twice as likely to return 3 (the index of the value 2) and twice as unlikely to return 0 (the index of the value 0.5) compared to the indices 1 and 2.
Prints an error and returns -1 if the array is empty.
var rng = RandomNumberGenerator.new()
var my_array = ["one", "two", "three", "four"]
var weights = PackedFloat32Array([0.5, 1, 1, 2])
# Prints one of the four elements in `my_array`.
# It is more likely to print "four", and less likely to print "one".
print(my_array[rng.rand_weighted(weights)])
Renvoie un flottant pseudo-aléatoire entre 0.0 et 1.0 (inclus).
float randf_range(from: float, to: float) 🔗
Renvoie un flottant pseudo-aléatoire entre from et to (inclus).
float randfn(mean: float = 0.0, deviation: float = 1.0) 🔗
Renvoie un flottant pseudo-aléatoire, distribué normalement, depuis la moyenne mean et l'écart-type deviation spécifiés. Ceci est également connu en tant que loi gaussienne.
Note : Cette méthode utilise l'algorithme de la méthode de Box-Muller.
Renvoie un entier non signé de 32 bits pseudo-aléatoire compris entre 0 et 4294967295 (inclus).
int randi_range(from: int, to: int) 🔗
Renvoie un entier signé de 32 bits pseudo-aléatoire compris entre from et to (inclus).
void randomize() 🔗
Définit une graine basée sur le temps pour cette instance RandomNumberGenerator. Contrairement aux fonctions de génération de nombres aléatoires de @GlobalScope, différentes instances de RandomNumberGenerator peuvent utiliser des graines différentes.