DarwinBotsManual
Open the app EspañolES

rnd

Replaces the top number of the stack with a random one between 0 and that number, both included.

Facts

Word
rnd
Family
Basic
Integer stack
pops 1, pushes 1
What it does
Pops a number n and pushes a random one between 0 and n, both included. With a negative n it is not symmetric: −5 rnd gives −4 to −1, without the −5 and, in practice, without the 0.
Cost
Basic command
Runs
inside a gene: in the condition (cond) and in the body (start or else)

n rnd pops n and pushes a random integer between 0 and n, both included and all equally likely. 10 rnd can give 0, 1… or 10. Together with rndstore, it's the source of randomness in the DNA, and the basis of any behavior that isn't entirely predictable: random headings, variable waits, decisions that don't repeat.

cond
start
  ' turns between 10 to the right and 10 to the left
  20 rnd 10 sub .aimsx store
  10 .up store
stop

This bot moves forward zigzagging: 20 rnd gives a number from 0 to 20 and, after subtracting 10, the turn of .aimsx ends up between −10 and 10. To pick any direction at all, 1256 rnd .setaim store (a full turn is 1256 units, see .setaim).

The odd cases:

To write a random value directly into a cell there is rndstore.