Random#

Introduction#

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

Below is a list of available methods:

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

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

Number Generators#

randint#

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

Usage:
random.randint(a, b)

Parameter

Description

a

An integer representing the inclusive lower bound of the range.

b

An integer representing the inclusive upper bound of the range.

# Generate a random integer between
# 1 and 10 (inclusive)
random_int = random.randint(1, 10)
robot.screen.print(random_int)

# random_int = (random integer between 1 and 10)

uniform#

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

Usage:
random.uniform(start, end)

Parameter

Description

start

A float or integer representing the lower bound of the range.

end

A float or integer representing the upper bound of the range.

# Generate a random float between 5.0 and 10.0
random_uniform = random.uniform(5.0, 10.0)
robot.screen.print(random_uniform)

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

Parameter

Description

start

Optional. An integer representing the inclusive starting value of the range. Default is 0.

stop

An integer representing the exclusive ending value of the range.

step

Optional. An integer representing the difference between each number in the range. Default is 1.

# Generate a random integer from
# 0 to 9 (exclusive of 10)
random_range = random.randrange(10)
robot.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)
robot.screen.print(random_range)

# random_range = (random integer from 5 to 14)

# Generate a random integer from
# 10 to 50, stepping by 5
random_range = random.randrange(10, 50, 5)
robot.screen.print(random_range)

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

Parameter

Description

This method has no parameters.

# Generate a random float between 0.0 and 1.0
random_float = random.random()
robot.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, where the number of bits is an integer between 0 and 32.

Usage:
random.getrandbits(n)

Parameter

Description

n

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

# Generate a random integer with 8 random bits
random_bits = random.getrandbits(8)
robot.screen.print(random_bits)

# random_bits = (random integer between 0 and 255)

Selection#

choice#

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

Usage:
random.choice(sequence)

Parameter

Description

sequence

A non-empty sequence (list, tuple, or other indexable object) from which a random element is selected.

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

# random_choice = (randomly chosen number from the list)