blib/lib/Games/ABC_Path/MicrosoftRand.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 43 / 43 CC 3 SCAR 4.7
CC
3
Cov
100%
shuffle
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« blib/lib/Games/ABC_Path/Generator/App.pm ↑ Summary blib/lib/Games/ABC_Path/Generator/FinalLayoutObj.pm »
1 package Games::ABC_Path::MicrosoftRand;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Games::ABC_Path::MicrosoftRand - a pseudo-random number generator compatible
10 with Visual C.
11  
12 =head1 VERSION
13  
14 Version 0.1.0
15  
16 =cut
17  
18 7 our $VERSION = '0.1.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Games::ABC_Path::MicrosoftRand;
24  
25 my $randomizer = Games::ABC_Path::MicrosoftRand->new(seed => 24);
26  
27 my $random_digit = $randomizer->rand_max(10);
28  
29 =head1 DESCRIPTION
30  
31 This is a random number generator use by Games::ABC_Path::Generator, which
32 emulates the one found in Microsoft's Visual C++. It was utilised here, out
33 of familiarity and accessibility, because it is commonly used to generate
34 Freecell layouts in the Freecell world (see
35 L<http://en.wikipedia.org/wiki/FreeCell_%28Windows%29> ).
36  
37 =cut
38  
39 7 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 use Class::XSAccessor {
42 7 constructor => 'new',
43 accessors => [qw(seed)],
44 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 sub rand
47 {
48 333 ▶ my $self = shift;
Subroutine rand: called CC=1 SCAR=0.0
Pod documented
49 333 $self->seed(($self->seed() * 214013 + 2531011) & (0x7FFF_FFFF));
50 333 return (($self->seed >> 16) & 0x7fff);
51 }
52  
53 sub max_rand
54 {
55 330 ▶ my ($self, $max) = @_;
Subroutine max_rand: called CC=1 SCAR=0.0
Pod documented
56  
57 330 return ($self->rand() % $max);
58 }
59  
60 sub shuffle
61 {
62 185 ▶ my ($self, $deck) = @_;
Subroutine shuffle: called CC=3 SCAR=11.0
Pod documented
63  
64 185 ▶ if (@$deck)
Branch: 1 branch
BranchTrueFalseTotal
if (@$deck) 181 4 185
65 {
66 181 my $i = @$deck;
67 181 ▶ while (--$i) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 326 181 507
68 326 my $j = $self->max_rand($i+1);
69 326 @$deck[$i,$j] = @$deck[$j,$i];
70 }
71 }
72  
73 185 return $deck;
74 }
75  
76 =head1 SUBROUTINES/METHODS
77  
78 =head2 new
79  
80 The constructor. Accepts a numeric seed as an argument.
81  
82 my $randomizer = Games::ABC_Path::MicrosoftRand->new(seed => 1);
83  
84 =head2 $randomizer->rand()
85  
86 Returns a random integer from 0 up to 0x7fff - 1.
87  
88 my $n = $randomizer->rand()
89  
90 =head2 $randomizer->max_rand($max)
91  
92 Returns a random integer in the range 0 to ($max-1).
93  
94 my $n = $randomizer->max_rand($max);
95 # $n is now between 0 and $max - 1.
96  
97 =head2 $randomizer->seed($seed)
98  
99 Can be used to re-assign the seed of the randomizer (though not recommended).
100  
101 =head2 my $array_ref = $randomizer->shuffle(\@array)
102  
103 Shuffles the array reference of the first argument, B<destroys it> and returns
104 it. This is using the fisher-yates shuffle.
105  
106 =cut
107  
108 =head1 AUTHOR
109  
110 Shlomi Fish, L<http://www.shlomifish.org/> .
111  
112 =head1 BUGS
113  
114 Please report any bugs or feature requests to C<bug-games-abc_path-generator at rt.cpan.org>, or through
115 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-ABC_Path-Generator>. I will be notified, and then you'll
116 automatically be notified of progress on your bug as I make changes.
117  
118  
119  
120  
121 =head1 SUPPORT
122  
123 You can find documentation for this module with the perldoc command.
124  
125 perldoc Games::ABC_Path::MicrosoftRand
126  
127  
128 You can also look for information at:
129  
130 =over 4
131  
132 =item * RT: CPAN's request tracker (report bugs here)
133  
134 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-ABC_Path-Generator>
135  
136 =item * AnnoCPAN: Annotated CPAN documentation
137  
138 L<http://annocpan.org/dist/Games-ABC_Path-Generator>
139  
140 =item * CPAN Ratings
141  
142 L<http://cpanratings.perl.org/d/Games-ABC_Path-Generator>
143  
144 =item * Search CPAN
145  
146 L<http://search.cpan.org/dist/Games-ABC_Path-Generator/>
147  
148 =back
149  
150  
151 =head1 ACKNOWLEDGEMENTS
152  
153  
154 =head1 LICENSE AND COPYRIGHT
155  
156 Copyright 2011 Shlomi Fish.
157  
158 This program is distributed under the MIT (X11) License:
159 L<http://www.opensource.org/licenses/mit-license.php>
160  
161 Permission is hereby granted, free of charge, to any person
162 obtaining a copy of this software and associated documentation
163 files (the "Software"), to deal in the Software without
164 restriction, including without limitation the rights to use,
165 copy, modify, merge, publish, distribute, sublicense, and/or sell
166 copies of the Software, and to permit persons to whom the
167 Software is furnished to do so, subject to the following
168 conditions:
169  
170 The above copyright notice and this permission notice shall be
171 included in all copies or substantial portions of the Software.
172  
173 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
174 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
175 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
176 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
177 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
178 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
179 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
180 OTHER DEALINGS IN THE SOFTWARE.
181  
182  
183 =cut
184  
185 7 1; # End of Games::ABC_Path::MicrosoftRand
« blib/lib/Games/ABC_Path/Generator/App.pm ↑ Summary blib/lib/Games/ABC_Path/Generator/FinalLayoutObj.pm »