Introduction

Language Structure

IB Statements

File System

Comet 32 Runtime

Index

RND function

Syntax: RND(numeric-argument)
Discussion: The RND function returns a pseudo-random value less than 1 but greater than or equal to 0. The returned value has a length/precision of 8.8 (i.e., all 8 digits are to the right of the decimal point).

The value of the numeric-argument determines how the RND function produces values:

  • RND(0) generates a pseudo-random number. If you use the RND(0) function more than once in a program, the same sequence of numbers is returned each time. This feature can be helpful for testing a program with the same sequence of random values.

    Each time that Comet is started, a unique sequence of pseudo-random numbers is returned when RND(0) is used.

  • Using a non-zero value for the numeric-argument (e.g., RND(1)) randomizes the value returned by the RND function. The randomness of the returned value is based on the system time.

    Note: If you want to generate a series of psuedo-random numbers, use RND(1) once to randomize the generator, then use RND(0) to produce each successive value in the series.

To produce random integers in a given range, use this formula:

INT((upperbound - lowerbound + 1) * RND(numeric-argument) + lowerbound)
Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.
History: This function was added in Build 290.
Example 1:
LENGTH 8.8 & LOCAL X
.
.
.
X = RND(0)
In this example, X is assigned a pseudo-random number. Since the numeric-argument is 0, the same sequence will be returned each time this program is run (while the Comet session remains active). When Comet is terminated and re-started, a different sequence will be returned when this program is run.
Example 2:
LENGTH 8.8 & LOCAL X
.
.
.
X = RND(1)     ! Randomize the pseudo-random number generator
.
.
.
LABEL:
  X = RND(0)   ! Generate a pseudo-random number
  .
  .
  .
  GOTO LABEL
In this example, the RND(1) function randomizes the pseudo-random number generator. Further on in the program, the RND(0) function returns a series of pseudo-random numbers.
Example 3:
LENGTH 2.0
LOCAL A,UPPER,LOWER
.
.
.
UPPER = 75
LOWER = 40
A = INT((UPPER-LOWER+1)*RND(0) + LOWER)

The above example shows how to produce a random integer between 40 and 75.