#!/usr/bin/perl

#use strict();
use vars qw($opt_s $opt_b, $opt_z $USAGE
            $seed $COUNT_BEWERBER $COUNT_ZUFALL @ALL @GEWINNER);
use Getopt::Std;


$USAGE=<<EOUSAGE;
Usage: $NAME [options]

Options: -h        Show this help.
         -s        zufälliger Startwert (falls 0, wird time() benutzt!)
	 -b        Anzahl der Bewerber
	 -z        Anzahl zu berechnender Zufallszahlen

EOUSAGE


getopts('hs:b:z:');
die $USAGE if($opt_h || $opt_H);


#if (defined $opt_s) { $seed = $opt_s == 0 ? time() % 10 : $opt_s; }
#if (defined $opt_s) { $seed = $opt_s; }
#else {
#    print "Zufaelligen Startwert eingeben: ";
#    chomp($seed = <STDIN>);
#}

if ($opt_b) { $COUNT_BEWERBER = $opt_b; }
else {
    print "Anzahl aller Bewerber: ";
    chomp($COUNT_BEWERBER = <STDIN>);
}

if ($opt_z) { $COUNT_ZUFALL = $opt_z; }
else {
    print "Anzahl zu berechnender Zufallszahlen: ";
    chomp($COUNT_ZUFALL = <STDIN>);
}


print "$COUNT_BEWERBER $COUNT_ZUFALL\n\n";
print "\n";

if ($COUNT_ZUFALL >= $COUNT_BEWERBER) { print "Alle Bewerber haben gewonnen!\n"; exit(0); }



for ($i=0; $i<$COUNT_BEWERBER; $i++) { $ALL[$i] = $i; } ; # put competitors into the array

#if ($seed) { srand($seed); } else { srand(); }

$last = $COUNT_BEWERBER;
$c    = $COUNT_ZUFALL;
$done = 0;

while ($c-- > 0) {
    $z = int(rand($last--));
    print "wuerfle: $z --> $ALL[$z]\n";
    $GEWINNER[$done++] = $ALL[$z];
    $ALL[$z] = $ALL[$last];
}

print "\nGewinner: \n";
for ($i=0; $i<$COUNT_ZUFALL;$i++) { print "$GEWINNER[$i] "; }
print "\n\n";