The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Perl.pm</title>
<link rel="stylesheet" href="./html/docs.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>
<table border="0" width="100%" cellspacing="0" cellpadding="3">
<tr><td class="block" valign="middle">
<big><strong><span class="block">&nbsp;Perl.pm</span></strong></big>
</td></tr>
</table>


<!-- INDEX BEGIN -->
<div name="index">
<p><a name="__index__"></a></p>

<ul>

    <li><a href="#name">NAME</a></li>
    <li><a href="#synopsis">SYNOPSIS</a></li>
    <li><a href="#description">DESCRIPTION</a></li>
    <ul>

        <li><a href="#functions">Functions</a></li>
        <li><a href="#export">Export</a></li>
    </ul>

    <li><a href="#speed">SPEED</a></li>
    <li><a href="#see_also">SEE ALSO</a></li>
    <li><a href="#author">AUTHOR</a></li>
    <ul>

        <li><a href="#credits">Credits</a></li>
    </ul>

    <li><a href="#license">LICENSE</a></li>
</ul>

<hr name="index" />
</div>
<!-- INDEX END -->

<p>
</p>
<hr />
<h1><a name="name">NAME</a></h1>
<p>Math::Random::MT::Perl - Pure Perl Mersenne Twister Random Number Generator</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
  <span class="keyword">use</span> <span class="variable">Math::Random::MT::Perl</span><span class="operator">;</span>
</pre>
<pre>
  <span class="variable">$gen</span> <span class="operator">=</span> <span class="variable">Math::Random::MT</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="variable">$seed</span><span class="operator">);</span> <span class="comment"># OR...</span>
  <span class="variable">$gen</span> <span class="operator">=</span> <span class="variable">Math::Random::MT</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="variable">@seed</span><span class="operator">);</span>
</pre>
<pre>
  <span class="keyword">print</span> <span class="variable">$gen</span><span class="operator">-&gt;</span><span class="keyword">rand</span><span class="operator">(</span><span class="number">42</span><span class="operator">);</span>         <span class="comment"># random float 0.0 .. 41.99999999 inclusive</span>
  <span class="variable">$dice</span> <span class="operator">=</span> <span class="keyword">int</span><span class="operator">(</span><span class="number">1</span><span class="operator">+</span><span class="variable">$gen</span><span class="operator">-&gt;</span><span class="keyword">rand</span><span class="operator">(</span><span class="number">6</span><span class="operator">));</span> <span class="comment"># random int between 1 and 6</span>
  <span class="keyword">print</span> <span class="variable">$gen</span><span class="operator">-&gt;</span><span class="keyword">rand</span><span class="operator">()</span> <span class="operator">&lt;</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span> <span class="operator">?</span> <span class="string">"heads"</span> <span class="operator">:</span> <span class="string">"tails"</span>
</pre>
<pre>
  OR</pre>
<pre>
  <span class="keyword">use</span> <span class="variable">Math::Random::MT</span> <span class="string">qw(srand rand)</span><span class="operator">;</span>
</pre>
<pre>
  <span class="comment"># now srand and rand behave as usual, except with 32 bit precsision not ~15</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>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.</p>
<p>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.</p>
<p>This module implements the same two interfaces found in Math::Random::MT,
as described in the synopsis above. It defines the following functions.</p>
<p>
</p>
<h2><a name="functions">Functions</a></h2>
<dl>
<dt><strong><a name="new" class="item"><code>new($seed)</code></a></strong>

<dd>
<p>Creates a new generator seeded with an unsigned 32-bit integer.</p>
</dd>
</li>
<dt><strong><code>new(@seed)</code></strong>

<dd>
<p>Creates a new generator seeded with an array of unsigned 32-bit integers.</p>
</dd>
</li>
<dt><strong><a name="rand" class="item"><code>rand($num)</code></a></strong>

<dd>
<p>Can be called via the OO in interface or exported. Behaves exactly like
Perl's builtin <a href="#item_rand"><code>rand()</code></a>, 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.</p>
</dd>
</li>
<dt><strong><a name="srand" class="item"><code>srand($seed)</code></a></strong>

<dd>
<p>Behaves just like Perl's builtin <a href="#item_srand"><code>srand()</code></a>. If you use this interface, it
is strongly recommended that you call <em>srand()</em> explicitly, rather than
relying on <em>rand()</em> to call it the first time it is used. Has no effect if
called via OO interface - pass the seed(s) to new.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="export">Export</a></h2>
<p>Nothing by default. <a href="#item_rand"><code>rand()</code></a> and <a href="#item_srand"><code>srand()</code></a> on demand.</p>
<p>
</p>
<hr />
<h1><a name="speed">SPEED</a></h1>
<p>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.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>Math::Random::MT</p>
<p><a href="http://www.math.keio.ac.jp/~matumoto/emt.html">http://www.math.keio.ac.jp/~matumoto/emt.html</a></p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>(c) Dr James Freeman 2000-08 &lt;airmedical [at] gmail [dot] com&gt;
All rights reserved.</p>
<p>
</p>
<h2><a name="credits">Credits</a></h2>
<p>almut from perlmonks for 64 bit debug and fix.</p>
<p>Abhijit Menon-Sen, Philip Newton and Sean M. Burke who contributed to
Math::Random::MT as this module is simply a translation.</p>
<p>
</p>
<hr />
<h1><a name="license">LICENSE</a></h1>
<p>This package is free software and is provided &quot;as is&quot; 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.</p>
<table border="0" width="100%" cellspacing="0" cellpadding="3">
<tr><td class="block" valign="middle">
<big><strong><span class="block">&nbsp;Perl.pm</span></strong></big>
</td></tr>
</table>

</body>

</html>