#!/usr/bin/perl -w
# Perl script: gendata
# Generates a "cascading" matrix, e.g.

use strict;

use Getopt::Declare;

my $usage_spec = '
	<rowLength:+i>	row length
	[required]

	<onesPerRow:+i>	number of ones per row
	[required]

	-r	number
';

my $args = Getopt::Declare->new($usage_spec) or die;

my $rowLength = $args->{'<rowLength>'};
my $onesPerRow = $args->{'<onesPerRow>'};
my $rowCount = $rowLength - $onesPerRow + 1;
my $randomize = $args->{'-r'};

my @data;

for(my $r = 0; $r < $rowCount; $r++)
{
      my $i;
      my @row;
      for($i = 0; $i < $r; $i++)
      {	  
	  $row[$i] = 0;
      }
      for(; $i < $r+$onesPerRow; $i++)
      {	  
	  $row[$i] = 1;
      }
      for(; $i < $rowLength; $i++)
      {	  
	  $row[$i] = 0;
      }
      $data[$r] = [ @row ] ;
}

if($randomize)
{
    for(my $r = 0; $r < $rowCount; $r++)
    {
#randomly swap 2 rows
	my $rand = int(rand($rowCount));
	
	( $data[$r], $data[$rand] ) = ( $data[$rand], $data[$r] );
    }
}

print "$rowCount\t$rowLength\n";

for my $i (0 .. $#data ) {
    my $aref = $data[$i];
    my $n = @$aref - 2;
    for my $j (0 .. $n) 
    {
	print "$aref->[$j]\t";
    }
    print "$aref->[$n + 1]";
    print "\n";
}
