Aleatorio#

Introducción#

Random provides access to Python’s random module, which is available by default in VEXcode IQ (2nd gen). 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 – Devuelve un entero aleatorio entre dos valores (inclusive).

  • uniforme – Devuelve un punto flotante aleatorio entre dos valores.

  • randrange – Devuelve un entero aleatorio de un rango con un paso opcional.

  • random – Devuelve un flotante aleatorio entre 0.0 (inclusive) y 1.0 (exclusivo).

  • getrandbits – Devuelve un entero con una cantidad específica de bits aleatorios.

  • choice – Devuelve un elemento aleatorio de una lista o secuencia no vacía.

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.

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

uniform#

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

Usage:
urandom.uniform(start, end)

Parámetro

Descripción

start

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

end

Un número 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)

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.

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

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.

# Generate a random float between 0.0 and 1.0
urandom_float = urandom.random()
brain.screen.print(urandom_float)

# urandom_float = (random float between 0.0 and 1.0)

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

# Generate a random integer with 8 random bits
urandom_bits = urandom.getrandbits(8)
brain.screen.print(urandom_bits)

# urandom_bits = (random integer between 0 and 255)

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.

# Choose a random number from a list
urandom_choice = urandom.choice([10, 20, 30, 40, 50])
brain.screen.print(urandom_choice)

# urandom_choice = (randomly chosen number from the list)