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.

RandomNumberGenerator

Eredita: RefCounted < Object

Fornisce metodi per generare numeri pseudocasuali.

Descrizione

RandomNumberGenerator è una classe per generare numeri pseudocasuali. Attualmente utilizza PCG32.

Nota: L'algoritmo sottostante è un dettaglio di implementazione e non ci si dovrebbe basare su di esso.

Per generare un numero float casuale (entro un dato intervallo) basato su un seed dipendente dal tempo:

var rng = RandomNumberGenerator.new()
func _ready():
    var my_random_number = rng.randf_range(-10.0, 10.0)

Tutorial

Proprietà

int

seed

0

int

state

0

Metodi

int

rand_weighted(weights: PackedFloat32Array)

float

randf()

float

randf_range(from: float, to: float)

float

randfn(mean: float = 0.0, deviation: float = 1.0)

int

randi()

int

randi_range(from: int, to: int)

void

randomize()


Descrizioni delle proprietà

int seed = 0 🔗

  • void set_seed(value: int)

  • int get_seed()

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.

int state = 0 🔗

  • void set_state(value: int)

  • int get_state()

Lo stato attuale del generatore di numeri casuali. Salva e ripristina questa proprietà per ripristinare il generatore a uno stato precedente:

var rng = RandomNumberGenerator.new()
print(rng.randf())
var saved_state = rng.state # Memorizza lo stato attuale.
print(rng.randf()) # Avanza lo stato interno.
rng.state = saved_state # Ripristina lo stato.
print(rng.randf()) # Stampa lo stesso valore di prima.

Nota: Non impostare lo stato su valori arbitrari, poiché il generatore di numeri casuali richiede che lo stato abbia determinate qualità per comportarsi correttamente. Dovrebbe essere impostato solo su valori che provengono dalla proprietà dello stato stessa. Per inizializzare il generatore di numeri casuali con input arbitrari, usa invece seed.

Nota: Il valore predefinito di questa proprietà è pseudocasuale e cambia quando si chiama randomize(). Il valore 0 qui documentato è un segnaposto e non lo stato predefinito effettivo.


Descrizioni dei metodi

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)])

float randf() 🔗

Restituisce un numero float pseudocasuale compreso tra 0.0 e 1.0 (inclusi).


float randf_range(from: float, to: float) 🔗

Restituisce un numero float pseudocasuale compreso tra from e to (inclusi).


float randfn(mean: float = 0.0, deviation: float = 1.0) 🔗

Restituisce un numero in virgola mobile pseudo-casuale normalmente distribuito dalla media (mean) specificata e una deviazione (deviation) standard. Questa è anche nota come distribuzione gaussiana.

Nota: Questo metodo utilizza l'algoritmo trasformazione di Box-Muller.


int randi() 🔗

Restituisce un intero pseudocasuale senza segno a 32 bit compreso tra 0 e 4294967295 (inclusi).


int randi_range(from: int, to: int) 🔗

Restituisce un intero pseudocasuale con segno a 32 bit compreso tra from e to (inclusi).


void randomize() 🔗

Imposta un seed basato sul tempo per questa istanza di RandomNumberGenerator. A differenza delle funzioni di generazione di numeri casuali @GlobalScope, diverse istanze di RandomNumberGenerator possono usare seed diversi.