blib/lib/Math/Random/MT/Perl.pm

filter: Statementstmt 100% 137 / 137 Branchbran 94.4% 34 / 36 Conditioncond 100% 10 / 10 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 20 / 20 Podpod 100% 6 / 6 totaltotal 99.0% 212 / 214 CC 24 SCAR 12.7
CC
24
Cov
99%
_mt_setup_array
7.0
rand
5.0
import
5.0
CRAP 3.6
↑ Summary
1 package Math::Random::MT::Perl;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 $VERSION = 1.15;
8  
9 10 my $N = 624;
10 10 my $M = 397;
11 10 my $UP_MASK = 0x80000000;
12 10 my $LOW_MASK = 0x7fffffff;
13  
14 10 my $gen = undef;
15  
16  
17 18 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 # Create a Math::Random::MT::Perl object
19 18 my ($class, @seeds) = @_;
20 18 my $self = {};
21 18 bless $self, $class;
22 # Seed the random number generator
23 18 $self->set_seed(@seeds);
24 18 return $self;
25 }
26  
27  
28 36 ▶sub rand {
Subroutine rand: called CC=5 SCAR=16.1
Pod documented
29 # Generate a random number in requested range
30 36 my ($self, $range) = @_;
31 36 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 16 20 36
32 16 ▶ return ($range || 1) * $self->_mt_genrand();
Condition100%2/2
$range || 1
l!l
511
MC/DC100%1/1
$range || 1
$range
Truth table100%2/2
A
A= $range
Aresult
11
00
33 }
34 else {
35 20 $range = $self;
36 20 ▶ Math::Random::MT::Perl::srand() unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 1 19 20
37 20 ▶ return ($range || 1) * $gen->_mt_genrand();
Condition100%2/2
$range || 1
l!l
218
MC/DC100%1/1
$range || 1
$range
Truth table100%2/2
A
A= $range
Aresult
11
00
38 }
39 }
40  
41  
42 24 ▶sub irand {
Subroutine irand: called CC=3 SCAR=11.0
Pod documented
43 # Generate a random integer
44 24 my ($self) = @_;
45 24 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 16 8 24
46 16 return $self->_mt_genirand();
47 }
48 else {
49 8 ▶ Math::Random::MT::Perl::srand() unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 8 8
50 8 return $gen->_mt_genirand();
51 }
52 }
53  
54  
55 12 ▶sub get_seed {
Subroutine get_seed: called CC=1 SCAR=0.0
Pod documented
56 # Get the seed
57 12 my ($self) = @_;
58 12 return $self->{seed};
59 }
60  
61  
62 19 ▶sub set_seed {
Subroutine set_seed: called CC=3 SCAR=11.0
Pod documented
63 # Set the seed
64 19 my ($self, @seeds) = @_;
65 19 $self->{mt} = undef;
66 19 $self->{mti} = undef;
67 19 $self->{seed} = undef;
68 19 ▶ @seeds > 1 ? $self->_mt_setup_array(@seeds) :
Branch: 2 branches
BranchTrueFalseTotal
@seeds > 1 ? : 3 16 19
defined $seeds[0] ? : 14 2 16
69 $self->_mt_init_seed(defined $seeds[0] ? $seeds[0] : _rand_seed());
70 19 return $self->{seed};
71 }
72  
73  
74 10 ▶sub srand {
Subroutine srand: called CC=2 SCAR=6.9
Pod documented
75 # Seed the random number generator, automatically generating a seed if none
76 # is provided
77 10 my (@seeds) = @_;
78 10 ▶ if (not @seeds) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@seeds) 7 3 10
79 7 $seeds[0] = _rand_seed();
80 }
81 10 $gen = Math::Random::MT::Perl->new(@seeds);
82 10 my $seed = $gen->get_seed;
83 10 return $seed;
84 }
85  
86  
87 9 ▶sub _rand_seed {
Subroutine _rand_seed: called CC=1 SCAR=0.0
88 9 my ($self) = @_;
89  
90 # Get a seed at random through Perl's CORE::rand(). We do not call
91 # CORE::srand() to avoid altering the random numbers that other parts of
92 # the running script might be using. The seeds obtained by rapid calls to
93 # the _rand_seed() function are all different.
94
95 9 return int(CORE::rand(2**32));
96 }
97  
98  
99 # Note that we need to use integer some of the time to force integer overflow
100 # rollover ie 2**32+1 => 0. Unfortunately we really want uint but integer
101 # casts to signed ints, thus we can't do everything within an integer block,
102 # specifically the bitshift xor functions below. The & 0xffffffff is required
103 # to constrain the integer to 32 bits on 64 bit systems.
104  
105 19 ▶sub _mt_init_seed {
Subroutine _mt_init_seed: called CC=2 SCAR=6.9
106 19 my ($self, $seed) = @_;
107 19 my @mt;
108 19 $mt[0] = $seed & 0xffffffff;
109 19 ▶ for ( my $i = 1; $i < $N; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $N) 11837 19 11856
110 11837 my $xor = $mt[$i-1]^($mt[$i-1]>>30);
111 10 ▶ { use integer; $mt[$i] = (1812433253 * $xor + $i) & 0xffffffff }
Subroutine BEGIN: called CC=1 SCAR=0.0
112 }
113 19 $self->{mt} = \@mt;
114 19 $self->{mti} = $N;
115 19 $self->{seed} = ${$self->{mt}}[0];
116 }
117  
118  
119 3 ▶sub _mt_setup_array {
Subroutine _mt_setup_array: called CC=7 SCAR=19.5
120 3 my ($self, @seeds) = @_;
121 3 @seeds = map{ $_ & 0xffffffff }@seeds; # limit seeds to 32 bits
122 3 $self->_mt_init_seed( 19650218 );
123 3 my @mt = @{$self->{mt}};
124 3 my $i = 1;
125 3 my $j = 0;
126 3 my $n = @seeds;
127 3 ▶ my $k = $N > $n ? $N : $n;
Branch: 1 branch
BranchTrueFalseTotal
$N > $n ? : 3 0 3
128 3 my ($uint32, $xor);
129 3 ▶ for (; $k; $k--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 1872 3 1875
130 1872 $xor = $mt[$i-1] ^ ($mt[$i-1] >> 30);
131 10 ▶ { use integer; $uint32 = ($xor * 1664525) & 0xffffffff }
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1872 $mt[$i] = ($mt[$i] ^ $uint32);
133 10 ▶ { use integer; $mt[$i] = ($mt[$i] + $seeds[$j] + $j) & 0xffffffff }
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1872 $i++; $j++;
135 1872 ▶ if ($i>=$N) { $mt[0] = $mt[$N-1]; $i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $N) 3 1869 1872
136 1872 ▶ if ($j>=$n) { $j=0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= $n) 468 1404 1872
137 }
138 3 ▶ for ($k=$N-1; $k; $k--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 1869 3 1872
139 1869 $xor = $mt[$i-1] ^ ($mt[$i-1] >> 30);
140 10 ▶ { use integer; $uint32 = ($xor * 1566083941) & 0xffffffff }
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1869 $mt[$i] = ($mt[$i] ^ $uint32) - $i;
142 1869 $i++;
143 1869 ▶ if ($i>=$N) { $mt[0] = $mt[$N-1]; $i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $N) 3 1866 1869
144 }
145 3 $mt[0] = 0x80000000;
146 3 $self->{mt} = \@mt;
147 3 $self->{seed} = ${$self->{mt}}[0];
148 }
149  
150  
151 36 ▶sub _mt_genrand {
Subroutine _mt_genrand: called CC=1 SCAR=0.0
152 36 my ($self) = @_;
153 36 return $self->_mt_genirand*(1.0/4294967296.0);
154 }
155  
156  
157 60 ▶sub _mt_genirand {
Subroutine _mt_genirand: called CC=4 SCAR=13.9
158 60 my ($self) = @_;
159 60 my ($kk, $y);
160 60 my @mag01 = (0x0, 0x9908b0df);
161 60 ▶ if ($self->{mti} >= $N) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mti"} >= $N) 12 48 60
162 12 ▶ for ($kk = 0; $kk < $N-$M; $kk++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kk < $N - $M) 2724 12 2736
163 2724 $y = ($self->{mt}->[$kk] & $UP_MASK) | ($self->{mt}->[$kk+1] & $LOW_MASK);
164 2724 $self->{mt}->[$kk] = $self->{mt}->[$kk+$M] ^ ($y >> 1) ^ $mag01[$y & 1];
165 }
166 12 ▶ for (; $kk < $N-1; $kk++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kk < $N - 1) 4752 12 4764
167 4752 $y = ($self->{mt}->[$kk] & $UP_MASK) | ($self->{mt}->[$kk+1] & $LOW_MASK);
168 4752 $self->{mt}->[$kk] = $self->{mt}->[$kk+($M-$N)] ^ ($y >> 1) ^ $mag01[$y & 1];
169 }
170 12 $y = ($self->{mt}->[$N-1] & $UP_MASK) | ($self->{mt}->[0] & $LOW_MASK);
171 12 $self->{mt}->[$N-1] = $self->{mt}->[$M-1] ^ ($y >> 1) ^ $mag01[$y & 1];
172 12 $self->{mti} = 0;
173 }
174 60 $y = $self->{mt}->[$self->{mti}++];
175 60 $y ^= $y >> 11;
176 60 $y ^= ($y << 7) & 0x9d2c5680;
177 60 $y ^= ($y << 15) & 0xefc60000;
178 60 $y ^= $y >> 18;
179 60 return $y;
180 }
181  
182  
183 10 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
184 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
185 10 my $pkg = caller;
186 10 for my $sym (@_) {
187 22 ▶ *{"${pkg}::$sym"} = \&$sym if $sym eq "srand" or $sym eq "rand" or $sym eq "irand";
Branch: 1 branch
BranchTrueFalseTotal
if $sym eq "srand" or $sym eq "rand" or $sym eq "irand" 12 10 22
Condition100%6/6
$sym eq "srand" or $sym eq "rand" or $sym eq "irand"
l!l&&r!l&&!r
9310
$sym eq "srand" or $sym eq "rand"
l!l&&r!l&&!r
5413
MC/DC100%3/3
$sym eq "srand" or $sym eq "rand" or $sym eq "irand"
$sym eq "srand" $sym eq "rand" $sym eq "irand"
Truth table100%4/4
A or B or C
A= $sym eq "srand"B= $sym eq "rand"C= $sym eq "irand"
ABCresult
1XX1
01X1
0011
0000
188 }
189 }
190  
191  
192 10 1;
193  
194 __END__
↑ Summary