Skip to content

Instantly share code, notes, and snippets.

@junichiro
Created May 15, 2009 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichiro/112021 to your computer and use it in GitHub Desktop.
Save junichiro/112021 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util qw/shuffle/;
my $roop = 1000;
my $number = 100;
my $limit = 50;
my @persons = 1 .. $number;
my $success = 0;
foreach ( 1 .. $roop ) {
my $cards = [ shuffle 1 .. $number ];
$success++ if ( is_success($cards) );
}
print $success / $roop, "\n";
sub is_success {
my $cards = shift;
my $is_ok = 0;
PERSON: foreach my $num (@persons) {
my $my_num = $num;
CARD: foreach ( 1 .. $limit ) {
$num = $cards->[ $num - 1 ];
next CARD unless ( $my_num eq $num );
$is_ok++;
next PERSON;
}
}
return ( $is_ok eq $number ) ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment