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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
retepCommented:


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simonetCommented:
Following...®
0
SofttechAuthor Commented:
retep...it works fine for my purposes.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.