Random#
Introduction#
Random provides access to Python’s urandom module, which is available by default in VEXcode V5. 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#
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)
seed#
seed sets the starting value for urandom’s pseudo-random number generator.
If you don’t set a seed, your project may produce the same random sequence each time it starts.
To get different results on each run, set the seed once at the beginning of the project using a value that changes between runs—such as battery readings, a timer value, or a combination of both.
Usage:
urandom.seed(seed)
Parameter |
Description |
|---|---|
|
An integer used to start the pseudo-random sequence. |
# Set a different seed each time the project starts
urandom.seed(int(brain.battery.current()))