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
CMS Programs: Choosing the CMS Program that WorksCMS (standing for Content Management System) programs are the newest craze among aspiring, young web professionals. They allow anyone who aspires to be any one of these, access...- Free Programs for WindowsThese free and safe programs run on your computer for your protection, amusement and help. They are a collection of programs you can download and install that I have found to be helpful and useful in my computing.
Why Affiliate Programs Are Right for YouNo matter how much education you have, or at what level your internet business is, joining affiliate programs is right for you. They add another income streams to your business.- Distance Gifted Education ProgramsJohns Hopkins University and Stanford University offer unique online gifted programs.
Programs and Activities for Seniors in San FranciscoRanging from bingo to tai chi to swimming to attending lectures to hiking to taking a day trip to eating lunch to becoming a volunteer to going on an overseas excursion, San Fra...
- Programming: Creating Executables
- 12 Step Programs for Addition Recovery
- Do Abstinence-only Programs Work?
- Free for All Welfare Grant Programs Hurt Our Nation's Most Vulnerable
- Alcohol Abstinence Programs; An Overview of the Latest Testing Technology
- Affiliate Programs, an Overview
- Recommendations for Animated Programs on the Cartoon Network, Nickelodeon and the...




