Skip to content

Instantly share code, notes, and snippets.

@spacebat
Created February 16, 2015 12:52
Show Gist options
  • Select an option

  • Save spacebat/3f09a66b700befe5b892 to your computer and use it in GitHub Desktop.

Select an option

Save spacebat/3f09a66b700befe5b892 to your computer and use it in GitHub Desktop.
sample without replacement
#!/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