Random#
Introduction#
Random provides access to Python’s random
module, which is available by default in VEXcode GO. 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.
randint#
randint
returns a random integer between two values (inclusive), where both values are integers.
Usage:
random.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. |
def main():
# Generate a random integer between
# 1 and 10 (inclusive)
console.print(random.randint(1, 10))
# Start threads — Do not delete
start_thread(main)
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. |
def main():
# Generate a random float between 1 and 10
console.print(random.uniform(1, 10), precision=4)
# Start threads — Do not delete
start_thread(main)
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 |
---|---|
|
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. |
def main():
# Generate a random integer from 0 to 9
console.print(random.randrange(10))
# Start threads — Do not delete
start_thread(main)
def main():
# Generate a random integer from 5 to 14
console.print(random.randrange(5, 15))
# Start threads — Do not delete
start_thread(main)
def main():
# Generate a random integer from 10 to 50, stepping
# by 5
console.print(random.randrange(10, 50, 5))
# Start threads — Do not delete
start_thread(main)
random#
random
returns a random float between 0.0 (inclusive) and 1.0 (exclusive).
Usage:
random.random()
Parameter |
Description |
---|---|
This method has no parameters. |
def main():
# Generate a random float between 0.0 and 1.0
console.print(random.random(), precision=1)
# Start threads — Do not delete
start_thread(main)
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 |
---|---|
|
An integer representing the number of random bits |
def main():
# Generate a random integer with 8 random bits
# (integer between 0 and 255)
console.print(random.getrandbits(8))
# Start threads — Do not delete
start_thread(main)
choice#
choice
returns a random element from a non-empty list or sequence.
Usage:
random.choice(sequence)
Parameter |
Description |
---|---|
|
A non-empty sequence (list, tuple, or other indexable object) from which a random element is selected. |
def main():
# Choose a random number from a list
console.print(random.choice([10, 20, 30, 40, 50]))
# Start threads — Do not delete
start_thread(main)