#!/usr/local/bin/perl

# configuration
my $factor = .5;		# how much gets swapped

# check params
if ($#ARGV != 1) {
  print "Usage: random.pl file1 file2\n";
  exit 1;
}

# init random number generator
srand;

# open files
open(FILE1, "$ARGV[0]");
open(FILE2, "$ARGV[1]");

$_ = int(rand 1000);
open(FILE1R, ">$ARGV[0].$_");
open(FILE2R, ">$ARGV[1].$_");

# read files
my @file1 = <FILE1>;
my @file2 = <FILE2>;

# randomly swap lines for random lines
for ($i = 0; $i <= $#file1; $i++) {
  if ((rand 1) > $factor) {
    my $tmp = $file1[$i];
    my $r = int(rand $#file2);
    $file1[$i] = $file2[$r];
    $file2[$r] = $tmp;
  }
}

# print new files
print FILE1R (join /\n/, @file1);
print FILE2R (join /\n/, @file2);

# close all files
close(FILE2R);
close(FILE1R);
close(FILE1);
close(FILE2);
