pyxen.random
Generate random numbers. Available globally via from pyxen import *.
random.seed(42)
value = random.random() # float in [0, 1)
roll = random.randint(1, 6) # int in [1, 6]
pick = random.choice(items) # random element from sequence
Methods
| Method | Returns | Description |
|---|---|---|
seed(n) | None | Set the random seed for reproducible results |
random() | float | Random float in [0, 1) |
randint(a, b) | int | Random integer in [a, b] inclusive |
choice(seq) | element | Random element from a sequence |
seed(n)
random.seed(42)
Set the internal state of the random number generator. Using the same seed produces the same sequence of numbers every time.
This is useful for:
- Reproducible procedural generation
- Deterministic game behavior for debugging
- Consistent level layouts across runs
random()
value = random.random()
Returns a random float between 0 (inclusive) and 1 (exclusive).
Example — random velocity in any direction:
vx = (random.random() * 2.0) - 1.0 # range [-1, 1)
vy = (random.random() * 2.0) - 1.0
randint(a, b)
roll = random.randint(1, 6)
Returns a random integer N such that a <= N <= b. Both endpoints are included.
Example — 50% chance:
if random.randint(0, 1) == 1:
spawn_coin()
Example — random tile variant:
random.seed(42)
for col in range(COLS):
alt = random.randint(0, 99) <= 10 # 10% chance
tile = (1 if alt else 0, 4)
choice(seq)
color = random.choice(["red", "green", "blue"])
Returns a random element from a non-empty sequence (list, tuple, or string).
Raises IndexError if the sequence is empty.
Example — random spawn position:
spawns = [(10, 20), (50, 80), (120, 40)]
x, y = random.choice(spawns)