~ubuntu-branches/ubuntu/trusty/libmath-randomorg-perl/trusty

« back to all changes in this revision

Viewing changes to lib/Math/RandomOrg.pm

  • Committer: Bazaar Package Importer
  • Author(s): David Moreno Garza
  • Date: 2006-05-30 11:33:40 UTC
  • Revision ID: james.westby@ubuntu.com-20060530113340-0p5j3h3msn4z3euk
Tags: upstream-0.03
ImportĀ upstreamĀ versionĀ 0.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=head1 NAME
 
2
 
 
3
Math::RandomOrg - Retrieve random numbers and data from random.org.
 
4
 
 
5
=head1 SYNOPSIS
 
6
 
 
7
  use Math::RandomOrg qw(randnum randbyte);
 
8
  my $number = randnum(0, 10);
 
9
  my $octet = randbyte(1);
 
10
 
 
11
=head1 DESCRIPTION
 
12
 
 
13
Math::RandomOrg provides functions for retrieving random data from the random.org server. Data may be retrieved in an integer or byte-stream format using the C<randnum> and C<randbyte> functions respectively.
 
14
 
 
15
=head1 REQUIRES
 
16
 
 
17
=over 4
 
18
 
 
19
=item Carp
 
20
 
 
21
=item Exporter
 
22
 
 
23
=item Math::BigInt
 
24
 
 
25
=item LWP::Simple
 
26
 
 
27
=back
 
28
 
 
29
=head1 EXPORT
 
30
 
 
31
None by default. You may request the following symbols be exported:
 
32
 
 
33
=over 4
 
34
 
 
35
=item * randnum
 
36
 
 
37
=item * randbyte
 
38
 
 
39
=back
 
40
 
 
41
=cut
 
42
 
 
43
package Math::RandomOrg;
 
44
 
 
45
use strict;
 
46
use vars qw(@ISA @EXPORT_OK @EXPORT $VERSION);
 
47
 
 
48
require Exporter;
 
49
 
 
50
@ISA = qw(Exporter);
 
51
 
 
52
@EXPORT_OK = qw( checkbuf randnum randbyte randseq );
 
53
@EXPORT = qw();
 
54
$VERSION = '0.03';
 
55
 
 
56
use Carp;
 
57
use Math::BigInt;
 
58
use LWP::Simple ();
 
59
 
 
60
my $RAND_MIN    = new Math::BigInt "-1000000000";       # random.org fixed min
 
61
my $RAND_MAX    = new Math::BigInt "1000000000";        # random.org fixed max
 
62
my $NUM_BUF     = 256;                                                                  # at least, request this number of random integers in each request to random.org
 
63
 
 
64
=head1 FUNCTIONS
 
65
 
 
66
=over 4
 
67
 
 
68
=cut
 
