Skip to content

Instantly share code, notes, and snippets.

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

  • Save spacebat/5fb7e70927a77e4dffbe to your computer and use it in GitHub Desktop.

Select an option

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