Author: Jonas Bilinkevicius
Would you mind to make me a random procedure to change the background of my program
in an interval of 15 seconds?
Answer:
The best would be to store the names in an array:
1 const
2 CaImgs: array[0..9] of string = ('image1.jpg', 'image2.jpg', ...);
This way, on start-up, you can check that the images are there. Then, if you merely
want a random image from the array, you do:
3
4 myFileName = CaImgs[random(10)];
This means that you have one chance out of ten of repeating the same image - no
visible change. If you want to show always different images, but in random order,
then you need a shuffle function (see above). To shuffle your array of filenames
(despite being declared a constant, it's actually a var), you do this:
5
6 procedure shuffleImages;
7 var
8 a: array[0..high(CaImgs)] of integer;
9 j: integer;
10 s: string;
11 begin
12 for j := low(a) to high(a) do
13 a[j] := j;
14 shuffle(a, 0);
15 for j := low(a) to high(a) do
16 begin
17 s := CaImgs[j];
18 CaImgs[j] := CaImgs[a[j]];
19 CaImgs[a[j]] := s;
20 end;
21 end;
You do this once at application start. This way, the 10 images will show in random
order (but the order will repeat throughout the current run).
In both cases (random of shuffle), you should call Randomize just once, at the start of the application.
|