3
Math::RandomOrg - Retrieve random numbers and data from random.org.
7
use Math::RandomOrg qw(randnum randbyte);
8
my $number = randnum(0, 10);
9
my $octet = randbyte(1);
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.
31
None by default. You may request the following symbols be exported:
43
package Math::RandomOrg;
46
use vars qw(@ISA @EXPORT_OK @EXPORT $VERSION);
52
@EXPORT_OK = qw( checkbuf randnum randbyte randseq );
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
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.
86
my $url = "http://www.random.org/cgi-bin/checkbuf";
87
my $data = LWP::Simple::get( $url );
92
carp "HTTP GET failed for $url";
97
=item C<randnum ( $min, $max )>
99
Return an integer (specifically a Math::BigInt object) between the bounds [ $min, $max ] (inclusive).
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.
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)!";
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));
120
carp "HTTP GET failed for $url";
124
my $num = shift(@randnums);
127
$num *= (1 + $max - $min);
128
$num /= ($RAND_MAX - $RAND_MIN);
136
=item C<randbyte ( $length )>
138
Returns an octet-string of specified length (defaults to one byte), which contains random bytes.
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.
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).";
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)) {
158
carp "HTTP GET failed for $url";
162
return substr($randbytes, 0, $length, '');
166
=item C<randseq ( $min, $max )>
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.
170
Example: C<randseq(1, 10)> will return the numbers between 1 and 10 (both inclusive) in a random order.
175
my ($min, $max) = @_;
176
return if ( (! defined $min) || (! defined $max) || ($min !~ /^\-?\d+$/) || ($max !~ /^\-?\d+$/) );
178
carp "MAX must be greater than MIN.";
181
if ($max - $min > 10000) {
182
carp "random.org restricts the size of sequences to <= 10,000.";
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));
191
carp "HTTP GET failed for $url";
195
return wantarray ? @sequence : \@sequence;
209
Gregory Williams <gwilliams@cpan.org>
215
=item * L<Math::TrulyRandom>
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.