MT(3pm)

πŸ’– MT β€” Math::Random::MT πŸ’–

okay bestie, so this is the manual page for Math::Random::MT β€” aka the Mersenne Twister PRNG, living its best life in the User Contributed Perl Documentation section of your man pages. pull up a chair, we're gonna get you fluent in random numbers πŸ’…


✨ NAME ✨

Math::Random::MT β€” the Mersenne Twister PRNG. no cap, that's the whole vibe. πŸŽ€


🌸 SYNOPSIS 🌸

two whole interfaces, pick your fighter:

## Object-oriented interface:
use Math::Random::MT;
$gen = Math::Random::MT->new()        # or...
$gen = Math::Random::MT->new($seed);  # or...
$gen = Math::Random::MT->new(@seeds);
$seed = $gen->get_seed();             # seed used to generate the random numbers
$rand = $gen->rand(42);               # random number in the interval [0, 42)
$dice = int($gen->rand(6)+1);         # random integer between 1 and 6
$coin = $gen->rand() < 0.5 ?          # flip a coin
  "heads" : "tails"
$int = $gen->irand();                 # random integer in [0, 2^32-1]

## Function-oriented interface:
use Math::Random::MT qw(srand rand irand);
# now use srand() and rand() as you usually do in Perl

like… look at that. she does it all 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

so the Mersenne Twister is a pseudorandom number generator, and it was developed by Makoto Matsumoto and Takuji Nishimura. it's described in their paper at <URL:http://www.math.keio.ac.jp/~nisimura/random/doc/mt.ps>. this algorithm has a very uniform distribution and it's genuinely great for modelling purposes β€” but listen to me, bestie, do not use it for cryptography. i said what i said. πŸ”₯

this module implements two interfaces:

πŸ’œ Object-oriented interface

  • new() β€” creates a new generator that is automatically seeded based on gettimeofday. cute, effortless, self-seeding ✨
  • new($seed) β€” creates a new generator seeded with an unsigned 32-bit integer.
  • new(@seeds) β€” creates a new generator seeded with an array of (up to 624) unsigned 32-bit integers. yes, 624. iconic number, don't question it.
  • set_seed() β€” seeds the generator and returns the seed used. it takes the same arguments as new(). very "give me something, i'll give it right back" of it πŸ’…
  • get_seed() β€” retrieves the value of the seed used. so you can peek. we love an honest queen πŸ‘‘
  • rand($num) β€” behaves exactly like Perl's builtin rand(), returning a number uniformly distributed in [0, $num) ($num defaults to 1).
  • irand() β€” returns a 32-bit integer, i.e. an integer uniformly distributed in [0, 2^32-1].

🌈 Function-oriented interface

  • srand($seed) β€” behaves just like Perl's builtin srand(). as in Perl >= 5.14, the seed is returned. and 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. do it on purpose, bestie, don't make rand() do the emotional labour for you 😭
  • rand($num) β€” behaves exactly like Perl's builtin rand(), returning a number uniformly distributed in [0, $num) ($num defaults to 1).
  • irand() β€” returns a 32-bit integer, i.e. an integer uniformly distributed in [0, 2^32-1].

πŸ‘€ SEE ALSO πŸ‘€

go read them, they're giving supporting-docs energy πŸ“


🫢 ACKNOWLEDGEMENTS 🫢

  • Sean M. Burke β€” for giving me the idea to write this module.
  • Philip Newton β€” for several useful patches.
  • Florent Angly β€” for implementing seed generation and retrieval.

the founding mothers and fathers of this module, fr fr 🌸


πŸŽ€ AUTHOR πŸŽ€

Abhijit Menon-Sen <ams@toroid.org>

Copyright 2001 Abhijit Menon-Sen. All rights reserved.

Based on the C implementation of MT19937 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura

This software is distributed under a (three-clause) BSD-style license. See the LICENSE file in the distribution for details.

πŸ’€ real talk: it's a (three-clause) BSD-style license, so go read the actual LICENSE file in the distribution for the details. i'm just the messenger 🧚

and that's the whole page, bestie! this was perl v5.42.3, dated 2026-08-19, and it's the MT(3pm) man page from User Contributed Perl Documentation. go forth and be random πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from