emacs.d/clones/lisp/www.cliki.net/MT19937.html
2022-10-07 15:47:14 +02:00

92 lines
No EOL
6.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CLiki: MT19937</title>
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
href="https://www.cliki.net/site/feed/article.atom?title=MT19937">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/css/colorize.css">
</head>
<body>
<span class="hidden">CLiki - MT19937</span>
<div id="content"><div id="content-area"><div id="article-title">MT19937</div><div id="article">MT19937 is a portable <a href="Mersenne&#32;Twister.html" class="category">Mersenne Twister</a> random number generator. It
is mainly a modification of <a href="CMUCL.html" class="internal">CMUCL</a>'s random number generator with all
the CMUCL-specific parts taken out.<p>It is faster than the <a href="https://www.cliki.net/JMT" class="new">JMT</a> Mersenne Twister
implementation, but significantly slower than native random number
generators provided by major Common Lisp implementations. For light
use this shouldn't be a problem, since it is still very fast.<p>It should be very stable, since it's based on stable CMUCL code. It
has been tested on <a href="CMUCL.html" class="internal">CMUCL</a>, <a href="SBCL.html" class="internal">SBCL</a>, (LispWorks), <a href="Allegro.html" class="internal">Allegro</a> CL, <a href="GCL.html" class="internal">GCL</a>, <a href="CLISP.html" class="internal">CLISP</a>, and <a href="https://www.cliki.net/Corman%20Lisp" class="new">Corman Lisp</a>. MT19937 is in the public domain.<p><h2>What's the point?</h2>
Why, you might ask, would you want to use a portable, slower random
number generator? The answer is consistency. The portable version of
this code was originally created for <a href="Maxima.html" class="internal">Maxima</a>, a computer algebra
system. They wanted the results of the random number generator to be
portable across several implementations and platforms, so that if you
used a certain seed on CMUCL the numbers generated would be the same
as you would get with the same seed on GCL or CLISP. This was more
important than achieving the maximum possible speed. You may have
similar problems.<p><h2>Usage</h2>
MT19937 is a plug-in replacement for the Common Lisp random-number
generator. The MT19937 package exports all the Common Lisp symbols
related to random number generation, so you just need to load MT19937
and call its functions instead of the built-in ones. For example:<p><pre>
;; Load MT19937
(asdf:oos &#039;asdf:load-op :mt19937)
;; Make random numbers with your implementation&#039;s random number generator.
(random 1234567)
(random 42.56)
(random 3.1415d0)
;; Make random numbers using MT19937
(mt19937:random 1234567)
(mt19937:random 42.56)
(mt19937:random 3.1415d0)
;; MT19937 has its own random state
(eq *random-state*
mt19937:*random-state*) =&gt; nil
</pre><p><h2>Download</h2><p>MT19937 can be downloaded from the <a href="http://common-lisp.net/project/asdf-packaging/">asdf-packaging home page</a>.<p><h2>Bug</h2>
This code has a bug in the definition of the "random" compiler macro at the bottom of the file. Because it is a macro the default value for the state argument needs a quote in front of it, so that the macro argument gets bound to a symbol, not the value of a symbol. <p>Without this fix, code calling mt19937:random with a constant argument probably won't compile at all (it would not on ACL 8.0) for lack of a make-load-form method for random-state's. If one is added so that it compiles, it will behave differently than expected: calls using constant arguments with no state argument will fail to use the dynamic value of *random-state* at the time of the call. <p>The macro definition should read
<pre>
(define-compiler-macro random (&amp;whole form arg &amp;optional (state &#039;*random-state*)
</pre>
with single quote added in front of *random-state*.<p>Or it might be safest to delete this macro entirely.<p>Another weakness is that it is very unclear how to contact a maintainer of the code to correct a bug like this, short of modifying this page!<p>Confirmed on SBCL 1.0.29. I believe the asdf-packaging mailing list is probably the best place to report the bug. I have asked on the list but have not yet received a response. -- Elliott Slaughter 2010-01-05<p>Update: As of 2011-01-30, this bug has finally been fixed. -- Elliott Slaughter</div></div>
<div id="footer" class="buttonbar"><ul><li><a href="MT19937.html">Current version</a></li>
<li><a href="https://www.cliki.net/site/history?article=MT19937">History</a></li>
<li><a href="https://www.cliki.net/site/backlinks?article=MT19937">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=MT19937&amp;from-revision=3686398038">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
</div>
<div id="header-buttons" class="buttonbar">
<ul>
<li><a href="https://www.cliki.net/">Home</a></li>
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
<li><a href="CLiki.html">About</a></li>
<li><a href="Text&#32;Formatting.html">Text Formatting</a></li>
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
</ul>
<div id="search">
<form action="https://www.cliki.net/site/search">
<label for="search_query" class="hidden">Search CLiki</label>
<input type="text" name="query" id="search_query" value="" />
<input type="submit" value="search" />
</form>
</div>
</div>
<div id="pageheader">
<div id="header">
<span id="logo">CLiki</span>
<span id="slogan">the common lisp wiki</span>
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
<label for="login_name" class="hidden">Account name</label>
<input type="text" name="name" id="login_name" class="login_input" />
<label for= "login_password" class="hidden">Password</label>
<input type="password" name="password" id="login_password" class="login_input" />
<input type="submit" name="login" value="login" id="login_submit" /><br />
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
</form>
</div>
</div>
</div>
</body></html>