Created
February 16, 2015 12:50
-
-
Save spacebat/5fb7e70927a77e4dffbe to your computer and use it in GitHub Desktop.
sample with 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/first 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 $rand = int rand $total; | |
| say "Selected: ", | |
| map $_->[0], first { $rand < $_->[2] } @elements; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment