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 create a random list of numbers 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
71
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I should give an example of what I'm trying to do. The NewTrackList procedure is 
supposed to create a list of 14 numbers from 1 to 14, with no numbers repeated. The 
list is supposed to be random, that is, a different sequence of numbers is created 
every time the procedure runs.

Answer:
1   
2   procedure NewTrackList;
3   var
4     TrackNumbersList: array[1..14] of Integer;
5     I, II: Integer;
6     SameTracks: Boolean;
7     S: string;
8   begin
9     for I := 1 to 14 do
10      TrackNumbersList[I] := 0;
11    for I := 1 to 14 do
12    begin
13      TrackNumbersList[I] := Random(14) + 1;
14      repeat
15        SameTracks := False;
16        for II := 1 to I - 1 do
17        begin
18          if I = 1 then
19            Break;
20          if TrackNumbersList[I] = TrackNumbersList[II] then
21          begin
22            SameTracks := True;
23            TrackNumbersList[I] := Random(14) + 1;
24            Break;
25          end;
26        end;
27      until
28        not SameTracks;
29    end;
30    S := '';
31    for I := 1 to 14 do
32      S := S + '  ' + IntToStr(TrackNumbersList[I]);
33    Form1.Label1.Caption := S;
34  end;
35  
36  procedure TTunesMain.FormCreate(Sender: TObject);
37  begin
38    Randomize;
39    NewTrackList;
40  end;


S is a local variable of type String. I obviously added a TLabel to the form, as well.

			
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