1MT(3) User Contributed Perl Documentation MT(3)
2
3
4
6 Math::Random::MT - The Mersenne Twister PRNG
7
9 ## Object-oriented interface:
10 use Math::Random::MT;
11 $gen = Math::Random::MT->new() # or...
12 $gen = Math::Random::MT->new($seed); # or...
13 $gen = Math::Random::MT->new(@seeds);
14 $seed = $gen->get_seed(); # seed used to generate the random numbers
15 $rand = $gen->rand(42); # random number in the interval [0, 42)
16 $dice = int($gen->rand(6)+1); # random integer between 1 and 6
17 $coin = $gen->rand() < 0.5 ? # flip a coin
18 "heads" : "tails"
19 $int = $gen->irand(); # random integer in [0, 2^32-1]
20
21 ## Function-oriented interface:
22 use Math::Random::MT qw(srand rand irand);
23 # now use srand() and rand() as you usually do in Perl
24
26 The Mersenne Twister is a pseudorandom number generator developed by
27 Makoto Matsumoto and Takuji Nishimura. It is described in their paper
28 at <URL:http://www.math.keio.ac.jp/~nisimura/random/doc/mt.ps>. This
29 algorithm has a very uniform distribution and is good for modelling
30 purposes but do not use it for cryptography.
31
32 This module implements two interfaces:
33
34 Object-oriented interface
35 new()
36 Creates a new generator that is automatically seeded based on
37 gettimeofday.
38
39 new($seed)
40 Creates a new generator seeded with an unsigned 32-bit integer.
41
42 new(@seeds)
43 Creates a new generator seeded with an array of (up to 624)
44 unsigned 32-bit integers.
45
46 set_seed()
47 Seeds the generator and returns the seed used. It takes the same
48 arguments as new().
49
50 get_seed()
51 Retrieves the value of the seed used.
52
53 rand($num)
54 Behaves exactly like Perl's builtin rand(), returning a number
55 uniformly distributed in [0, $num) ($num defaults to 1).
56
57 irand()
58 Returns a 32-bit integer, i.e. an integer uniformly distributed in
59 [0, 2^32-1].
60
61 Function-oriented interface
62 srand($seed)
63 Behaves just like Perl's builtin srand(). As in Perl >= 5.14, the
64 seed is returned. If you use this interface, it is strongly
65 recommended that you call srand() explicitly, rather than relying
66 on rand() to call it the first time it is used.
67
68 rand($num)
69 Behaves exactly like Perl's builtin rand(), returning a number
70 uniformly distributed in [0, $num) ($num defaults to 1).
71
72 irand()
73 Returns a 32-bit integer, i.e. an integer uniformly distributed in
74 [0, 2^32-1].
75
77 <URL:http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>
78
79 Data::Entropy
80
82 Sean M. Burke
83 For giving me the idea to write this module.
84
85 Philip Newton
86 For several useful patches.
87
88 Florent Angly
89 For implementing seed generation and retrieval.
90
92 Abhijit Menon-Sen <ams@toroid.org>
93
94 Copyright 2001 Abhijit Menon-Sen. All rights reserved.
95
96 Based on the C implementation of MT19937 Copyright (C) 1997 - 2002,
97 Makoto Matsumoto and Takuji Nishimura
98
99 This software is distributed under a (three-clause) BSD-style license.
100 See the LICENSE file in the distribution for details.
101
102
103
104perl v5.38.0 2023-07-20 MT(3)