Dr. James Freeman
and 1 contributors


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


  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"


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

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


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.



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


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


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.


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.


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


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.





(c) Dr James Freeman 2000-08 <airmedical [at] gmail [dot] com> All rights reserved.


almut from perlmonks for 64 bit debug and fix.

Abhijit Menon-Sen, Philip Newton and Sean M. Burke who contributed to Math::Random::MT as this module is simply a translation.


This package is free software and is provided "as is" without express or implied warranty. It may be used, redistributed and/or modified under the terms of the Artistic License 2.0. A copy is include in this distribution.