Aleatorio#
Introducción#
Random provides access to Python’s random
module, which is available by default in VEXcode AIR. 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. To begin using these functions, add import random
at the top of the project.
A continuación se muestra una lista de los métodos disponibles:
Generadores de números: producen directamente números o bits aleatorios.
randint
– Returns a random integer between two values (inclusive).uniform
– Returns a random float between two values.randrange
– Returns a random integer from a range with an optional step.random
– Returns a random float between 0.0 (inclusive) and 1.0 (exclusive).getrandbits
– Returns an integer with a specified number of random bits.
Selección: elige elementos al azar de las secuencias.
choice
– Returns a random element from a non-empty list or sequence.
Control de estado: administre o inicialice el estado del generador de números aleatorios.
seed
– Sets the seed for the random number generator to produce deterministic results.
Generadores de números#
randint#
randint
returns a random integer in the range a
(inclusive) to b
(inclusive).
Usage:
random.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. |
# Pick a number from 0 to 100
random_int = random.randint(0, 100)
controller.screen.print(random_int)
#random_int = (random integer between 0-100)
uniform#
uniform
returns a random float between start
and end
.
Usage:
random.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
random_uniform = random.uniform(5.0, 10.0)
controller.screen.print(random_uniform)
# random_uniform = (random float between 5.0 and 10.0)
randrange#
randrange
returns a random integer from the specified range.
Usage:
random.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)
random_range = random.randrange(10)
controller.screen.print(random_range)
# random_range = (random integer from 0 to 9)
# Generate a random integer from
# 5 to 15 (exclusive of 15)
random_range = random.randrange(5, 15)
controller.screen.print(random_range)
# random_range = (random integer from 5 to 14)
random#
random
returns a random float in the range 0.0 (inclusive) to 1.0
(exclusive).
Usage:
random.random()
Parámetro |
Descripción |
---|---|
Este método no tiene parámetros. |
# Generate a random float between 0.0 and 1.0
random_float = random.random()
controller.screen.print(random_float)
# random_float = (random float between 0.0 and 1.0)
getrandbits#
getrandbits
returns an integer with a specified number of random bits.
Usage:
random.getrandbits(n)
Parámetro |
Descripción |
---|---|
|
An integer representing the number of random bits |
# Generate a random integer with 8 random bits
random_bits = random.getrandbits(8)
controller.screen.print(random_bits)
# random_bits = (random integer between 0 and 255)
Selección#
choice#
choice
chooses and returns one item at random from a sequence.
Usage:
random.choice(sequence)
Parámetro |
Descripción |
---|---|
|
Una secuencia no vacía (lista, tupla u otro objeto indexable) de la que se selecciona un elemento aleatorio. |
# Choose a random number from a list
random_choice = random.choice([10, 20, 30, 40, 50])
controller.screen.print(random_choice)
# random_choice = (randomly chosen number from the list)
Control estatal#
seed#
seed
initializes the random number generator, setting its state so that random results can be reproduced.
Usage:
random.seed(a=None)
Parámetro |
Descripción |
---|---|
|
Optional. The seed value. Can be an int, float, str, bytes, or bytearray. If |
random.seed(42)
# Always return the same results
print(random.randint(1, 10))
print(random.random())