Aleatorio#
Introducción#
Random provides access to Python’s urandom module, which is available by default in VEXcode V5. These methods let you generate random numbers, floats, and values from lists or ranges—useful for adding unpredictability to games, behaviors, and autonomous decision-making.
A continuación se muestra una lista de los métodos disponibles:
randint#
randint returns a random integer between two values (inclusive), where both values are integers.
Usage:
urandom.randint(a, b)
Parámetro |
Descripción |
|---|---|
|
Un número entero que representa el límite inferior inclusivo del rango. |
|
Un número entero que representa el límite superior inclusivo del rango. |
# Generate a random integer between
# 1 and 10 (inclusive)
urandom_int = urandom.randint(1, 10)
brain.screen.print(urandom_int)
# urandom_int = (random integer between 1 and 10)
uniforme#
uniform returns a random float between two values, where both values are floats or integers.
Usage:
urandom.uniform(start, end)
Parámetro |
Descripción |
|---|---|
|
Un flotante o entero que representa el límite inferior del rango. |
|
Un flotante o entero que representa el límite superior del rango. |
# Generate a random float
# between 5.0 and 10.0
urandom_uniform = urandom.uniform(5.0, 10.0)
brain.screen.print(urandom_uniform)
# urandom_uniform = (random float
# between 5.0 and 10.0)
rango de rand#
randrange returns a random integer from a range with an optional step, where both the start and stop values are integers.
Usage:
urandom.randrange(start, stop, step)
Parámetro |
Descripción |
|---|---|
|
Opcional. Un entero que representa el valor inicial inclusivo del rango. El valor predeterminado es 0. |
|
Un número entero que representa el valor final exclusivo del rango. |
|
Opcional. Un entero que representa la diferencia entre cada número del rango. El valor predeterminado es 1. |
# Generate a random integer from
# 0 to 9 (exclusive of 10)
urandom_range = urandom.randrange(10)
brain.screen.print(urandom_range)
# urandom_range = (random integer from 0 to 9)
# Generate a random integer from
# 5 to 15 (exclusive of 15)
urandom_range = urandom.randrange(5, 15)
brain.screen.print(urandom_range)
# urandom_range = (random integer from 5 to 14)
# Generate a random integer from
# 10 to 50, stepping by 5
urandom_range = urandom.randrange(10, 50, 5)
brain.screen.print(urandom_range)
# urandom_range = (random multiple of 5 between 10 and 45)
semilla#
seed sets the starting value for urandom’s pseudo-random number generator.
Si no establece una semilla, su proyecto puede producir la misma secuencia aleatoria cada vez que se inicie.
Para obtener resultados diferentes en cada ejecución, configure la semilla una vez al comienzo del proyecto usando un valor que cambie entre ejecuciones, como lecturas de batería, un valor de temporizador o una combinación de ambos.
Usage:
urandom.seed(seed)
Parámetro |
Descripción |
|---|---|
|
Un número entero utilizado para iniciar la secuencia pseudoaleatoria. |
# Set a different seed each time the project starts
urandom.seed(int(brain.battery.current()))