Manual:DIL Manual/rnd()
Jump to navigation
Jump to search
rnd()
Function (integer). A random whole number in a range.
rnd(from : integer, to : integer) : integer
Returns a random number from from to to, both included, each equally likely. rnd(1, 100) <= 25 is the usual way to say "a 25% chance".
Parameters
| Name | Type | Meaning |
|---|---|---|
| from | integer | The lowest value. |
| to | integer | The highest value. |
Returns
The random number. When from is bigger than to the two are swapped, so rnd(10, 1) works like rnd(1, 10). When either argument fails (for example a field of a null unit), the call fails and the variable assigned keeps its old value.
Notes
rnd(5, 5)is always 5.- To pick an entry of a list, use
rnd(0, length(list) - 1)as the index.
Example
dilbegin squeak();
code
{
heartbeat := PULSE_SEC * 20;
:loop:
wait(SFB_TICK, TRUE);
if (rnd(1, 100) <= 25)
act("$1n squeaks loudly.", A_SOMEONE, self, null, null, TO_ROOM);
goto loop;
}
dilend
An NPC that squeaks at one tick in four.