#!/usr/local/bin/perl
# -----------------------------------------------------------------
#
# 'randomseq.pl'
#
# Generates a random sequence of all numbers
# between two given numbers (min, max).
#
# -----------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------
#
# Copyright (c) 2005 by Joerg Hau
#
# Author:   Joerg Hau
# e-mail:   joerg.hau-at-dplanet.ch
#
# Revision History:
#   2005-11-30, first working version (JHa)
#   2005-12-02, cmd line checks (JHa)
#   2005-12-05, more help text (JHa)
# -----------------------------------------------------------------

use strict;
use warnings;

# -----------------------------------------------------------------
# random_seq:   generates a randomized sequence of numbers
# input     :   min and max number in the array
# returns   :   array of randomized numbers
# -----------------------------------------------------------------
sub random_seq
    {
    my ($first, $last)=@ARGV;
    my @list = ($first .. $last);   # fill list with sequential numbers
    my ($listsize, $i, @pool);

    for ($i = ($last-$first); $i >= 0; $i--)
    # count backwards since the array size will shrink now:
        {
        $listsize = @list;
        # generate random number between 0 and $listsize,
        # then remove this number from the list
        $pool[$i] = splice (@list, rand $listsize, 1);
        }
    return @pool;
    }


# -----------------------------------------------------------------
# Print usage mode
# -----------------------------------------------------------------
sub usage
{
print <<EOF
$0 - Generate a random sequence of all numbers between "min" and "max".

Copyright (c) 2005 Joerg Hau <joerg.hau(at)dplanet.ch>.

This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Usage: $0 min max

EOF
}

# -----------------------------------------------------------------
# testing
# -----------------------------------------------------------------
my $numArgs = $#ARGV + 1;
unless ($numArgs==2) {
    usage;
    die "Error: Must use exactly 2 arguments.\n"
    }

my($min, $max)=@ARGV;
# test if args are numbers
($min =~ /\d+/) or die "Error: '$min' is not a number.\n";
($max =~ /\d+/) or die "Error: '$max' is not a number.\n";

my @num=random_seq ($min, $max);
my $nelem=@num;         # number of elements

for (0 .. $nelem-1)
    {
    print $num[$_], "\n";
    }

1;
