Randomize the lines in a file

Ask questions about how to create a script or swap scripts with other users.
Post Reply
gRider
Posts: 2
Joined: 24 Oct 2009 22:09

Randomize the lines in a file

Post 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.

fred
Posts: 90
Joined: 02 Oct 2008 11:43
Location: The Netherlands

Post 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.

gRider
Posts: 2
Joined: 24 Oct 2009 22:09

Post 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.

Shoe123
Posts: 1
Joined: 03 Dec 2014 13:28

Re: Randomize the lines in a file

Post 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;
Pass your itil certification and PW0-105 braindumps exams in first try by using our northwestern passguide and www.usna.edu and best quality

Post Reply