Math::Random::Xorshift - a fast PRNG
my @seeds = (123_456_789, 362_436_069, 521_288_629, 88_675_123);
# OO interface
my $rng = Math::Random::Xorshift->new(@seeds);
my $n = $rng->rand;
my $i = $rng->irand;
# Functional interface
use Math::Random::Xorshift qw/srand irand rand/;
$n = rand(10);
$i = irand;
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.
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.
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.
Returns unsigned random integer in range of [0, UINT32_MAX).
Returns random real number in range of [0, $upper_limit). $upper_limit should be positive.
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% --
Koichi SATOH, <firstname.lastname@example.org>
The MIT License
Copyright (C) 2010 by Koichi SATOH
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
To install Math::Random::Xorshift, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell
For more information on module installation, please visit the detailed CPAN module installation guide.