#!/usr/bin/perl -w
use strict;

print "m = n := ";
my $size = <STDIN>;
chomp($size);
print "number of blocks :";
my $numBlocks = <STDIN>;
chomp($numBlocks);
my $N = $size * $size;
my $blocks = $N / $numBlocks;
#print "$blocks\n";
my %numbers = ();
my $increment = 1;
my $r = 0;

my $filename = "rb.txt";

srand(time());

for(my $i=0;$i<$blocks;$i++){
	while($increment <= ($i * $numBlocks) + $numBlocks){
		$r = randNum($i*$numBlocks,($i*$numBlocks)+$numBlocks);
		if( $numbers{$r} ){
			#nothing
		}else{
			$numbers{$r} = $increment;
			$increment++;
		}
	}
}

open ORDER, ">$filename" or die "crap";
print ORDER "$size\t$size\n";
my @ordKeys = sort by_count keys %numbers;
foreach my $i (@ordKeys){
	my $c = $numbers{$i} - 1;
	print ORDER "$c\n";
}
close ORDER;

system("chmod 644 $filename");
print "PS I didn't check anything.  If it doesn't work, don't blame me.\n";

sub by_count {
	return $a <=> $b;
}
sub randNum {
	return int ( rand( $_[1] - $_[0] + 1 ) ) + $_[0];
}
