Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use Randomize so that the same value is not chosen more than once (2) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Algorithm
Language
Delphi 2.x
Views
78
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC