123456789 to 482176935

I am looking for suggestions on how to handle the following task using Delphi 3...

Assuming I have a text file which contains 1000 lines of text (the 1000 could vary, BTW)...  I would like to generate a new file containing the same 1000 lines of text, HOWEVER, I want the lines to be RANDOMIZED, so that...

1
2
3
4
5
6
7
8
9

...will be saved to disk as...

4
8
2
1
7
6
9
3
5

...or some other randomized order.

Any suggestions on the best method to do this?
SofttechAsked:
Who is Participating?
 
retepConnect With a Mentor Commented:


Hi Softtech. I have a suggestion for a solution. It might not be the smartest way to do it, but it works for the intentions...


Procedure RandomizeTextFiles(OrgText,NewText:string);
Var
T : textfile;//existing file
T2: TextFile;//newFile
StrList: TStringList;
i,j,k,ran : integer;
s,temp : String;

begin
  StrList := TStringList.Create;
  Randomize;
  i := 0;
  AssignFile(t, OrgText);
  Reset(t);
  try
   //Put all the lines of the original *.txt-file in an TStringList
   While not eof(t) do
   begin
     inc(i);
     readln(t,s);
     StrList.Add(s);
   end;
   //Randomize the content of the TStringList
   For k := 0 to i - 1  do
   begin
      Ran := random(i);
      temp := StrList[Ran];
      StrList[ran] := StrList[k];
      Strlist[k]:= temp;
   end;

  Assignfile(T2,NewText);
  Rewrite(T2);
  //Enter the new data to the new file;
  For J:= 0 to i - 1 do
    Writeln(T2,StrList[j]);


  finally
    CloseFile(T);
    CloseFile(T2);
  end;

end;



Peter :-)
0
 
sburckCommented:
If you keep your lines in a TStringList, and made a randomizer your customsort function, each call to it would randomize the order of the list.  Look at the docs on customsort.
0
 
simonetCommented:
Following...®
0
 
SofttechAuthor Commented:
retep...it works fine for my purposes.  Thanks!
0
All Courses

From novice to tech pro — start learning today.