Programming Using the GWBASIC RND Command

Z. Perry
RND is a useful command in the programming language GWBASIC, especially for creating games and simulations. It can also be used with other varieties of BASIC, like QBASIC and Visual BASIC, but the syntax varies slightly in some situations. It is relatively easy to learn and apply to programming. Here is a simple example of using RND in GWBASIC:

10 A = INT(RND * 10) + 1
20 PRINT A

As always in GWBASIC, you can type the RUN command and press enter to activate the program you have entered. This example sets the variable "A" to a random whole number (INT) between one and ten, then displays (PRINT) it on the screen. Ten and twenty are line numbers. However, this will produce the same number every time the program runs. To fix this, add the following command and try running the program again:

5 RANDOMIZE TIMER

This instructs the GWBASIC program to use the current time (down to the hundredth of a second) in the process of determining a random number, which prevents it from being the same each run. Using the line number "5" instructs GWBASIC to automatically place this command at the beginning of the program, before the previously-entered lines numbered 10 and 20.

The RND command doesn't have to be used with variables. The following commands all use RND directly, to create random sounds, numbers, and colors:

30 PRINT "Random Number, 0 to 50: "; INT(RND*50)
40 SOUND INT(RND * 200)+100, 5
50 COLOR INT(RND * 4), INT(RND * 4) + 4

Type COLOR 7, 0 and press ENTER to change back to white text over black, after running this program example. The SOUND command will work on most computers, but not all. Use the "LIST" command to view the entire program, then type "NEW" and press enter to clear it.

Here is an example of using the RND command to create a working, useful GWBASIC program. It lets you guess a number from one to twenty-five in up to four guesses, and provides hints as to whether the correct number is higher or lower. To end the program early, press the "CTRL" and "C" keys at the same time...

10 RANDOMIZE TIMER
15 N = INT(RND * 25) + 1
20 INPUT "GUESS...",G
25 T=T+1
30 IF G < N THEN PRINT "TOO LOW"
35 IF G = N THEN PRINT "CORRECT!":END
40 IF G > N THEN PRINT "TOO HIGH"
45 IF T = 4 THEN PRINT "LAST GUESS... IT WAS";N:END
50 GOTO 20

A more sophisticated version of the program has the user guess a letter and tells him or her if the letter is earlier or later in the alphabet:

5 A$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10 RANDOMIZE TIMER
15 N = INT(RND * 26) + 1
18 N$ = MID$(A$,N,1)
20 INPUT "GUESS...",G$
25 T=T+1
30 FOR X = 1 TO 26
35 IF MID$(A$,X,1) = G$ THEN 100
40 NEXT X
45 GOTO 20
100 IF X = N THEN PRINT "CORRECT!":END
105 IF X > N THEN PRINT "EARLIER IN ALPHABET"
110 IF X < N THEN PRINT "LATER IN ALPHABET"
111 IF T = 4 THEN PRINT "LAST GUESS... IT WAS ";N$:END
115 GOTO 20

Be sure to use uppercase letters when running this program. The MID$ variable selects specific letters from the alphabet (stored in the A$ variable), while the FOR/NEXT command set instructs GWBASIC to repeat line 35 twenty-six times, checking the alphabet position of a letter which has been guessed. You can enter the "SYSTEM" command and press enter to exit GWBASIC.

Published by Z. Perry

Freelance writer, website operator, and programmer  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.