随机的#

介绍#

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.

以下是可用方法的列表:

数字生成器——直接生成随机数或位。

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

状态控制——管理或初始化随机数生成器的状态。

  • seed – Sets the seed for the random number generator to produce deterministic results.

数字生成器#

randint#

randint returns a random integer in the range a (inclusive) to b (inclusive).

Usage:
random.randint(a, b)

范围

描述

a

表示范围的包含下限的整数。

b

表示范围上限的整数。

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

范围

描述

start

表示范围下限的浮点数或整数。

end

表示范围上限的浮点数或整数。

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

范围

描述

start

可选。一个整数,表示范围的起始值(含)。默认值为 0。

stop

表示范围的唯一结束值的整数。

step

可选。一个整数,表示范围内每个数字之间的差值。默认值为 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()

范围

描述

该方法没有参数。

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

范围

描述

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)

选择#

choice#

choice chooses and returns one item at random from a sequence.

Usage:
random.choice(sequence)

范围

描述

sequence

从中选择随机元素的非空序列(列表、元组或其他可索引对象)。

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

国家控制#

seed#

seed initializes the random number generator, setting its state so that random results can be reproduced.

Usage:
random.seed(a=None)

范围

描述

a

Optional. The seed value. Can be an int, float, str, bytes, or bytearray. If None (default), the current system time is used.

random.seed(42)

# Always return the same results
print(random.randint(1, 10))
print(random.random())