pyxen.random
乱数を生成します。from pyxen import *でグローバルに利用可能です。
random.seed(42)
value = random.random() # [0, 1)のfloat
roll = random.randint(1, 6) # [1, 6]のint
pick = random.choice(items) # シーケンスからランダムな要素
メソッド
| メソッド | 戻り値 | 説明 |
|---|---|---|
seed(n) | None | 再現可能な結果のためにランダムシードを設定 |
random() | float | [0, 1)のランダムなfloat |
randint(a, b) | int | [a, b]のランダムな整数(両端を含む) |
choice(seq) | 要素 | シーケンスからランダムな要素 |
seed(n)
random.seed(42)
乱数生成器の内部状態を設定します。同じシードを使用すると、毎回同じ数値列が生成されます。
用途:
- 再現可能なプロシージャル生成
- デバッグのための決定的なゲーム動作
- 実行間で一貫したレベルレイアウト
random()
value = random.random()
0(含む)と1(含まない)の間のランダムなfloatを返します。
例 — 任意の方向のランダムな速度:
vx = (random.random() * 2.0) - 1.0 # 範囲 [-1, 1)
vy = (random.random() * 2.0) - 1.0
randint(a, b)
roll = random.randint(1, 6)
a <= N <= bを満たすランダムな整数Nを返します。両端を含みます。
例 — 50%の確率:
if random.randint(0, 1) == 1:
spawn_coin()
例 — ランダムなタイルバリエーション:
random.seed(42)
for col in range(COLS):
alt = random.randint(0, 99) <= 10 # 10%の確率
tile = (1 if alt else 0, 4)
choice(seq)
color = random.choice(["red", "green", "blue"])
空でないシーケンス(リスト、タプル、または文字列)からランダムな要素を返します。
シーケンスが空の場合IndexErrorが発生します。
例 — ランダムなスポーン位置:
spawns = [(10, 20), (50, 80), (120, 40)]
x, y = random.choice(spawns)