Koichi SATOH
and 1 contributors

# NAME

Math::Random::Xorshift - a fast PRNG

# SYNOPSIS

`````` my @seeds = (123_456_789, 362_436_069, 521_288_629, 88_675_123);

# OO interface
use Math::Random::Xorshift;
my \$rng = Math::Random::Xorshift->new(@seeds);
\$rng->srand(@seeds);
my \$n = \$rng->rand;
my \$i = \$rng->irand;

# Functional interface
use Math::Random::Xorshift qw/srand irand rand/;
srand(@seeds);
\$n = rand(10);
\$i = irand;``````

# DESCRIPTION

This module is a straight forward implementation of Xorshift PRNG proposed by G. Marsaglia in 2003.

Note that the algorithm is extremely fast and passes the Diehard test though, is not reliable enough statistically (see "SEE ALSO" section). I think however this module is useful for games and suchlike usages.

If you want rather more reliability than fastness, I recommend Math::Random::MT.

## EXPORT

None by default. You can import `srand`, `rand` and `irand` to replace Perl's builtins. These functions manupilate static PRNG object in C level. So these are about 3-4x faster than OO interface, since there's no method resolution overhead.

# METHOD

## new([\$seed | @seeds])

Constructor. You can specify up to 4 seed(s). At least 1 seed value must not be zero. If no seeds are given, return value of `time(3)` is used.

Resets seeds.

## irand()

Returns unsigned random integer in range of [0, UINT32_MAX).

## rand([\$upper_limit = 1.0])

Returns random real number in range of [0, \$upper_limit). `\$upper_limit` should be positive.

# BENCHMARK

Here is a benchmark result on my Macbook(Core 2 Duo 2.4 GHz/4GB DDR3 RAM). Competitors are Math::Random::MT, Math::Random::ISAAC, and Perl's builtin `rand()`.

You can run bench.pl included in this distribution to benchmark on your machine.

``````                            Rate M::R::ISAAC#irand M::R::MT#rand M::R::ISAAC#rand M::R::Xorshift#rand M::R::Xorshift#irand M::R::Xorshift::irand M::R::Xorshift::rand CORE::rand
M::R::ISAAC#irand       910221/s                --           -2%              -3%                -64%                 -66%                  -89%                 -90%       -96%
M::R::MT#rand           927943/s                2%            --              -1%                -63%                 -66%                  -89%                 -90%       -96%
M::R::ISAAC#rand        936228/s                3%            1%               --                -63%                 -65%                  -89%                 -90%       -96%
M::R::Xorshift#rand    2502283/s              175%          170%             167%                  --                  -8%                  -71%                 -73%       -88%
M::R::Xorshift#irand   2706501/s              197%          192%             189%                  8%                   --                  -68%                 -71%       -87%
M::R::Xorshift::irand  8485586/s              832%          814%             806%                239%                 214%                    --                  -8%       -59%
M::R::Xorshift::rand   9175040/s              908%          889%             880%                267%                 239%                    8%                   --       -56%
CORE::rand            20852364/s             2191%         2147%            2127%                733%                 670%                  146%                 127%         --``````

Math::Random::MT - The Mersenne Twister PRNG
G. Marsaglia, 2003, "Xorshift PRNGs" - Original paper
F. Panneton and P. L'ecuyer, 2005, "On the xorshift random number generators" - According to this paper, Xorshift is not reliable

# AUTHOR

Koichi SATOH, <r.sekia@gmail.com>