| 1 | package App::Sandy::Role::Counter; | ||
| 2 | # ABSTRACT: Bayes counter | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'role'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.25'; # VERSION | ||
| 7 | |||
| 8 | sub with_make_counter { | ||
| 9 | # ALgorithm based in perlfaq: | ||
| 10 | # How do I select a random line from a file? | ||
| 11 | # "The Art of Computer Programming" | ||
| 12 | |||
| 13 | my ($self, $num, $picks, $rng) = @_; | ||
| 14 | return sub { | ||
| 15 | state $count_down = $num; | ||
| 16 | state $picks_left = $picks; | ||
| 17 | |||
| 18 | my $rc = 0; | ||
| 19 | my $rand = $rng->get_n($count_down); | ||
| 20 | |||
| 21 | if ($rand < $picks_left) { | ||
| 22 | $rc = 1; | ||
| 23 | $picks_left--; | ||
| 24 | } | ||
| 25 | |||
| 26 | $count_down--; | ||
| 27 | return $rc; | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | __END__ | ||