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
|
|
15240
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
9
|
1
|
|
|
1
|
|
3
|
use Exporter 'import'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
166
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw( randomizer ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub randomizer { |
13
|
1
|
|
|
1
|
0
|
437
|
my $args = shift; |
14
|
1
|
|
|
|
|
2
|
my $total; |
15
|
1
|
|
|
|
|
4
|
$total += $_ for values %$args; |
16
|
1
|
|
|
|
|
1
|
my $weight; |
17
|
1
|
|
|
|
|
2
|
my $count = 0; |
18
|
1
|
|
|
|
|
2
|
for my $key( keys %$args ) { |
19
|
1
|
|
|
|
|
2
|
my $set = $args->{$key}; |
20
|
1
|
|
|
|
|
2
|
for ( 1 .. $set ) { |
21
|
1
|
|
|
|
|
3
|
$weight->{$count++} = $key; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
return sub { |
25
|
1
|
|
|
1
|
|
31
|
my $rand = int(rand($total)); |
26
|
1
|
|
|
|
|
5
|
return $weight->{$rand}; |
27
|
|
|
|
|
|
|
} |
28
|
1
|
|
|
|
|
5
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
1; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
__END__ |