Math::Random::MT#

📦 std

The Mersenne Twister pseudo-random number generator, MT19937: a long period (2**19937 - 1), fast, and the same sequence on every platform for the same seed. It is not for cryptography.

Math::Random::MT->new(@seeds) makes a generator; one seed is a 32-bit integer, several are an array of them, and none takes a seed from perl’s own rand. $gen->rand($n) returns a number in [0, $n) ($n defaults to 1), $gen->irand an integer in [0, 2**32), and $gen->get_seed the seed. Imported, srand, rand and irand work on one generator of the module’s own, as perl’s built-ins do.

This is a native implementation of Math::Random::MT 1.17’s MT.xs and _mt.c (Matsumoto and Nishimura’s mt19937ar.c); its Math/Random/MT.pm is used unchanged.

Synopsis#

use Math::Random::MT;

my $gen = Math::Random::MT->new(5489);
print $gen->irand, "\n";        # 3499211612, mt19937ar's first output
my $die = 1 + int $gen->rand(6);

use Math::Random::MT qw(srand rand);
srand(42);
print rand(10), "\n";

Functions#

Generators#

init#

Math::Random::MT::init(): a new generator object, not yet seeded; new seeds it.

genrand#

$gen->genrand: the next number in [0, 1).

genirand#

$gen->genirand: the next integer in [0, 2**32).

Seeding#

init_seed#

$gen->init_seed($seed): seed with one 32-bit integer.

setup_array#

$gen->setup_array(@seeds): seed with an array of 32-bit integers.

get_seed#

$gen->get_seed: the seed it was given with init_seed, or 19650218 after setup_array.