Exploring random numbers with your computer
This set of experiments shows you how to create and use random numbers. You will need QuickBASIC in order to run these experiments. If you have downloaded the seminar materials, then you can also use the Rand function in the Programmer's Toolkit.
Run QBASIC
Begin by running the QuickBASIC program. If you aren't sure how to load and run BASIC programs, check out the Really really basic BASIC course first. When QuickBASIC is running, you will see this screen:
Random numbers using the BASIC RND
There is a command in BASIC that you can use for creating random numbers. Type the following instruction on the screen and press Enter. To run the program and see the result, press F5.
PRINT RND
This prints a random number on your screen. The random number that BASIC prints is probably .7055475. This is because BASIC prints the same "random" numbers each time you run a program. There are ways you can fix this, but the easiest thing to do is skip the BASIC RND entirely and use my toolkit function Rand instead. But before you do this, try the next program and print a whole bunch of numbers using RND.
To see a series of random numbers, add these next instructions. Caution! When this program runs, the computer will be in a continuous loop. To stop the program you will need to press the Control key and the Break key (Ctrl-Break). Now run the program and stop it after you have seen enough random numbers.
DO
PRINT RND
LOOP
When you stop this program by using Crtl-Break, the computer will automatically return to the blue screen and your program. To see what the program was printing when you stopped it, press F4. You can toggle back and forth between your program and the output with F4 whenever the program is stopped.
Random numbers using the Toolkit function Rand
I have created a special function Rand that makes it much easier to create a random number in any range. Here's how it works.
x = Rand(0, 5) 'x is a random number from 0 to 5
y = Rand(1, 10) 'y is a random number from 1 to 10
z = Rand(12, 50) 'z is a random number from 12 to 50
The apostrophe (') in front of a comment tells the computer to ignore the text that follows. You will notice that I use comments a lot in these programs so that you can better understand what the instructions are supposed to do. I also add comments to my own software because it helps me understand what the instructions are supposed to do. It's easy to forget what you meant when you wrote an instruction, and a comment or two can often help keep things sorted out.
To use this function in your programs, you will have to first load it into a program. There are two ways you can do this. If you have downloaded the seminar materials, you already have the toolkit and this function. If not, you can type it in now.
First method: loading the Rand function with the START1.BAS program.
Run BASIC and open the START program. This places the Rand function and all the other tools in the toolkit in your program automatically. As you can see, this is a lot easier than typing this function into your computer, which is why I wrote and use the toolkit.
Second method: typing the Rand function into your program as a SUB.
You can add the Rand function yourself with these steps:
In QuickBASIC select Edit and New Function...
Type the name of the function Rand and press Enter.
Add these instructions to the function exactly:
FUNCTION Rand (Bottom, Top)
RANDOMIZE TIMER 'this sets the random number generator
RANDOMIZE RND * 10 'this confuses it to get more randomness
IF Bottom > Top THEN 'oops - reverse order
Temp = Bottom
Bottom = Top
Top = Temp
END IF
Rand = INT((Top - Bottom + 1) * RND + Bottom)
END FUNCTION
Return to the main program and declare the function with these steps:
Press F2 to see the program and the subs.
Select Untitled, the name of your program, and press Enter.
Now add this instruction to your program:
DECLARE FUNCTION Rand (Top, Bottom)
Experiments with the Rand function (finally!)
Now that you have my function Rand in your program, creating random numbers is easy. Enter this program and run it by pressing F5. To stop the program, press Control and Break (Ctrl-Break). To see the output of your program, press F4. You can use this key to toggle between the program and its output whenever the program is stopped.
CLS
DO
PRINT Rand(0, 9);
LOOP
This prints a screen full of random numbers from one to nine. You can easily print numbers in any range by changing the values in Rand and running your program again. Here are some suggestions for changing the PRINT statement in your program:
PRINT Rand(0, 100); PRINT Rand(-1000, 1000); PRINT Rand(0, 1);
Random ASCII characters
The computer uses numbers for everything, even letters of the alphabet. To see what numbers are used with what letters, use the Help system in BASIC by making these selections from the BASIC screen:
Help
Contents
ASCII Character Codes
As you can see from this chart, the letter "A" is the ASCII number 65, while "a" is 97. If you look at the next screen (scroll down) you will see ASCII values for other characters you can print. This is where I get the characters to print boxes on the screen and the Robot figure in the Hangperson program. Try this following program and print random letters on the screen. The CHR$ instruction means "print the ASCII character with this number."
CLS
DO
PRINT CHR$(Rand(65, 90));
LOOP
And here are some interesting variations, or try your own:
PRINT CHR$(Rand(97, 122));
PRINT CHR$(Rand(65, 90));
PRINT CHR$(Rand(65, 90));
PRINT CHR$(Rand(176,178));
Random locations on the screen
You can print numbers or characters anywhere on the screen with the LOCATE command. This example prints "x" on the top row, left column and prints "y" in the bottom row, right edge (The semicolon after "y" keeps the screen from scrolling up when you print on the last line):
LOCATE 1, 1
PRINT "x"
LOCATE 25, 79
PRINT "y";
You can use the Rand function to print "x" in random rows (from 1 to 24) and in random columns (from 1 to 79) with this program:
CLS
DO
LOCATE Rand(1, 24), Rand(1, 79)
PRINT "x";
LOOP
Computer art?
Just one more instruction and your program becomes an example of computer art. Not a very good example, but a start. I also changed from printing "x" to printing (you guessed it) random characters:
CLS
DO
LOCATE Rand(1, 24), Rand(1, 79)
COLOR Rand(0,15)
PRINT CHR$(Rand(176, 178));
LOOP
World's Most Annoying Screen Saver -- the program
With the final addition of random sounds, your program becomes a genuine screen saver. You might want to personalize your version by using ASCII characters that appeal to you. If you would like to go into the software business yourself, then you have my Official Permission to sell your version of the World's Most Annoying Screen Saver program to anybody you like -- or don't like.
CLS
DO
SOUND(32, 200), 2
LOCATE Rand(1, 24), Rand(1, 79)
COLOR Rand(0,15)
PRINT CHR$(Rand(176, 178));
LOOP
A final word about my Rand function
Not only is this function much easier to use than the RND function that comes with BASIC, it also creates numbers that are much more random. When is random not random? As it turns out, true randomness or complete avoidance of any set pattern is very difficult to achieve. My Rand function uses a trick to produce numbers that are highly randomized and much better choices if you are using this function to create art or music programs. Here's how it works:
RANDOMIZE TIMER sets the random number generator seed to the number in TIMER, which is the number of seconds since midnight.
RANDOMIZE RND * 10 seeds the random number generator again. This tricks the computer into randomly creating a different random set of random numbers, really!
Bottom and Top are the variables I use to create a number within a range you specify. Notice that you can reverse the order and Rand still figures out what to do. This is a much better solution than crashing your program if you happen to get these numbers reversed by accident. Here is the complete function as it appears in TOOLKIT1.BAS and START1.BAS:
FUNCTION Rand (Bottom, Top)
'create a random number between Bottom and Top, inclusive
RANDOMIZE TIMER 'this sets the random number generator
RANDOMIZE RND * 10 'this confuses it to get more randomness
IF Bottom > Top THEN 'oops - reverse order
Temp = Bottom
Bottom = Top
Top = Temp
END IF
Rand = INT((Top - Bottom + 1) * RND + Bottom)
END FUNCTION