randomize.pl

randomize.pl is a small script I wrote, which does about the opposite of the sort-command in unix. sort reads all lines from a file (or stdin), sorts them and returns the result on stdout.

randomize.pl reads all lines from a file (or more files) or stdin, randomizes the order of the lines, and returns the output on stdout. I have found this usefull when working with MP3 playlists, but also other line-based files that need to be randomized.

You can download the script here

#!/usr/bin/perl
@array=<>;
for ($i=$#array;$i>0;$i--) {
  $j=int(rand($i));
  @array[$i,$j]=@array[$j,$i];
}
foreach $line (@array) {
  chomp($line);
  print("$line\n");
}
Last updated: 2005.04.29