blib/lib/Role/Random/PerInstance.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 84.6% 22 / 26 Conditioncond 73.3% 11 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 90.8% 129 / 142 CC 21 SCAR 14.4
CC
21
Cov
92%
random
7.0
_binary_range
6.0
_constrain
5.0
CRAP 4.2
↑ Summary
1 package Role::Random::PerInstance;
2  
3 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.04';
9  
10 2 ▶use List::Util qw(sum reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Math::Round qw(nlowmult);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 has random_seed => (
14 is => 'rw',
15 isa => 'Int',
16 lazy => 1,
17 builder => '_build_random_seed',
18 );
19  
20 4 ▶sub _build_random_seed { 0 }
Subroutine _build_random_seed: called CC=1 SCAR=0.0
21  
22 # this is only used internally in deterministic_rand() to reset the seed for
23 # the next call to deterministic_rand().
24 2 has _seed => (
25 is => 'rw',
26 isa => 'Int',
27 predicate => '_seed_is_set',
28 );
29  
30 4030 ▶sub deterministic_rand {
Subroutine deterministic_rand: called CC=4 SCAR=14.4
Pod documented
31 4030 my $self = shift;
32 4030 state $modulus = 2**31 - 1;
33 4030 state $multiplier_a = 1_103_515_245;
34 4030 state $increment_c = 12_345;
35  
36 4030 ▶ if ( !$self->random_seed && !$self->_seed_is_set ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->random_seed or $self->_seed_is_set) 0 4030 4030
Condition33%1/3
$self->random_seed or $self->_seed_is_set
l!l&&r!l&&!r
403000
MC/DC0%0/2
$self->random_seed or $self->_seed_is_set
$self->random_seed $self->_seed_is_set
Truth table33%1/3
A or B
A= $self->random_seedB= $self->_seed_is_set
ABresult
1X1
011
000
37 0 croak("You must provide a random_seed to the constructor");
38 }
39  
40 # only set this once via random_seed. After that, this algorithm will set
41 # it.
42 4030 ▶ $self->_seed( $self->random_seed ) unless $self->_seed_is_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_seed_is_set 6 4024 4030
43 4030 my $xn_new = ( $multiplier_a * $self->_seed + $increment_c ) % $modulus;
44 4030 $self->_seed($xn_new);
45 4030 return 0 + sprintf "%0.9f" => substr( $xn_new, -7 ) / 10_000_000;
46 }
47  
48 3002 ▶sub attempt {
Subroutine attempt: called CC=2 SCAR=6.9
Pod documented
49 3002 my ( $self, $base_chance ) = @_;
50 3002 my $chance = _constrain( 0, $base_chance, 1 );
51  
52 3002 my $rand = $self->random;
53 3002 ▶ return $rand < $chance ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rand < $chance ? : 2250 752 3002
54 }
55  
56 17032 ▶sub random {
Subroutine random: called CC=7 SCAR=19.5
Pod documented
57 17032 my ( $self, $min, $max, $step ) = @_;
58 17032 ▶ $min //= 0;
Condition100%2/2
$min //= 0
l!l
140293003
MC/DC100%1/1
$min //= 0
$min
Truth table100%2/2
A
A= $min
Aresult
11
00
59 17032 ▶ $max //= 1;
Condition100%2/2
$max //= 1
l!l
140293003
MC/DC100%1/1
$max //= 1
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
60 17032 ▶ $step //= 0;
Condition100%2/2
$step //= 0
l!l
120005032
MC/DC100%1/1
$step //= 0
$step
Truth table100%2/2
A
A= $step
Aresult
11
00
61  
62 # We add $step to ensure that $max is inclusive in our random set.
63 # If $step is set to 0, then $max will be exclusive of the result set.
64 17032 my $maxrand = $max - $min + $step;
65 17032 ▶ $maxrand = nlowmult( $step, $maxrand ) if $step;
Branch: 1 branch
BranchTrueFalseTotal
if $step 12000 5032 17032
66 17032 ▶ my $rand =
Branch: 1 branch
BranchTrueFalseTotal
$self->random_seed ? : 4000 13032 17032
67 $self->random_seed
68 ? $self->deterministic_rand
69 : rand();
70 17032 $rand *= $maxrand;
71 17032 ▶ $rand = nlowmult( $step, $rand ) if $step;
Branch: 1 branch
BranchTrueFalseTotal
if $step 12000 5032 17032
72 17032 $rand += $min;
73  
74 17032 return $rand;
75 }
76  
77 3000 ▶sub random_int {
Subroutine random_int: called CC=1 SCAR=0.0
Pod documented
78 3000 my ( $self, $min, $max ) = @_;
79 3000 return $self->random( $min, $max, 1 );
80 }
81  
82 2000 ▶sub weighted_pick {
Subroutine weighted_pick: called CC=3 SCAR=11.0
Pod documented
83 2000 my ( $self, $weight_for ) = @_;
84 2000 my ( @weights, @choices );
85 2000 my $total = 0;
86  
87 # Use foreach with a sort to ensure that the order of items in weights and
88 # choices is always the same
89 2000 foreach my $choice ( sort keys %$weight_for ) {
90 10000 my $weight = $weight_for->{$choice};
91 10000 ▶ next unless $weight; # don't include weights of 0
Branch: 1 branch
BranchTrueFalseTotal
unless $weight 1000 9000 10000
92 9000 $total += $weight;
93 9000 push @weights => $total;
94 9000 push @choices => $choice;
95 }
96 return
97 2000 $choices[ $self->_binary_range( $self->random( 0, $weights[-1] ),
98 \@weights ) ];
99 }
100  
101 2000 ▶sub _binary_range {
Subroutine _binary_range: called CC=6 SCAR=17.9
102 2000 my ( $self, $elem, $list ) = @_;
103 2000 my $max = $#$list;
104 2000 my $min = 0;
105  
106 2000 ▶ while ( $max >= $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max >= $min) 3611 0 3611
107 3611 my $index = int( ( $max + $min ) / 2 );
108 3611 my $curr = $list->[$index];
109 3611 ▶ my $prev = 0 == $index ? 0 : $list->[ $index - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
0 == $index ? : 1131 2480 3611
110 3611 ▶ if ( $prev < $elem && $curr >= $elem ) { return $index }
Branch: 2 branches
BranchTrueFalseTotal
if ($prev < $elem and $curr >= $elem) { } 2000 1611 3611
elsif ($curr > $elem) { } 689 922 1611
Condition100%3/3
$prev < $elem and $curr >= $elem
!ll&&!rl&&r
6899222000
MC/DC100%2/2
$prev < $elem and $curr >= $elem
$prev < $elem $curr >= $elem
Truth table100%3/3
A and B
A= $prev < $elemB= $curr >= $elem
ABresult
0X0
100
111
111 689 elsif ( $curr > $elem ) { $max = $index - 1 }
112 922 else { $min = $index + 1; }
113 }
114 }
115  
116 3002 ▶sub _constrain {
Subroutine _constrain: called CC=4 SCAR=16.1
117 3002 my ( $min, $num, $max ) = @_;
118 3002 ▶ $max //= $num;
Condition33%1/3
$max //= $num
l!l&&r!l&&!r
300200
MC/DC0%0/2
$max //= $num
$max $num
Truth table33%1/3
A //= B
A= $maxB= $num
ABresult
1X1
011
000
119 return
120 3002 ▶ $num < $min ? $min
Branch: 2 branches
BranchTrueFalseTotal
$num < $min ? : 0 3002 3002
$num > $max ? : 0 3002 3002
121 : $num > $max ? $max
122 : $num;
123 }
124  
125 2 1;
126  
127 __END__
↑ Summary