INTRODUCTION TO BASIC by David Ross ( NY Computer Interest Group / KCN News ) Using Commodore ' s Somewhat Random Numbers : Part 1 In games, simulations, and scientific programs, a source of random numbers may be needed . A number is random if its selection from a set of numbers is as probable as the selection of any other number from that set . The RaNDom function in Commodore BASIC -- RND () -- provides a source of somewhat random numbers . The qualification " somewhat " is necessary since computers are determinant systems in which every process depends on what occurred before . The random function starts with a given number, or seed, and alters it according to a fixed rule . Since it follows such a rule, the result is not random, but because the rule is unknown to the user of the program, it appears to be random . And, in fact, these pseudo random numbers are unpredictable enough to be useful in games and most simulations, but not in rigorous scientific applications . The random function produces a decimal fraction between 0 and 1, excluding both . Enter and run the following program : 10 PRINT RND ( 1 ) 20 GOTO 10 Line 10 sends a random number between 0 and 1, exclusive, to the screen . The number 1 is used within the parentheses in this and the following programs without comment . Its effect will be discussed fully in a later article . The GOTO keyword in line 20 returns the execution of the program to line 10 . To slow the output, hold down the control key . To stop the program, press the RUN / STOP key . Since programs are likely to require random integers, not decimal fractions, it is necessary to manipulate the result of the random function to produce the desired values . The program below, RANDOM . DEMO . 1, constructs step by step the expression which produces random numbers between 1 and 10, including both 1 and 10 . Next, each line of the program is described in detail . 100 REM ******* RANDOM . DEMO . 1 ******* 110 ::::::::::::::::::::::::::::::::: 120 LET RN = RND ( 1 ) 130 PRINT " RN =",, RN 140 PRINT " RN * 10 =",, RN * 10 150 PRINT " INT ( RN * 10 ) =", INT ( RN * 10 ) 160 PRINT " INT ( RN * 10 )+ 1 =", INT ( RN * 10 )+ 1 170 END LINE 100 : The REM keyword allows for documentation -- REMarks -- to be included in the program . BASIC ignores everything after this keyword while the program is running . LINE 110 : Colons are mainly used to separate statements in a BASIC line . However, here a line of them is used to mark off the title from the main program . LINE 120 : LET is the keyword used to introduce the assignment statement where in the value of the expression RND ( 1 ) becomes the value of the variable RN . LET can be omitted and the statement rewritten as 120 RN = RND ( 1 ). LINE 130 : The keyword PRINT sends the expressions that follow it to the screen . The expression within quotation marks is a string, or literal . It is sent to the screen just as it is . The expression RN is a variable . Its value is copied from memory and then sent to the screen . The commas between these two expressions format the output . After the first expression is displayed, the first comma tabulates to the eleventh column, and the second comma to the twenty - first column . Then the value of the second expression is displayed on the screen starting in that twenty - first column . This aligns the values of the variable in this line with corresponding values in lines 150 and 160 . LINE 140 : This line is similar to line 130 except that the value of RN is multiplied by 10 . Recall that the object of this program is to produce random numbers between 1 and 10, inclusive . Since the range of these numbers is 10, the decimal fraction represented by RN is multiplied by 10 . The product is a number between 0 and 10, including 0 but excluding 10 . The PRINT keyword causes this product to be sent to the screen . LINE 150 : It is now necessary to remove the trailing decimal fractions from the selected number . INT () is a numeric function which returns an INTeger less than or equal to the expression within the parentheses . The expression now produces an integer ranging from 0 to 9, inclusive, which is displayed on the screen . LINE 160 : The addition of 1 to the random integer shifts its range to 1 through 10, inclusive . The goal of the program has been reached and been displayed on the screen . LINE 170 : The END keyword marks the end of the program and stops execution . It is optional in this program since BASIC will also stop when the program runs out of lines . The above program, can now be rewritten in one statement : 200 PRINT INT ( RND ( 1 )* 10 )+ 1 As an exercise, rewrite the above programs to produce random numbers from 1 to 100, 10 to 20, and - 1 to + 1, all inclusive . Run the programs as many times as needed to check that the full range of values is produced . A GOTO statement may be useful . In the next article, there will be a demonstration of a programing solution to the above exercise . In the following articles, there will be programs and discussions showing how expressions within the parentheses of the random function affect the output and the use of the random function in a simple game .