Aleatorio#

Introducción#

Random provides access to Python’s random module, which is available by default in VEXcode VR. 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:

Generadores de números: genere números enteros, flotantes y 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 – Selecciona elementos aleatorios de listas y secuencias.

  • choice – Returns a random element from a non-empty list or sequence.

Generadores de números#

randint#

randint returns a random integer between two values (inclusive), where both values are integers.

Usage:
random.randint(a, b)

Parámetro

Descripción

a

Un número entero que representa el límite inferior inclusivo del rango.

b

Un número entero que representa el límite superior inclusivo del rango.

def main():
    # Generate a random integer between
    # 1 and 10 (inclusive)
    random_int = random.randint(1, 10)
    brain.print(random_int)

    # random_int = (random integer between 1 and 10)

# VR threads — Do not delete
vr_thread(main)

uniform#

uniform returns a random float between two values, where both values are floats or integers.

Uso:

random.uniform(start, end)

Parámetro

Descripción

start

Un flotante o entero que representa el límite inferior del rango.

end

Un flotante o entero que representa el límite superior del rango.

def main():
    # Generate a random float 
    # between 5.0 and 10.0
    random_uniform = random.uniform(5.0, 10.0)
    brain.print(random_uniform, precision=1)

    # random_uniform = (random float 
    # between 5.0 and 10.0)

# VR threads — Do not delete
vr_thread(main)

randrange#

randrange returns a random integer from a range with an optional step, where both the start and stop values are integers.

Usage:
random.randrange(start, stop, step)

Parámetro

Descripción

start

Opcional. Un entero que representa el valor inicial inclusivo del rango. El valor predeterminado es 0.

stop

Un número entero que representa el valor final exclusivo del rango.

step

Opcional. Un entero que representa la diferencia entre cada número del rango. El valor predeterminado es 1.

def main():
    # Generate a random integer from
    # 0 to 9 (exclusive of 10)
    random_range = random.randrange(10)
    brain.print(random_range)

    # random_range = (random integer from 0 to 9)

# VR threads — Do not delete
vr_thread(main)

def main():
    # Generate a random integer from
    # 5 to 15 (exclusive of 15)
    random_range = random.randrange(5, 15)
    brain.print(random_range)

    # random_range = (random integer from 5 to 14)

# VR threads — Do not delete
vr_thread(main)

def main():
    # Generate a random integer from
    # 10 to 50, stepping by 5
    random_range = random.randrange(10, 50, 5)
    brain.print(random_range)

    # random_range = (random multiple of 5 between 10 and 45)

# VR threads — Do not delete
vr_thread(main)

random#

random returns a random float between 0.0 (inclusive) and 1.0 (exclusive).

Usage:
random.random()

Parámetro

Descripción

Este método no tiene parámetros.

def main():
    # Generate a random float between 0.0 and 1.0
    random_float = random.random()
    brain.print(random_float, precision=1)

    # random_float = (random float between 0.0 and 1.0)

# VR threads — Do not delete
vr_thread(main)

getrandbits#

getrandbits returns an integer with a specified number of random bits, where the number of bits is an integer between 0 and 32.

Usage:
random.getrandbits(n)

Parámetro

Descripción

n

An integer representing the number of random bits (0 <= n <= 32).

def main():
    # Generate a random integer with 8 random bits
    random_bits = random.getrandbits(8)
    brain.print(random_bits)

    # random_bits = (random integer between 0 and 255)

# VR threads — Do not delete
vr_thread(main)

Selección#

choice#

choice returns a random element from a non-empty list or sequence.

Usage:
random.choice(sequence)

Parámetro

Descripción

sequence

Una secuencia no vacía (lista, tupla u otro objeto indexable) de la que se selecciona un elemento aleatorio.

def main():
    # Choose a random number from a list
    random_choice = random.choice([10, 20, 30, 40, 50])
    brain.print(random_choice)

    # random_choice = (randomly chosen number from the list)

# VR threads — Do not delete
vr_thread(main)