60 lines
No EOL
3.2 KiB
HTML
60 lines
No EOL
3.2 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Random.range — Ludic</title>
|
||
<meta name="description" content="A random integer in the inclusive range [low, high].">
|
||
<link rel="stylesheet" href="base.css">
|
||
<link rel="stylesheet" href="docs.css">
|
||
</head>
|
||
<body>
|
||
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
|
||
<main class="wrap item">
|
||
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="ns-random.html">Random</a> <span>›</span> <span class="here">Random.range</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-method">method</span>
|
||
<h1 id="top">Random.range</h1>
|
||
</div>
|
||
<code class="sig">Random.range(low, high) -> int</code>
|
||
<div class="desc"><p>Returns a random integer between <code>low</code> and <code>high</code>, inclusive of both ends — so <code>Random.range(low: 0, high: 3)</code> can return <code>0</code>, <code>1</code>, <code>2</code>, or <code>3</code>. 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 <code>Random.seed</code> first if you need reproducible runs. To pick a random <code>column</code> across a grid of width <code>GRID_WIDTH</code>, pass <code>high: GRID_WIDTH - 1</code>, since the last valid index is one less than the count.</p></div>
|
||
<div class="params"><h2>Parameters</h2><div class="param" id="param-low"><code class="pname">low</code><span class="pdesc">the smallest value that can be returned (inclusive)</span></div><div class="param" id="param-high"><code class="pname">high</code><span class="pdesc">the largest value that can be returned (inclusive)</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">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()
|
||
}
|
||
}</pre></div>
|
||
|
||
<a class="back" href="api.html">← All symbols</a>
|
||
</main>
|
||
<script src="ludic-highlight.js"></script>
|
||
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
|
||
</body></html> |