Difference between revisions of "Manual:DIL Manual/rnd()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfrnd"></span>
+
= rnd() =
−
  integer rnd ( i1 : integer , i2 : integer )
+
'''Function''' (integer). A random whole number in a range.
−
    i1 : Start of range.
+
 
−
    i2 : End of range.
+
  rnd(from : integer, to : integer) : integer
−
    return: A random integer in an interval from 'i1' to 'i2'.
+
 
−
    Example: i := rnd(1,10);
+
Returns a random number from <code>from</code> to <code>to</code>, both included, each equally likely. <code>rnd(1, 100) <= 25</code> is the usual way to say "a 25% chance".
−
<span id="bfrnd"></span>
+
 
−
integer rnd ( i1 : integer , i2 : integer )
+
== Parameters ==
−
    i1 : Start of range.
+
{| class="wikitable"
−
    i2 : End of range.
+
! Name !! Type !! Meaning
−
    return: A random integer in an interval from 'i1' to 'i2'.
+
|-
−
    Example: i := rnd(1,10);
+
| from || integer || The lowest value.
 +
|-
 +
| to || integer || The highest value.
 +
|}
 +
 
 +
== Returns ==
 +
The random number. When <code>from</code> is bigger than <code>to</code> the two are swapped, so <code>rnd(10, 1)</code> works like <code>rnd(1, 10)</code>. When either argument fails (for example a field of a null unit), the call fails and the variable assigned keeps its old value.
 +
 
 +
== Notes ==
 +
* <code>rnd(5, 5)</code> is always 5.
 +
* To pick an entry of a list, use <code>rnd(0, length(list) - 1)</code> as the index.
 +
 
 +
== Example ==
 +
<pre>
 +
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
 +
</pre>
 +
An NPC that squeaks at one tick in four.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/openroll()|openroll()]]
 +
* [[Manual:DIL Manual/findrndunit()|findrndunit()]]
 +
* [[Manual:DIL Manual/length()|length()]]

Latest revision as of 23:18, 14 November 2026

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.

See also