blib/lib/Math/Random/Free.pm

filter: Statementstmt 77.7% 42 / 54 Branchbran 45.8% 11 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 7 totaltotal 65.6% 63 / 96 CC 13 SCAR 21.9
CC
13
Cov
68%
random_uniform
30.0
random_normal
5.0
random_exponential
4.9
CRAP 9.0
↑ Summary
1 package Math::Random::Free;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Free drop-in replacement for Math::Random
7 1 our $VERSION = '0.2.0'; # VERSION
8  
9 =pod
10  
11 =head1 NAME
12  
13 Math::Random::Free - free drop-in replacement for Math::Random
14  
15 =head1 DESCRIPTION
16  
17 This is free (see below) implementation of L<Math::Random|Math::Random>
18 0.72, serving as drop-in replacement for this module.
19  
20 =head1 MOTIVATION
21  
22 L<Math::Random|Math::Random> is a great and widely-used module for the
23 generation of random numbers and permutations. Despite being open-source,
24 L<Math::Random|Math::Random> does not fulfill free open-source software
25 definitions as established by the Open Source Initiative
26 (L<https://opensource.org/osd>) and the Debian Project
27 (L<https://www.debian.org/social_contract#guidelines>, a.k.a. DFSG). This
28 is mostly because C<randlib> code cannot be copied nor distributed for
29 direct commercial advantage. Math::Random::Free is created to free the
30 code depending on L<Math::Random|Math::Random> from these limitations.
31  
32 =cut
33  
34 1 ▶use Digest::SHA qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 require Exporter;
38 1 our @ISA = qw(Exporter);
39 1 our @EXPORT = qw(
40 random_normal
41 random_permutation
42 random_permuted_index
43 random_set_seed_from_phrase
44 );
45 1 our @EXPORT_OK = qw(
46 random_exponential
47 random_normal
48 random_permutation
49 random_permuted_index
50 random_set_seed_from_phrase
51 random_uniform
52 random_uniform_integer
53 );
54 1 our $PI = 4 * atan2( 1, 1 );
55  
56 =head1 SUPPORT
57  
58 our @EXPORT = qw(
59 random_normal
60 random_permutation
61 random_permuted_index
62 random_set_seed_from_phrase
63 );
64 our @EXPORT_OK = qw(
65 random_exponential
66 random_normal
67 random_permutation
68 random_permuted_index
69 random_set_seed_from_phrase
70 random_uniform
71 random_uniform_integer
72 );
73  
74 =cut
75  
76 sub random_exponential
77 2 ▶{
Subroutine random_exponential: called CC=4 SCAR=15.9
Pod undocumented
78 2 my( $n, $rate ) = @_;
79  
80 2 ▶ $n = 1 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2 0 2
81 2 ▶ $rate = 1 unless defined $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate 2 0 2
82  
83 2 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
84 0 return map { scalar random_exponential( $n, $rate ) } 1..$n;
85 } else {
86 2 return log( 1 - rand ) / -$rate;
87 }
88 }
89  
90 sub random_normal
91 12 ▶{
Subroutine random_normal: called CC=5 SCAR=16.1
Pod undocumented
92 12 my( $n, $mean, $sd ) = @_;
93  
94 12 ▶ $n = 1 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 12 12
95 12 ▶ $mean = 0 unless defined $mean;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mean 2 10 12
96 12 ▶ $sd = 1 unless defined $sd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sd 2 10 12
97  
98 12 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 10 12
99 2 return map { scalar random_normal( $n, $mean, $sd ) } 1..$n;
100 } else {
101 # Box-Muller method
102 10 return $mean + $sd * cos( 2 * $PI * (1 - rand) )
103 * sqrt( -2 * log(1 - rand) );
104 }
105 }
106  
107 sub random_permutation
108 2 ▶{
Subroutine random_permutation: called CC=1 SCAR=0.0
Pod undocumented
109 2 my( @array ) = @_;
110 2 return @array[random_permuted_index( scalar @array )];
111 }
112  
113 sub random_permuted_index
114 2 ▶{
Subroutine random_permuted_index: called CC=1 SCAR=0.0
Pod undocumented
115 2 my( $n ) = @_;
116  
117 2 return shuffle 0..$n-1;
118 }
119  
120 sub random_set_seed_from_phrase
121 8 ▶{
Subroutine random_set_seed_from_phrase: called CC=1 SCAR=0.0
Pod undocumented
122 8 my( $seed ) = @_;
123  
124 # On 64-bit machine the max. value for srand() seems to be 2**50-1
125 8 srand hex substr( sha1_hex( $seed ), 0, 6 );
126 }
127  
128 sub random_uniform
129 0 ▶{
Subroutine random_uniform: not called CC=5 SCAR=34.0
Pod undocumented
130 0 my( $n, $low, $high ) = @_;
131  
132 0 ▶ $n = 1 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
133 0 ▶ $low = 0 unless defined $low;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $low 0 0 0
134 0 ▶ $high = 1 unless defined $high;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high 0 0 0
135  
136 0 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
137 0 return map { rand() * ($high - $low) + $low } 1..$n;
138 } else {
139 0 return rand() * ($high - $low) + $low;
140 }
141 }
142  
143 sub random_uniform_integer
144 2 ▶{
Subroutine random_uniform_integer: called CC=2 SCAR=7.9
Pod undocumented
145 2 my( $n, $low, $high ) = @_;
146  
147 2 my $range = int($high) - int($low) + 1;
148  
149 2 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
150 0 return map { int( rand($range) + $low ) } 1..$n;
151 } else {
152 2 return int( rand($range) + $low );
153 }
154 }
155  
156 =head1 CAVEATS
157  
158 This module has only a subset of L<Math::Random|Math::Random> subroutines
159 (contributions welcome), implemented using either Perl core subroutines
160 or other well-known modules. Thus Math::Random::Free is neither as
161 complete, nor as fast, nor as random as L<Math::Random|Math::Random>.
162 Also Math::Random::Free does not aim for cryptographic security.
163  
164 While Math::Random::Free supports seed setting, it does that differently
165 from L<Math::Random|Math::Random>. It means that one should not expect
166 the same seed producing identical random sequences in both modules.
167  
168 As Math::Random::Free employs L<List::Util|List::Util> for producing
169 random permutations, these are influenced by C<$List::Util::RAND>
170 variable.
171  
172 =head1 TESTED WITH
173  
174 =over 4
175  
176 =item *
177  
178 L<Graph::Maker> 0.02
179  
180 =back
181  
182 =head1 AUTHOR
183  
184 Andrius Merkys, L<mailto:merkys@cpan.org>
185  
186 =cut
187  
188 1 1;
↑ Summary