RND

BASin

RND RaNDom number

Function

RND is used to generate a random number.

How to use RND

RND is used alone in a statement or command, for example

60 LET x=RND

RND then returns a random number less than 1 and greater than or equal to 0.

When BASIC is reset or started, or NEW is used, numbers are subsequently returned by RND in the same sequence. The sequence is generated by taking the powers of 75 (75, 75*75, 75*75*75 and so on) dividing each power by 65537 and using the remainder only, then subtracting 1 from the remainder and dividing this result by 65536.

If a more random sequence or another fixed sequence is required, then use RANDOMIZE before RND.

Random whole numbers

Many of the BASIC's statements and functions, such as INK and CHR$, round numbers to the nearest integer and RND may be used with them directly, for example

INK RND*7

produces an ink colour at random. Others require integers and any whole number from 1 to x is given by

INT (RND*x)+1

To generate a random integer from 0 to x, use

INT (RND*x+0.5)

Format

  • RND

See also

Chapter 11.