~ubuntu-branches/ubuntu/saucy/freecell-solver/saucy

« back to all changes in this revision

Viewing changes to t/t/lib/Games/ABC_Path/MicrosoftRand.pm

  • Committer: Package Import Robot
  • Author(s): Gergely Risko
  • Date: 2012-06-22 10:08:05 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120622100805-evoda1ccdr8vt5xr
Tags: 3.12.0-1
New upstream version. (closes: #675262)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Games::ABC_Path::MicrosoftRand;
 
2
 
 
3
use 5.006;
 
4
use strict;
 
5
use warnings;
 
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
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
use integer;
 
40
 
 
41
use Class::XSAccessor {
 
42
    constructor => 'new',
 
43
    accessors => [qw(seed)],
 
44
};
 
45
 
 
46
sub rand
 
47
{
 
48
    my $self = shift;
 
49
    $self->seed(($self->seed() * 214013 + 2531011) & (0x7FFF_FFFF));
 
50
    return (($self->seed >> 16) & 0x7fff);
 
51
}
 
52
 
 
53
sub max_rand
 
54
{
 
55
    my ($self, $max) = @_;
 
56
 
 
57
    return ($self->rand() % $max);
 
58
}
 
59
 
 
60
sub shuffle
 
61
{
 
62
    my ($self, $deck) = @_;
 
63
 
 
64
    if (@$deck)
 
65
    {
 
66
        my $i = @$deck;
 
67
        while (--$i) {
 
68
            my $j = $self->max_rand($i+1);
 
69
            @$deck[$i,$j] = @$deck[$j,$i];
 
70
        }
 
71
    }
 
72
 
 
73
    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
1; # End of Games::ABC_Path::MicrosoftRand