++ed by:

1 non-PAUSE user.

David Cantrell


Net::Random - get random data from online sources


    my $rand = Net::Random->new( # use fourmilab.ch's randomness source,
        src => 'fourmilab.ch',   # and return results from 1 to 2000
        min => 1,
        max => 2000
    @numbers = $rand->get(5);    # get 5 numbers
    my $rand = Net::Random->new( # use random.org's randomness source,
        src => 'random.org',     # with no explicit range - so values will
    );                           # be in the default range from 0 to 255

    $number = $rand->get();      # get 1 random number


The two sources of randomness above correspond to http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=1024&fmt=hex and http://random.org/cgi-bin/randbyte?nbytes=1024&format=hex. We always get chunks of 1024 bytes at a time, storing it in a pool which is used up as and when needed. The pool is shared between all objects using the same randomness source. When we run out of randomness we go back to the source for more juicy random goodness.

While we always fetch 1024 bytes, data can be used up one, two, three or four bytes at a time, depending on the range between the minimum and maximum desired values. There may be a noticeable delay while more random data is fetched. Warnings may be emitted in case of network problems.

The maintainers of both randomness sources claim that their data is *truly* random. A some simple tests show that they are certainly more random than the rand() function on this 'ere machine.



The constructor returns a Net::Random object. It takes named parameters, of which one - 'src' - is compulsory, telling the module where to get its random data from. The 'min' and 'max' parameters are optional, and default to 0 and 255 respectively. Both must be integers, and 'max' must be at least min+1. The minimum value of 'min' is 0. The maximum value of 'max' is 2^32-1, the largest value that can be stored in a 32-bit int, or 0xFFFFFFFF.

Currently, the only valid values of 'src' are 'fourmilab.ch' and 'random.org'.


Takes a single optional parameter, which must be a positive integer. This determines how many random numbers are to be returned and, if not specified, defaults to 1.

If it fails to retrieve data, we return undef. Note that fourmilab.ch rations random data and you are only permitted to retrieve a certain amount of randomness in any 24 hour period.


Doesn't handle really BIGNUMs. Patches are welcome to make it use Math::BigInt internally. Note that you'll need to calculate how many random bytes to use per result. I strongly suggest only using BigInts when absolutely necessary, because they are slooooooow.

Tests are a bit lame. Really needs to test the results to make sure they're truly random (to make sure I haven't introduced any bias) and in the right range. The current tests for whether the distributions look sane suck donkey dick.


I welcome feedback about my code, including constructive criticism. And, while this is free software (both free-as-in-beer and free-as-in-speech) I also welcome payment. In particular, your bug reports will get moved to the front of the queue if you buy me something from my wishlist, which can be found at http://www.cantrell.org.uk/david/shopping-list/wishlist.

I do *not* welcome automated bug reports from people who haven't read the README. Yes, CPAN-testers, that means you.


David Cantrell <david@cantrell.org.uk>

Thanks are also due to the maintainers of the randomness sources. See their web sites for details on how to praise them.


Copyright 2003 David Cantrell

This module is free-as-in-speech software, and may be used, distributed, and modified under the same terms as Perl itself.