`RANDOMIZE TIMER`

INPUT "Low Number";ln

INPUT "High Number";hn

rn=INT(RND*hn)+ln

PRINT rn

Ok this is going to be one jam packed lesson!

First let's start off with the "Golden Rule" of programming:

**You will NEVER produce perfect code on the first try.**

This leads to the "Almost Golden Rule" of programming:

**You will (almost) ALWAYS assume your code is perfect on the first try.**

When our code fails to work as designed it is called a "bug". In this case we intended to create a random number from ln to hn but failed to lower the hn calculation to accomodate the addition of ln. Here is the** formula** to insure that we get the actual range of numbers we want:

INT(RND * (max - min + 1)) + min

In the above, max=high number and min=low number. So, what we need to do is replace that section of code with the proper formula exactly like this:

rn=INT(RND * (hn - ln + 1))+ ln

Now this may seem complicated but don't worry, you have the formula that you can refer to at any time. In fact its right in the F1 section for RND whenever you need it. The fact that you know this formula exists is all that matters and so you can rest easy about it.

To make sure the use of this formula is clear, let's say we wanted a random number from 18 to 36. Using the RND formula we would do this:

*INT(RND * (max - min + 1)) + min*

INT(RND*(36-18+1))+18

Then of course we would resolve the simple math in the parenthesis and have:

INT(RND*19)+18

Now for the flaw that December identified:

INPUT "Low Number";ln

INPUT "High Number";hn

Here we are relying upon the user to cooperate with us and enter a low number that is actually lower than the high number. We can actually check the data that the user enters to be sure that it meets the criteria we specify. This is a form of **error trapping **and the first thing we need to do is to label our lines because we are going to be altering the flow of the code.

When line labeling was in use in the programming community, numbers were seen as the best method; if a line number is lower than a particular section, you know its located above that section and a higher number is located below. I still do it that way. The conventional method was to space them apart by 10; so 10,20,30 etc. The reason for this is that if you need to add lines in a section you have space and can use 5 like 15,25,35 or 2 like 12,14,16 or even 1 if necessary 10,11,12. QB64 does not require these labels, I just use them because BASIC used to require them and they work well for me. Here's how it looks:

10 RANDOMIZE TIMER

20 INPUT "Low Number";ln

30 INPUT "High Number";hn

40 rn=INT(RND * (hn - ln + 1))+ ln

50 PRINT rn

The command we will learn to check the numbers entered by the user is called IF...THEN. This is known as **condition testing**. The way it works is:

IF (a condition exists) THEN (perform action)

The condition we want to test is that the low number is lower than the high number. So we need to first have the user enter both of those numbers and perform our condition test afterwards. This means the test needs to be between line 30 and line 40, like so:

35 IF ln >= hn THEN CLS:GOTO 20

This reads "if ln is greater than or equal to hn". If the user entered the numbers properly, ln will be lower than hn. The commands after THEN are ONLY executed when the condition after IF is true, as follows:

**ln=1, hn=10** -- the program proceeds to line 40 because 1 < (is less than) 10.

**ln=10, hn=1** -- the program clears the screen and executes the code located at line 20 because 10 >= 1

**ln=10, hn=10** -- the program clears the screen and executes the code located at line 20 because 10 >= 10

As you might have guessed, CLS stands for "clear screen" and will empty the screen of all data.

GOTO tells QB64 to execute the code at the specified line rather than simply continuing to the next line in the code. We have specified line 20 so the user can re-enter the numbers properly.

Note that NOTHING after THEN will be executed when the condition specified by IF is not true, not even things after a colon! IF...THEN will ALWAYS proceed to the next line in the code when the condition specified by IF is false.

Now for some practical examples of IF...THEN from our Rstation code:

**30 IF rs < s THEN so(sl) = so(sl) + 1: sl = 0: GOTO 70**

If rs is less than s, the entire line is executed. If rs is greater than or equal to s, the rest of the line is skipped.

**50 sl = sl + 1: tol = tol + 1: IF sl > ml THEN ml = sl**

Here the variables sl & tol are increased by 1 and ml is made to equal sl ONLY IF sl is greater than ml. The IF... has no effect on commands that come before it on the line, only on commands after it.

**90 IF sp = 16000000 THEN 120**

This is a fancy way of saying THEN GOTO 120. When using IF...THEN where you only want to move to another section of code, you can simply say THEN and specify the line of code. It would be useless to put a colon on line 90 as nothing after the colon would be ever executed because if sp <> (does not equal) 16M, the line is skipped but if sp = 16M the program jumps to line 120 to execute code there which would prevent the colon from being reached.

**101 a$ = INKEY$: IF a$ = "q" OR a$ = "Q" THEN END**

This has some additional features which we will discuss in the next lesson!