RANDOMIZE Statement

Purpose:

To reseed the random number generator.

Syntax:

RANDOMIZE [expression]
RANDOMIZE TIMER

Comments:

If expression is omitted, GW-BASIC suspends program execution and asks for a value by displaying the following line:

Random number seed (-32768 to 32767)?

If the random number generator is not reseeded, the RND function returns the same sequence of random numbers each time the program runs.

To change the sequence of random numbers every time the program runs:
 1. place a RANDOMIZE statement at the beginning of the program
 2. change the argument with each run (see RND function).

RANDOMIZE with no arguments will prompt you for a new seed. RANDOMIZE [expression] will not force floating-point values to integer. expression may be any numeric formula.

To get a new random seed without prompting, use the new numeric TIMER function as follows:

RANDOMIZE TIMER 

Example 1:

The internal clock can be set at intervals.

10 RANDOMIZE TIMER
20 FOR I=1 to 5
30 PRINT RND;
40 NEXT I
RUN
.88598 .484668 .586328 .119426 .709225
RUN
.803506 .162462 .929364 .292443 .322921

Example 2:

The random number seed uses the internal clock.

5 NUMBER=VAL(MID$(TIME$, 7, 2)) 'get seconds for seed
10 RANDOMIZE NUMBER 'install number
20 PRINT NUMBER 'print seconds
30 PRINT RND 'print random number generated
RUN
36
.2466638
RUN
37
.6530511
RUN
38
5.943847E+02
RUN
40
.8722131