File Coverage

blib/lib/Math/Random/Free.pm
Criterion Covered Total %
statement 22 32 68.7
branch 1 10 10.0
condition n/a
subroutine 8 9 88.8
pod 0 5 0.0
total 31 56 55.3


line stmt bran cond sub pod time code
1             package Math::Random::Free;
2              
3 1     1   518 use strict;
  1         3  
  1         28  
4 1     1   5 use warnings;
  1         1  
  1         45  
5              
6             # ABSTRACT: Free drop-in replacement for Math::Random
7             our $VERSION = '0.1.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
18             0.72, serving as drop-in replacement for this module.
19              
20             =head1 MOTIVATION
21              
22             L is a great and widely-used module for the
23             generation of random numbers and permutations. Despite being open-source,
24             L does not fulfill free open-source software
25             definitions as established by the Open Source Initiative
26             (L) and the Debian Project
27             (L, a.k.a. DFSG). This
28             is mostly because C code cannot be copied nor distributed for
29             direct commercial advantage. Math::Random::Free is created to free the
30             code depending on L from these limitations.
31              
32             =cut
33              
34 1     1   572 use Digest::SHA qw( sha1_hex );
  1         3259  
  1         90  
35 1     1   8 use List::Util qw( shuffle );
  1         2  
  1         533  
36              
37             require Exporter;
38             our @ISA = qw(Exporter);
39             our @EXPORT = qw(
40             random_permutation
41             random_permuted_index
42             random_set_seed_from_phrase
43             );
44             our @EXPORT_OK = qw(
45             random_permutation
46             random_permuted_index
47             random_set_seed_from_phrase
48             random_uniform
49             random_uniform_integer
50             );
51              
52             =head1 SUPPORT
53              
54             our @EXPORT = qw(
55             random_permutation
56             random_permuted_index
57             random_set_seed_from_phrase
58             );
59             our @EXPORT_OK = qw(
60             random_permutation
61             random_permuted_index
62             random_set_seed_from_phrase
63             random_uniform
64             random_uniform_integer
65             );
66              
67             =cut
68              
69             sub random_permutation
70             {
71 1     1 0 4 my( @array ) = @_;
72 1         3 return @array[random_permuted_index( scalar @array )];
73             }
74              
75             sub random_permuted_index
76             {
77 1     1 0 3 my( $n ) = @_;
78              
79 1         14 return shuffle 0..$n-1;
80             }
81              
82             sub random_set_seed_from_phrase
83             {
84 1     1 0 911 my( $seed ) = @_;
85              
86             # On 64-bit machine the max. value for srand() seems to be 2**50-1
87 1         16 srand hex substr( sha1_hex( $seed ), 0, 6 );
88             }
89              
90             sub random_uniform
91             {
92 0     0 0 0 my( $n, $low, $high ) = @_;
93              
94 0 0       0 $n = 1 unless defined $n;
95 0 0       0 $low = 0 unless defined $low;
96 0 0       0 $high = 1 unless defined $high;
97              
98 0 0       0 if( wantarray ) {
99 0         0 return map { rand() * ($high - $low) + $low } 1..$n;
  0         0  
100             } else {
101 0         0 return rand() * ($high - $low) + $low;
102             }
103             }
104              
105             sub random_uniform_integer
106             {
107 1     1 0 7 my( $n, $low, $high ) = @_;
108              
109 1         4 my $range = int($high) - int($low) + 1;
110              
111 1 50       4 if( wantarray ) {
112 0         0 return map { int( rand($range) + $low ) } 1..$n;
  0         0  
113             } else {
114 1         10 return int( rand($range) + $low );
115             }
116             }
117              
118             =head1 CAVEATS
119              
120             This module has only a subset of L subroutines
121             (contributions welcome), implemented using either Perl core subroutines
122             or other well-known modules. Thus Math::Random::Free is neither as
123             complete, nor as fast, nor as random as L.
124             Also Math::Random::Free does not aim for cryptographic security.
125              
126             While Math::Random::Free supports seed setting, it does that differently
127             from L. It means that one should not expect
128             the same seed producing identical random sequences in both modules.
129              
130             As Math::Random::Free employs L for producing
131             random permutations, these are influenced by C<$List::Util::RAND>
132             variable.
133              
134             =head1 TESTED WITH
135              
136             =over 4
137              
138             =item *
139              
140             L 0.02
141              
142             =back
143              
144             =head1 AUTHOR
145              
146             Andrius Merkys, L
147              
148             =cut
149              
150             1;