Random#
Introduction#
Random provides access to Python’s urandom 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.
Below is a list of available methods:
Number Generators#
randint#
randint returns a random integer between two values (inclusive), where both values are integers.
Usage:
urandom.randint(a, b)
Parameter |
Description |
|---|---|
|
An integer representing the inclusive lower bound of the range. |
|
An integer representing the inclusive upper bound of the range. |
# 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)
Parameter |
Description |
|---|---|
|
A float or integer representing the lower bound of the range. |
|
A float or integer representing the upper bound of the range. |
# 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:
urandom.randrange(start, stop, step)
Parameter |
Description |
|---|---|
|
Optional. An integer representing the inclusive starting value of the range. Default is 0. |
|
An integer representing the exclusive ending value of the range. |
|
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)
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)