Random#

Introduction#

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.

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 in the range a (inclusive) to b (inclusive).

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.

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

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

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

Parameter

Description

This method has no parameters.

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

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)
controller.screen.print(random_bits)

# random_bits = (random integer between 0 and 255)

Selection#

choice#

choice chooses and returns one item at random from a 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])
controller.screen.print(random_choice)

# random_choice = (randomly chosen number from the list)