method
Random.range
Random.range(low, high) -> int
Returns a random integer between low and high, inclusive of both ends — so Random.range(low: 0, high: 3) can return 0, 1, 2, or 3. Use it to place things randomly on a grid, pick from a list of options by index, or add jitter to positions and timers. The sequence is deterministic for a given seed, so call Random.seed first if you need reproducible runs. To pick a random column across a grid of width GRID_WIDTH, pass high: GRID_WIDTH - 1, since the last valid index is one less than the count.
Parameters
lowthe smallest value that can be returned (inclusive)highthe largest value that can be returned (inclusive)Example
program ScatterPickups {
property Position { column: int = 0, row: int = 0 }
model Pickup { Position }
const GRID_WIDTH: int = 20
const GRID_HEIGHT: int = 15
const TILE_SIZE: int = 16
handler SeedPickups phase Start {
Random.seed(value: 7)
for spawn_index in 0 .. 5 {
spawn Pickup {
Position {
column: Random.range(low: 0, high: GRID_WIDTH - 1),
row: Random.range(low: 0, high: GRID_HEIGHT - 1)
}
}
}
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
for (Position) in query [Position, {Pickup}] {
Screen.fill_rectangle(
x: Position.column * TILE_SIZE,
y: Position.row * TILE_SIZE,
width: TILE_SIZE,
height: TILE_SIZE,
color: Color.Gold)
}
Screen.show()
}
}