blib/lib/Math/Random/Discrete.pm

filter: Statementstmt 100% 46 / 46 Branchbran 81.2% 13 / 16 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 84.5% 71 / 84 CC 14 SCAR 21.9
CC
14
Cov
89%
new
12.4
rand
3.0
BEGIN
1.0
CRAP 9.0
↑ Summary
1 package Math::Random::Discrete;
2 2 $Math::Random::Discrete::VERSION = '1.02';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Discrete random variables with general distributions
9  
10 # This is an implementation of Walker's alias method.
11  
12 1 ▶sub new {
Subroutine new: called CC=12 SCAR=25.2
Pod documented
13 1 my ($class, $_weights, $values) = @_;
14  
15 1 ▶ croak("No weights specified")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_weights and @$_weights 0 1 1
Condition33%1/3
defined $_weights and @$_weights
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_weights and @$_weights
defined $_weights @$_weights
Truth table33%1/3
A and B
A= defined $_weightsB= @$_weights
ABresult
0X0
100
111
16 if !defined($_weights) || !@$_weights;
17 1 ▶ croak("Number of values must equal number of weights")
Branch: 1 branch
BranchTrueFalseTotal
if defined $values and @$values != @$_weights 0 1 1
Condition33%1/3
defined $values and @$values != @$_weights
!ll&&!rl&&r
010
MC/DC0%0/2
defined $values and @$values != @$_weights
defined $values @$values != @$_weights
Truth table33%1/3
A and B
A= defined $valuesB= @$values != @$_weights
ABresult
0X0
100
111
18 if defined($values) && @$values != @$_weights;
19  
20 1 my @weights = @$_weights;
21  
22 # compute average weight
23  
24 1 my $N = @weights;
25 1 my $sum = 0;
26  
27 1 for my $weight (@weights) {
28 10 $sum += $weight;
29 }
30  
31 1 my $avg = $sum / $N;
32  
33 # split weights into two groups: smaller and larger than average
34  
35 1 my (@small, @large);
36  
37 1 ▶ for (my $i = 0; $i < $N; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $N) 10 1 11
38 10 ▶ if ($weights[$i] <= $avg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weights[$i] <= $avg) { } 5 5 10
39 5 push(@small, $i);
40 }
41 else {
42 5 push(@large, $i);
43 }
44 }
45  
46 # generate F and A arrays
47  
48 1 my (@F, @A);
49  
50 1 ▶ while (@small and @large) {
Branch: 1 branch
BranchTrueFalseTotal
if (@small and @large) 9 1 10
Condition66%2/3
@small and @large
!ll&&!rl&&r
019
MC/DC50%1/2
@small and @large
@small @large
Truth table66%2/3
A and B
A= @smallB= @large
ABresult
0X0
100
111
51 9 my $i = pop(@small);
52 9 my $j = $large[-1];
53 9 $A[$i] = $j;
54 9 $F[$i] = $weights[$i] / $avg;
55  
56 9 $weights[$j] -= $avg - $weights[$i];
57  
58 9 ▶ push(@small, pop(@large))
Branch: 1 branch
BranchTrueFalseTotal
if $weights[$j] <= $avg 5 4 9
59 if $weights[$j] <= $avg;
60 }
61  
62 1 for my $i (@small, @large) {
63 1 $A[$i] = $i;
64 1 $F[$i] = 1.0;
65 }
66  
67 # create blessed ref
68  
69 1 my $self = {
70 values => $values,
71 A => \@A,
72 F => \@F,
73 };
74  
75 1 return bless($self, $class);
76 }
77  
78 55000 ▶sub rand {
Subroutine rand: called CC=3 SCAR=11.0
Pod documented
79 55000 my $self = shift;
80  
81 55000 my $F = $self->{F};
82 55000 my $r = CORE::rand(@$F);
83 55000 my $ri = int($r); # integer part
84 55000 my $rf = $r - $ri; # fractional part
85  
86 55000 ▶ my $i = $rf < $F->[$ri] ? $ri : $self->{A}[$ri];
Branch: 1 branch
BranchTrueFalseTotal
$rf < $F->[$ri] ? : 36018 18982 55000
87  
88 55000 my $values = $self->{values};
89  
90 55000 ▶ return $values ? $values->[$i] : $i;
Branch: 1 branch
BranchTrueFalseTotal
$values ? : 55000 0 55000
91 }
92  
93 2 1;
94  
95 __END__
↑ Summary