Random numbers

Random numbers can be created with CHOOSE.

CHOOSE
CHOOSE ( n -- rand, select random number between 0 and N-1 ) 10 choose    ok Stack<10> 5 10 choose   ok Stack<10> 5 7 10 choose   ok Stack<10> 5 7 0

To choose a random number between zero and ten (but exclusive of ten) simply enter

10 CHOOSE

and CHOOSE will leave the random number on the stack.

A Random Number Generator
This simple random number generator can be useful for games, although for more sophisticated applications such as simulations, better versions are available.

( Random number generation -- High level ) VARIABLE rnd  HERE rnd !
 * RANDOM rnd @ 31421 *  6927 +  DUP rnd ! ;
 * CHOOSE ( u1 -- u2 )  RANDOM UM*  NIP ;

where CHOOSE returns a random integer within the range 0 = or < u2 < u1.