line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Random::Weighted; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: get weighted random data |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
26296
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
222
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
0
|
515
|
my $class = shift; |
12
|
1
|
|
50
|
|
|
4
|
my $args = shift || {}; |
13
|
1
|
|
|
|
|
3
|
my $self = bless {}, $class; |
14
|
1
|
|
|
|
|
4
|
$self->{'roller'} = $self->randomizer($args); |
15
|
1
|
|
|
|
|
3
|
return $self; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub randomizer { |
19
|
1
|
|
|
1
|
0
|
2
|
my ( $self, $args ) = @_; |
20
|
1
|
|
|
|
|
1
|
my ( $weight, $total ); |
21
|
1
|
|
|
|
|
2
|
my $count = 0; |
22
|
1
|
|
|
|
|
4
|
for my $key( keys %$args ) { |
23
|
1
|
|
|
|
|
2
|
my $set = $args->{$key}; |
24
|
1
|
|
|
|
|
2
|
$total += $set; |
25
|
1
|
|
|
|
|
3
|
for ( 1 .. $set ) { |
26
|
1
|
|
|
|
|
5
|
$weight->{$count++} = $key; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
return sub { |
30
|
1
|
|
|
1
|
|
35
|
my $rand = int(rand($total)); |
31
|
1
|
|
|
|
|
7
|
return $weight->{$rand}; |
32
|
|
|
|
|
|
|
} |
33
|
1
|
|
|
|
|
7
|
} |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
1
|
0
|
6
|
sub roll { &{ shift->{'roller'} }() } |
|
1
|
|
|
|
|
4
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
__END__ |