Created
February 16, 2015 12:52
-
-
Save spacebat/3f09a66b700befe5b892 to your computer and use it in GitHub Desktop.
sample without replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use Modern::Perl; | |
| use List::Util qw/sum/; | |
| my @elements = (["low", 10], ["mid", 100], ["high", 1000]); | |
| my $accum = 0; | |
| my $total = sum map { | |
| $_->[2] = $accum += $_->[1]; | |
| $_->[1]; | |
| } @elements; | |
| for (1..10) { | |
| my $found = 0; | |
| my $rand = int rand $total; | |
| say "Selected: ", map $_->[0], grep { | |
| if ($rand < $_->[2]) { | |
| $_->[2]--; | |
| ! $found++; | |
| } | |
| } @elements; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment