search.cpan.org is shutting down
Dr. James Freeman > Math-Random-MT-Perl-1.05 > Math::Random::MT::Perl

Math-Random-MT-Perl-1.05.tar.gz

Dependencies

Annotate this POD

# CPAN RT

 Open 0
View/Report Bugs
Module Version: 1.05   Source   Latest Release: Math-Random-MT-Perl-1.15

# NAME

Math::Random::MT::Perl - Pure Perl Mersenne Twister Random Number Generator

# SYNOPSIS

```  use Math::Random::MT::Perl;

\$gen = Math::Random::MT->new(\$seed); # OR...
\$gen = Math::Random::MT->new(@seed);

print \$gen->rand(42);         # random float 0.0 .. 41.99999999 inclusive
\$dice = int(1+\$gen->rand(6)); # random int between 1 and 6
print \$gen->rand() < 0.5 ? "heads" : "tails"

OR

use Math::Random::MT qw(srand rand);

# now srand and rand behave as usual, except with 32 bit precsision not ~15```

# DESCRIPTION

Pure Perl implementation of the Mersenne Twister algorithm as implemented in C/XS in Math::Random::MT. The output is identical to the C/XS version. The Mersenne Twister is a 32 bit pseudorandom number generator developed by Makoto Matsumoto and Takuji Nishimura. The algorithm is characterised by a very uniform distribution but is not cryptographically secure. What this means in real terms is that it is fine for modeling but no good for crypto.

Note: Internally unsigned 32 bit integers are used. The range of possible values for such integers is 0..4294967295 (0..2**32-1). The generator takes a random integer from within this range and multiplies it by (1.0/4294967296.0). As a result the range of possible return values is 0 .. 0.999999999767169. This number is then multiplied by the argument passed to rand (default=1). In other words the maximum return value from rand will always be slightly less than the argument - it will never equal that argument. Only the first 10 digits of the returned float are mathematically significant.

This module implements the same two interfaces found in Math::Random::MT, as described in the synopsis above. It defines the following functions.

## Functions

new(\$seed)

Creates a new generator seeded with an unsigned 32-bit integer.

new(@seed)

Creates a new generator seeded with an array of unsigned 32-bit integers.

rand(\$num)

Can be called via the OO in interface or exported. Behaves exactly like Perl's builtin rand(), returning a number uniformly distributed in [0, \$num) (\$num defaults to 1) except the underlying complexity is 32 bits rather than some small fraction of this.

srand(\$seed)

Behaves just like Perl's builtin srand(). If you use this interface, it is strongly recommended that you call srand() explicitly, rather than relying on rand() to call it the first time it is used. Has no effect if called via OO interface - pass the seed(s) to new.

## Export

Nothing by default. rand() and srand() on demand.

# SPEED

Runs around 1/3 as fast as the C code of Math::Random::MT, however that still means a random number generation speed of 100,000/sec on modest hardware.

Math::Random::MT

http://www.math.keio.ac.jp/~matumoto/emt.html