69
 
 
70
{
 
71
        my @randnums;
 
72
 
 
73
=item C<checkbuf()>
 
74
 
 
75
This routine takes no parameters and simply returns a single value (e.g., 
 
76
C<28%>) telling you how full the buffer is. At 100%, the buffer is full 
 
77
and you are free to hit it with automated clients. At 0%, the buffer is 
 
78
empty and requests will hang. When less than 100%, the buffer is being 
 
79
filled continually, but doing so takes time. I advise people with 
 
80
automated clients to check the buffer level every once in a while and only 
 
81
issue requests when the buffer level is 20% or higher.
 
82
 
 
83
=cut
 
84
 
 
85
        sub checkbuf {
 
86
                my $url         = "http://www.random.org/cgi-bin/checkbuf";
 
87
                my $data        = LWP::Simple::get( $url );
 
88
                if (defined($data)) {
 
89
                        $data =~ s/\%//;
 
90
                        return $data;
 
91
                } else {
 
92
                        carp "HTTP GET failed for $url";
 
93
                        return;
 
94
                }
 
95
        }
 
96
 
 
97
=item C<randnum ( $min, $max )>
 
98
 
 
99
Return an integer (specifically a Math::BigInt object) between the bounds [ $min, $max ] (inclusive).
 
100
 
 
101
By default, $max and $min are positive and negative 1e9, respectively. These default
 
102
values represent random.org's current extrema for the bounds of the randnum function.
 
103
Therefore, $min and $max may not exceed the default values.
 
104
 
 
105
=cut
 
106
        sub randnum (;$$) {
 
107
                use integer;
 
108
                my $min = new Math::BigInt (defined($_[0]) ? $_[0] : $RAND_MIN);
 
109
                my $max = new Math::BigInt (defined($_[1]) ? $_[1] : $RAND_MAX);
 
110
                if ($min < $RAND_MIN or $max > $RAND_MAX) {
 
111
                        carp "The $min and $max arguments to the randnum() function may not exceed the bounds ($RAND_MIN, $RAND_MAX)!";
 
112
                        return undef;
 
113
                }
 
114
                if ($#randnums == -1) {
 
115
                        my $url         = "http://www.random.org/cgi-bin/randnum?num=${NUM_BUF}&min=${RAND_MIN}&max=${RAND_MAX}&col=1";
 
116
                        my $data        = LWP::Simple::get( $url );
 
117
                        if (defined($data)) {
 
118
                                @randnums       = map { new Math::BigInt $_ } (split(/\n/, $data));
 
119
                        } else {
 
120
                                carp "HTTP GET failed for $url";
 
121
                                return undef;
 
122
                        }
 
123
                }
 
124
                my $num = shift(@randnums);
 
125
                
 
126
                $num    -= $RAND_MIN;
 
127
                $num    *= (1 + $max - $min);
 
128
                $num    /= ($RAND_MAX - $RAND_MIN);
 
129
                $num    += $min;
 
130
                
 
131
                return $num;
 
132
        }
 
133
 
 
134
        my $randbytes   = '';
 
135
 
 
136
=item C<randbyte ( $length )>
 
137
 
 
138
Returns an octet-string of specified length (defaults to one byte), which contains random bytes.
 
139
 
 
140
$length may not exceed 16,384, as this is the maximum number of bytes retrievable from the
 
141
random.org server in one request, and making multiple requests for an unbounded amount of
 
142
data would unfairly tax the random.org server. If you need large amounts of random data,
 
143
you may wish to try the Math::TrulyRandom module.
 
144
 
 
145
=cut
 
146
        sub randbyte (;$) {
 
147
                my $length      = +(shift || 1);
 
148
                if ($length > 16_384) {
 
149
                        carp "randbyte() should not be used to generate random data larger than 16,384 bytes (lest we swamp random.org's entropy source).";
 
150
                        return '';
 
151
                } elsif (length($randbytes) < $length) {
 
152
                        my $nbytes      = ($length > 512) ? $length : 512;
 
153
                        my $url         = "http://www.random.org/cgi-bin/randbyte?nbytes=${nbytes}&format=f";
 
154
                        my $data        = LWP::Simple::get( $url );
 
155
                        if (defined($data)) {
 
156
                                $randbytes      .= $data;
 
157
                        } else {
 
158
                                carp "HTTP GET failed for $url";
 
159
                                return undef;
 
160
                        }
 
161
                }
 
162
                return substr($randbytes, 0, $length, '');
 
163
        }
 
164
}
 
165
 
 
166
=item C<randseq ( $min, $max )>
 
167
 
 
168
The randseq script returns a randomized sequence of numbers. This corresponds to dropping a number of lottery tickets into a hat and drawing them out in random order. Hence, each number in a randomized sequence occurs exactly once.
 
169
 
 
170
Example: C<randseq(1, 10)> will return the numbers between 1 and 10 (both inclusive) in a random order.
 
171
 
 
172
=cut
 
173
 
 
174
        sub randseq (;$$) {
 
175
                my ($min, $max) = @_;
 
176
                return if ( (! defined $min) || (! defined $max) || ($min !~ /^\-?\d+$/) || ($max !~ /^\-?\d+$/) );
 
177
                if ($max < $min) {
 
178
                        carp "MAX must be greater than MIN.";
 
179
                        return;
 
180
                }
 
181
                if ($max - $min > 10000) {
 
182
                        carp "random.org restricts the size of sequences to <= 10,000.";
 
183
                        return;
 
184
                }
 
185
                my @sequence = ();
 
186
                my $url         = "http://www.random.org/cgi-bin/randseq?min=$min&max=$max";
 
187
                my $data        = LWP::Simple::get( $url );
 
188
                if (defined($data)) {
 
189
                        @sequence = map { new Math::BigInt $_ } (split(/\n/, $data));
 
190
                } else {
 
191
                        carp "HTTP GET failed for $url";
 
192
                        return undef;
 
193
                }
 
194
                
 
195
                return wantarray ? @sequence : \@sequence;
 
196
        }
 
197
 
 
198
1;
 
199
__END__
 
200
 
 
201
=back
 
202
 
 
203
=head1 BUGS
 
204
 
 
205
None known.
 
206
 
 
207
=head1 AUTHOR
 
208
 
 
209
Gregory Williams <gwilliams@cpan.org>
 
210
 
 
211
=head1 SEE ALSO
 
212
 
 
213
=over 4
 
214
 
 
215
=item * L<Math::TrulyRandom>
 
216
 
 
217
=item * L<rand>
 
218
 
 
219
=back
 
220
 
 
221
=head1 COPYRIGHT
 
222
 
 
223
Copyright (c) 2001--2006, Gregory Williams. All rights reserved.
 
224
This module is free software. It may be used, redistributed
 
225
and/or modified under the same terms as Perl itself.
 
226
 
 
227
=cut
 
228