Random. Variable

So far, we´ve been working with programs that behave very precisely. Sometimes it may happen that we will need a random selection. Lazarus, respectively Pascal, has this one purposeful function Random, which has one parameter - an integer, through which we tell our computer how large the random selection will be. Each Random(N) call selects a number from 0 to N-1, that is one of N different values. Let´s assume that we´re working with a dice with numbers ranging from 0 to 5 and whenever we want a random value Random(6), we roll the dice and get the number which fell on that dice.

This is shown in the example (Figure 36). Insert a Graphic area into the Form (Image1 from the Additional tab) and the Button1 (from the Standard tab). After (every) pressing the button a line with random X and Y coordinates is drawn into the graphic area (Figure 37). 

Figure 36: Random function
Figure 36: Random function

while:

  • Random(300) represents the X coordinate and selects a number in the range between 0 and 299.
  • Random(200) represents the Y coordinate and selects a number in the range between 0 and 199.
Figure 37: Lines with random X and Y coordinates
Figure 37: Lines with random X and Y coordinates

Let´s try to draw a square in a similar way (let´s not forget it has 4 parameters) (Figure 38).

Figure 38: Square drawn with Random function
Figure 38: Square drawn with Random function

We see that after running the program and several presses of the button, the program does not draw squares, but rectangles of different sizes (Figure 39). 

Figure 39: Rectangles drawn instead of squares
Figure 39: Rectangles drawn instead of squares

Every time we use Random, we get a different result. Pairs of two are selected of different random numbers, so the program draws rectangles instead of squares. However, we need the program to remember the first random number and use it in both places in the Rectangle command. Variables are used for this purpose.

Variable is a part of the operating memory. Each variable has its name - the identifier, which:

  • may not begin with a numeral.
  • cannot match keywords (e. g. begin, end, procedure, etc.).
  • should express the value as clearly as possible (e. g. Height, Sum, etc.).
  • the same entry should be used in all places (e. g. not the height, HEIght, etc.) since Lazarus does not distinguish upper and lower case letter.

In order to use the variables in the program, we have to ask the computer to reserve memory locations (assigned memory) for these variables and to name them. This occurs at the declaration of the variable. In program then we just use variable names and the system knows with which part of the operating memory it should work. The declaration therefore means physical assigning of the memory for the variable and its temporary naming by the identifier. 

Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky