ランダム#
導入#
Random 関数は、VEXcode VR にデフォルトで搭載されている Python の random へのアクセスを提供します。これらのメソッドを使用すると、乱数、浮動小数点数、リストまたは範囲からの値などを生成できます。これは、ゲーム、動作、自律的な意思決定に予測不可能性を加えるのに役立ちます。
利用可能なメソッドの一覧は以下のとおりです。
乱数発生器 – ランダムな整数、浮動小数点数、ビットを生成します。
randint– 2 つの値の間のランダムな整数を返します (両端を含む)。uniform– 2 つの値の間のランダムな浮動小数点数を返します。randrange– オプションのステップを指定して、範囲からランダムな整数を返します。random– 0.0 (含む) から 1.0 (含まない) までのランダムな浮動小数点数を返します。getrandbits– 指定された数のランダムビットを持つ整数を返します。
選択 – リストやシーケンスからランダムに要素を選択します。
choice– 空でないリストまたはシーケンスからランダムな要素を返します。
乱数発生器#
randint#
randint は、2 つの値(両端を含む)の間のランダムな整数を返します。ただし、両方の値は整数です。
Usage:
random.randint(a, b)
パラメータ |
説明 |
|---|---|
|
範囲の下限値を表す整数。 |
|
範囲の上限を表す整数。 |
def main():
# Generate a random integer between
# 1 and 10 (inclusive)
random_int = random.randint(1, 10)
brain.print(random_int)
# random_int = (random integer between 1 and 10)
# VR threads — Do not delete
vr_thread(main)
uniform#
uniform 、2 つの値の間のランダムな浮動小数点数を返します。ここで、両方の値は浮動小数点数または整数です。
使用例:
random.uniform(start, end)
パラメータ |
説明 |
|---|---|
|
範囲の下限を表す浮動小数点数または整数。 |
|
範囲の上限を表す浮動小数点数または整数。 |
def main():
# Generate a random float
# between 5.0 and 10.0
random_uniform = random.uniform(5.0, 10.0)
brain.print(random_uniform, precision=1)
# random_uniform = (random float
# between 5.0 and 10.0)
# VR threads — Do not delete
vr_thread(main)
randrange#
randrange 、オプションのステップを含む範囲からランダムな整数を返します。開始値と終了値はどちらも整数です。
Usage:
random.randrange(start, stop, step)
パラメータ |
説明 |
|---|---|
|
オプション。範囲の開始値(両端を含む)を表す整数。デフォルト値は0です。 |
|
範囲の唯一の終了値を表す整数。 |
|
オプション。範囲内の各数値間の差を表す整数。デフォルト値は1です。 |
def main():
# Generate a random integer from
# 0 to 9 (exclusive of 10)
random_range = random.randrange(10)
brain.print(random_range)
# random_range = (random integer from 0 to 9)
# VR threads — Do not delete
vr_thread(main)
def main():
# Generate a random integer from
# 5 to 15 (exclusive of 15)
random_range = random.randrange(5, 15)
brain.print(random_range)
# random_range = (random integer from 5 to 14)
# VR threads — Do not delete
vr_thread(main)
def main():
# Generate a random integer from
# 10 to 50, stepping by 5
random_range = random.randrange(10, 50, 5)
brain.print(random_range)
# random_range = (random multiple of 5 between 10 and 45)
# VR threads — Do not delete
vr_thread(main)
random#
random 、0.0 (含む) から 1.0 (含まない) までのランダムな浮動小数点数を返します。
Usage:
random.random()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
def main():
# Generate a random float between 0.0 and 1.0
random_float = random.random()
brain.print(random_float, precision=1)
# random_float = (random float between 0.0 and 1.0)
# VR threads — Do not delete
vr_thread(main)
getrandbits#
getrandbits は、指定された数のランダムビットを持つ整数を返します。ビット数は 0 から 32 までの整数です。
Usage:
random.getrandbits(n)
パラメータ |
説明 |
|---|---|
|
乱数ビット数を表す整数 |
def main():
# Generate a random integer with 8 random bits
random_bits = random.getrandbits(8)
brain.print(random_bits)
# random_bits = (random integer between 0 and 255)
# VR threads — Do not delete
vr_thread(main)
選択#
choice#
choice は、空でないリストまたはシーケンスからランダムな要素を返します。
Usage:
random.choice(sequence)
パラメータ |
説明 |
|---|---|
|
ランダムな要素が選択される、空でないシーケンス(リスト、タプル、またはその他のインデックス可能なオブジェクト)。 |
def main():
# Choose a random number from a list
random_choice = random.choice([10, 20, 30, 40, 50])
brain.print(random_choice)
# random_choice = (randomly chosen number from the list)
# VR threads — Do not delete
vr_thread(main)