Welcome to the fifth video-article of the Basic Programming course. Today we see the RND function, which generates pseudo-random numbers, the INT function, which returns the integer part of a number and also the system variable TIMER.
We implement functions RND and INT within a program, which can be useful for your children and grandchildren, to learn the times tables! The computer also measures the time taken to calculate the various times tables, using the system variable TIMER.
The program for learning the times tables is available at the bottom of this page.
Learn to program in Basic, on which computers?
Even the example program we are analyzing today can be run on various 8-bit Commodore, such as the Commodore 64, C16, Vic20, Commodore 128 and PET. You can run it on emulators, such as the VICE and the CCS64. It is also compatible with the old DOS GWBASIC for IBM PCs, which we see running in the DOSBox emulator. For those unfamiliar with it, DOSBox allows you to run old DOS programs, even on Windows 10.
Last but not least, the program to learn the times tables is also compatible with the QuickBasic QB64, introduced in episode 3 of the course, which allows you to compile programs in Basic and to create executable files for Windows, Linux and Mac.
Basic Programming Course #5: listing of the program to learn the times tables
Here is the listing of the program to learn the times tables. At the bottom of the listing, the last line of the program is slightly different between Commodore and DOS PCs. As you continue reading this Basic Programming course, you will find out why.
10 REM IL MIO QUINTO PROGRAMMA IN BASIC 20 REM RIPASSO TABELLINE 30 REM (C) 2021, WWW.VALOROSO.IT 40 REM TIPO ESERCIZIO 50 PRINT "FINO QUALE TABELLINA" 60 INPUT "RIPASSI (1-10)";NT 70 IF NT<1 OR NT>10 THEN GOTO 50 80 PRINT: INPUT "QUANTI ESERCIZI";NE 90 IF NE<1 THEN GOTO 80 100 REM INIZIALIZZAZIONE 110 X = RND(-TIMER) 120 PRINT 130 TM = TIMER 200 REM ESERCIZI 210 FOR ES = 1 TO NE 220 M1 = INT(RND(1) * (NT + 1)) 230 M2 = INT(RND(1) * 11) 240 PRINT: PRINT "QUANTO FA" 250 PRINT STR$(M1) + " X" + STR$(M2); 260 INPUT MR 270 IF MR = M1 * M2 THEN PRINT "GIUSTO!": NG = NG + 1 280 IF MR <> M1 * M2 THEN PRINT "SBAGLIATO!": NS = NS + 1 290 NEXT ES 300 REM PUNTEGGIO 310 PRINT 320 PRINT "NUMERO ESERCIZI:" + STR$(NE) 330 PRINT "- GIUSTI: " + STR$(NG) 340 PRINT "- SBAGLIATI: " + STR$(NS) 350 PRINT "TEMPO:" + STR$(INT((TIMER - TM) / 60)) + " S"
Analysis of the example program to learn the times tables
The first three program lines, 10, 20 and 30, contain the instructions REM, to write the comments, which we have already seen in first episode of the course.
Lines 40 to 90 inclusive also contain instructions already seen.
In particular, from line 50, the computer asks the user to which table he wants to review. I wanted to limit the monitor width of the various requests, due to the monitor with fewer characters, which is that of the Commodore Vic20.
The example program also allows you to review up to a specific table. In fact, kids don't study all the times tables together and may want to go over the times tables as far as they got with the study.
The computer stores the number of the times table, up to which you want to review, in the numeric variable NT.
Line 70, through the instruction IF… THEN, already seen in second episode of the course, check that the table is between that of 1 and that of 10. If the number were outside these limits, the question would be reformulated.
On line 80, the computer asks how many exercises you want to do. In fact, you can decide the number NE of calculations that the user must perform for review.
There is no maximum limit to the number of accounts that the unfortunate user must perform! This number, however, cannot be less than 1, otherwise the computer asks the question again.
The random number generator RND
Line 110 initialises the random number generator RND. In fact, if RND it is not initialized with a random number, it would always return the same sequence of numbers.
Let's do a test with the Commodore PET. We turn it on and ask to write a random number. The function RND always returns a value less than 1, but greater than or equal to zero. We transcribe it. Then we reset the computer and always ask for a random number. The number… is always the same!
If, however, we initialize the random number generator with a variable that changes at all times, for example TIMER (we will deepen the discussion in the following paragraphs), the random number generated is always different.
In the times tables program, the random number is used to identify the two factors, always different, of the various multiplications that the user must calculate.
The system variable TIMER
Line 130 stores the TIMER in the numeric variable TM, which is used to count the time it took the user to carry out the various accounts.
Relative to the system variable TIMER, in the Commodore Basic it doesn't exist, but it works!
In fact, the system variable exists TIME, which returns the number of sixtieths of seconds that have elapsed since the computer was turned on.
Since variables are recognized by their first two letters, TIME is recognized by TI, as is TIMER. This is why it works. Any other name starting with TI would work too.
Anyway, in the program, I wrote just TIMER in the place of TIME, for compatibility with other DOS Basics, where the system variable TIMER is implemented.
Always delving into the TIMER, in the Commodore the value is updated by 60 units per second. Instead, in the PC Basic, the value is decimal and the integer part represents the seconds elapsed since midnight. We must take this difference into account in the last line of the program, where we calculate the time that the user takes to carry out the calculations of the tables.
The generation of the two factors of the multiplication tables
From lines 200 to 290 inclusive we get to the heart of the program! The cycle FOR… NEXT, which we have already studied in third episode of the course, generates for NE times (the number of exercises), the multiplications to be proposed to the user.
Line 220 generates the first factor M1, random integer, which must be between 0 and the last table that the user has studied (NT).
The second factor M2 multiplication, generated on line 230, is always between 0 and 10.
Programming in Basic: the function INT to get the integer part of a number
For these accounts, the function is useful for us INT(), which, for positive numbers, returns the integer part, leaving out what is after the comma.
Let's deepen the calculation of the two factors M1 and M2 with the Commodore Vic20. The random number, generated by the function RND, is between 0 inclusive and 1 excluded. Suppose we want to calculate the times table of 10. Even if the computer generated a very large random number (for example 0.99), multiplying by 10 could never reach the whole number 10. For this, we need to multiply the function RND for the maximum number you want to generate +1. In this case, to get up to 10, we need to multiply by 11.
The multiplication factors must be whole numbers. That's why the function comes in handy INT which, for positive numbers, returns only the whole part of the number, leaving out the decimals.
Verification of the value entered by the user
Let's go back to the program: we have the two factors M1 and M2, integers and random, to compose the multiplication.
From lines 240 to 260, the computer asks the user for the result of multiplication M1 * M2. This result is stored in the variable MR.
By two instructions IF… THEN, the computer evaluates whether MR, the result entered by the user is equal to the correct multiplication value. If the result is correct, the value of the variable is incremented of, which represents the number of correct exercises. In addition, the computer gratifies the user with the words "RIGHT!".
If, on the other hand, the result is incorrect, the value of NS is increased, which is the number of incorrect answers entered by the user. In addition, the computer writes: "WRONG!".
Reached education NEXT ES, present on line 290, the cycle FOR ES repeats: the user is asked to calculate the remaining multiplications.
Programming in Basic: the final score!
When the challenge is finished, the computer returns the results, from line 300 onwards.
The following are indicated on the monitor:
- the total number of exercises (NE);
- how many multiplications the user has performed correctly (NG);
- the number of incorrect multiplications (NS);
- in line 350, the computer indicates the time taken by the user to make the calculations, expressed in seconds. The computer writes the time elapsed between the last exercise (current TIMER value) and the first (previously stored in the TM variable).
Here, there is a small difference between the Commodore Basic and the one for PCs. In fact, in the Commodore the value of the TIMER it must be divided by 60. For DOS PCs, the division is not necessary, as the value is already expressed in seconds.
Here, then, is the last line of the program, the 350, for Commodore computers:
350 PRINT "TEMPO:" + STR$(INT((TIMER - TM) / 60)) + " S"
And this is for the Gw-Basic and the QuickBasic QB64 for MSDOS PC:
350 PRINT "TEMPO:" + STR$(INT(TIMER - TM)) + " S"
Listing of the times tables program
And here is the listing of the Basic program of the fifth episode of the video course on how to program in Basic with the Commodore and with the DOS PC.
The program is available in two formats:
- CORSO-BASIC-5. TXT, in text format, to analyze it on the PC or to transcribe it on the various Commodore, on the GwBasic in the DOSBox and on the QuickBasic QB64;
- CORSO-BASIC-5. ZIP, ZIP compressed folder to unpack. It includes the program in PRG format, which can be loaded and booted from the Commodore 64 and a virtual D64 disk containing the programs that can be loaded from the C16, Vic20, C64 and C128, as well as emulators (CCS64, VICE or others). It also includes the program in BAS format for QuickBasic QB64 and GwBasic for DOSBox and MSDOS.
The topics of the course are still many!
Credits: in the video course, SID music is Credits: DYSPIDCE, by Sami Seppä (Rock).
To be notified when other tutorials, experiments and reviews related to retro computers and subsequent installments of the BASIC course will come out, I suggest you subscribe to the YouTube channel and activate the notification bell!