# Tutorial 2 : Random effects

### I love you rand(0,1)

Let’s get down to it. Turbo Rascal SE initializes a random method if you have used the “Rand” function anywhere in your code.

The Rand function requires three parameters: Lower and upper bounds, plus the variable to write to. Note a couple of things:

• The Random value uses output from the SID, so it will interefere with any music / sound effects being played
• The function is kinda of slow, especially when having a narrow range of upper/lower bounds.

It is therefore recommended that you initialize a random table in the beginning of the code and use this instead of calling the (slow) random routine directly.

In this tutorial, be are basically doing the following:

• Initializing the random table
• Clearing the screen
• Setting borders
• Filling the screen with random values
• Filling lines on the screen with random color values
• While the MoveTo method positions a screen pointer is useful, it is also costly. It is recommended to keep the use to a minimum, and instead use the IncScreenX method to position the cursor between calls.
• Dividing by powers of two are always recommended, as these methods are optimised to use bit shifting
``````
program randomstuff;
var
random_char, cnt, random_color: byte;
random_values : array of byte;
x, y : byte;
index : byte;

// Initialize random table, much faster then using the sid's random value
// generator
procedure InitializeRandom();
begin
for x:=0 to 255 do begin
rand(0, 255, y);
random_values[x]:=y;
end;

end;

begin
InitializeRandom();
// Fill screen with blank
ClearScreen(\$20, SCREEN_CHAR_LOC );
// Fill color screen with background F and foreground 8
ClearScreen(\$F8, SCREEN_COL_LOC );

// Set screen foreground and background to black. The second parameter is an offset.
Poke(SCREEN_BG_COL, 0, BLACK);
Poke(SCREEN_FG_COL, 0, BLACK);
// point to start of random table
index:=0;
// infinite loop
cnt := 0;
while 1=1 do begin
// loop y
for y:=5 to 20 do begin
// moves current screen position
moveto(0, y, \$04); // \$04 is screen memory upper byte
// Select some random color
for x:=5 to 35 do begin
// Sets both screen and color values
screenmemory[x] := random_values[index];
// increases screen X counter
// Increase by some random non-repeatable prime
index:=index+17;
end;
moveto(0, y, \$D8); // \$04 is screen memory upper byte
// Select some random color
random_color := random_values[index]/16;
// restrict to 0-16
for x:=5 to 35 do begin
// Sets both screen and color values
screenmemory[x] := random_color;
// increases screen X counter

end;

end
end;
end.
``````