use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;

my $num_of_pics = 535;
my $pics_per_package = 5;
my $iterations = 1;

GetOptions ("pics=i"       => \$num_of_pics,
            "ppp=i"        => \$pics_per_package,
            "iterations=i" => \$iterations
            );

my @collection;
my $pics_collected = $num_of_pics;

for (my $i = 1; $i<= $num_of_pics; $i++) {
  push(@collection, $i);
}

my @choice = @collection;

my $num_bought_pics = 0;
my $pics_per_slot = {};

my $i;

for ($i = 1; $i <= $iterations; $i++) {
   my $pic_stats = {};
   my @collect = @collection;
   while (scalar @collect > 0) {
      $num_bought_pics += $pics_per_package;
      my @package = &get_package;
      foreach my $pic (@package) {
        $pic_stats->{$pic}++;
        @collect = grep { $_ != $pic } @collect; ## very inefficient but me not cares
      }
    }
    
    foreach my $pic (keys %$pic_stats) {
      $pics_per_slot->{$pic_stats->{$pic}}++;
    }  
}        
    
print "How many n-plicates per collection\n";
foreach my $num ( sort {$a <=> $b} keys %$pics_per_slot ) {
  printf "%+5s : %3.5f\n", $num, $pics_per_slot->{$num}/$iterations;
}

print  "options:\n";
printf "pictures in collection [pics]       : %d\n", $num_of_pics;
printf "pictures per package   [ppp]        : %d\n", $pics_per_package;
printf "number of iterations   [iterations] : %d\n", $iterations;
printf "pictures: %s \n", $num_bought_pics/$iterations;
printf "packages: %s \n", $num_bought_pics/$pics_per_package/$iterations;

sub get_package {
  my @package;
  my @seed = @choice;
  
  for (my $j = 0; $j <= $pics_per_package - 1; $j++) {
    my $zuf = int( rand()*($num_of_pics - $j) );  
    my $zahl = splice (@seed, $zuf, 1);
    push (@package, $zahl);
  }  
  return @package;
}