📄 blitz_9.html
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on October, 14 2005 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Blitz++: Random Number Generators</TITLE><META NAME="description" CONTENT="Blitz++: Random Number Generators"><META NAME="keywords" CONTENT="Blitz++: Random Number Generators"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"></HEAD><BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"><A NAME="SEC142"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_8.html#SEC141"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC143"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H1> 9. Random Number Generators </H1><!--docid::SEC142::--><P><HR SIZE="6"><A NAME="SEC143"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC144"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 9.1 Overview </H2><!--docid::SEC143::--><P>These are the basic random number generators (RNGs):</P><P><A NAME="IDX339"></A><A NAME="IDX340"></A><A NAME="IDX341"></A></P><P><DL COMPACT><DT><CODE>Uniform</CODE><DD>Uniform reals on [0,1)<P><DT><CODE>Normal</CODE><DD>Normal with specified mean and variance<P><DT><CODE>Exponential</CODE><DD>Exponential with specified mean<P><DT><CODE>DiscreteUniform</CODE><DD>Integers uniformly distributed over a specified range.<P><DT><CODE>Beta</CODE><DD>Beta distribution<P><DT><CODE>Gamma</CODE><DD>Gamma distribution<P><DT><CODE>F</CODE><DD>F distribution</DL><P>To use these generators, you need to include some subset of these headers:</P><P><TABLE><tr><td> </td><td class=example><pre>#include <random/uniform.h>#include <random/normal.h>#include <random/exponential.h>#include <random/discrete-uniform.h>#include <random/beta.h>#include <random/gamma.h>#include <random/chisquare.h>#include <random/F.h>using namespace ranlib;</pre></td></tr></table></P><P>All the generators are inside the namespace <STRONG>ranlib</STRONG>, <A NAME="IDX342"></A>so a <STRONG>using namespace ranlib</STRONG> directive is required (alternately, youcan write e.g. <CODE>ranlib::Uniform<></CODE>).</P><P>These generators are all class templates. The first template parameter isthe number type you want to generate: float, double or long double forcontinuous distributions, and integer for discrete distributions. Thisparameter defaults to <CODE>float</CODE> for continuous distributions,and <CODE>unsigned int</CODE> for discrete distributions.</P><P>The constructors are:</P><P><TABLE><tr><td> </td><td class=example><pre>Uniform();Normal(T mean, T standardDeviation);Exponential(T mean);DiscreteUniform(T n); // range is 0 .. n-1Beta(T a, T b);Gamma(T mean);ChiSquare(T df);F(T dfn, T dfd);</pre></td></tr></table></P><P>where <CODE>T</CODE> is the first template parameter (<CODE>float</CODE>, <CODE>double</CODE>,or <CODE>long double</CODE>). To obtain a random number, use the method<CODE>random()</CODE>. Here is an example of constructing and using a<CODE>Normal</CODE> generator:</P><P><TABLE><tr><td> </td><td class=example><pre>#include <random/normal.h>using namespace ranlib;void foo(){ Normal<double> normalGen; double x = normalGen.random(); // x is a normal random number}</pre></td></tr></table></P><P><HR SIZE="6"><A NAME="SEC144"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC143"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC145"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC145"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 9.2 Note: Parallel random number generators </H2><!--docid::SEC144::--><P>The generators which Blitz++ provides are not suitable for parallelprograms. If you need parallel RNGs, you may find<A HREF="http://www.ncsa.uiuc.edu/Apps/SPRNG">http://www.ncsa.uiuc.edu/Apps/SPRNG</A> useful.</P><P><HR SIZE="6"><A NAME="SEC145"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC144"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC146"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC146"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 9.3 Seeding a random number generator </H2><!--docid::SEC145::--><P><A NAME="IDX343"></A></P><P>You may seed a random number generator using the member function<CODE>seed(unsigned int)</CODE>. <A NAME="IDX344"></A>By default, all randomnumber generators share the same underlying integer random number generator.So seeding one generator will seed them all. (Note: you can creategenerators with their own internal state; see the sections below). Youshould generally only seed a random number generator once, at the beginningof a program run. </P><P>Here is an example of seeding with the system clock:</P><P><TABLE><tr><td> </td><td class=example><pre>#include <random/uniform.h>#include <time.h>using namespace ranlib;int main(){ // At start of program, seed with the system time so we get // a different stream of random numbers each run. Uniform<float> x; x.seed((unsigned int)time(0)); // Rest of program ...}</pre></td></tr></table></P><P>Note: you may be tempted to seed the random number generator from a staticinitializer. <STRONG>Don't do it!</STRONG> Due to an oddity of C++, there is noguarantee on the order of static initialization when templates are involved.Hence, you may seed the RNG before its constructor is invoked, in which caseyour program will crash. If you don't know what a static initializer is,don't worry -- you're safe!</P><P><HR SIZE="6"><A NAME="SEC146"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC145"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC147"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC147"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 9.4 Detailed description of RNGs </H2><!--docid::SEC146::--><P><A NAME="IDX345"></A></P><P>There are really two types of RNGs:</P><P><DL COMPACT><DT><CODE>Integer</CODE><DD>RNGs provide uniformly distributed, unsigned 32 bit integers.<A NAME="IDX346"></A><A NAME="IDX347"></A><P><DT><CODE>RNGs</CODE><DD>use Integer RNGs to provide other kinds of random numbers. </DL><P>By default, the Integer RNG used is a faithful adaptation of the MersenneTwister <CODE>MT19937</CODE> @cindex MersenneTwister due to Matsumoto andNishimura (see <CITE>ACM Transactions on Modeling and Computer Simulation,Vol. 8, No. 1, January 1998, pp 3-30</CITE>,<A HREF="http://www.math.keio.ac.jp/~matumoto/emt.html">http://www.math.keio.ac.jp/~matumoto/emt.html</A>,<A HREF="http://www.acm.org/pubs/citations/journals/tomacs/1998-8-1/p3-matsumoto/">http://www.acm.org/pubs/citations/journals/tomacs/1998-8-1/p3-matsumoto/</A>).This generator has a period of <EM>2^{19937</EM>-1}2^(19937)-1, passed several stringentstatistical tests (including the<A HREF="http://stat.fsu.edu/~geo/diehard.html">http://stat.fsu.edu/~geo/diehard.html</A> tests), and has speedcomparable to other modern generators.</P><P><HR SIZE="6"><A NAME="SEC147"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC146"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC148"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC148"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_9.html#SEC142"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_10.html#SEC158"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_toc.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="blitz_abt.html#SEC_About"> ? </A>]</TD></TR></TABLE><H2> 9.5 Template parameters </H2><!--docid::SEC147::--><P>RNGs take three template parameters, all of which have default values.Using the <CODE>Uniform</CODE> RNG as an example, the template parameters of<CODE>Uniform<T, IRNG, stateTag></CODE> are:</P><P><DL COMPACT><DT><CODE>T</CODE><DD>is the type of random number to generate (one of <CODE>float</CODE>,<CODE>double</CODE>, or <CODE>long double</CODE> for continuous distributions; aninteger type for discrete distributions). Note that generating double and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -