Page 1 of 1

Randomize the lines in a file

Posted: 24 Oct 2009 22:14
by gRider
I am a bit rusty on my Pascal. I have a need to randomize the lines in a text file. Does anyone have a sample script that might help me get started? I need to do this several times a week so it seems worth the effort to create a script. The files contain between 10 and 20 thousand lines.

Posted: 03 Nov 2009 13:38
by fred
Don't know if this works for you but it's a start, i don't think it will run very fast on so many lines.

Code: Select all

// Pascal script

// Global variables
var
  i, n, m: integer;
  temp: WideString;

// Enter code here
begin
  Randomize;
  // the times you want to run it
  for i := 1 to 2 do begin  
    for n := 0 to Document.LineCount-1 do
    begin
      // pick a random number
      m := Trunc(Random * Document.LineCount);
      // swap lines
      temp := Document.Lines(n);
      Document.Lines(n) := Document.Lines(m);
      Document.Lines(m) := Temp;
    end;
  end;
end.

Posted: 03 Nov 2009 20:55
by gRider
Excellent. Works perfectly for what I needed. Wicked slow on the large files but it does get the job done. Thank you very much.

Re: Randomize the lines in a file

Posted: 03 Dec 2014 13:28
by Shoe123
begin
// pick a random number
m := Trunc(Random * Document.LineCount);
// swap lines
temp := Document.Lines(n);
Document.Lines(n) := Document.Lines(m);
Document.Lines(m) := Temp;