⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nr02doc.htm

📁 随机数生成程序
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html><head><meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0"><title>Newran - random number generator library</title></head><body BGCOLOR="#FFFFFF"><h1><a NAME="top"></a><font COLOR="#FF0000">Newran02A - a random number generator library</font></h1><i><p></i>30 August, 1998</p><p>Copyright (C) 1989, 1995, 1998: R B Davies </p><p><i>Permission is granted to use or distribute but not to sell</i>. <ul>  <li><a HREF="#overview">Overview</a></li>  <li><a HREF="#starting">Getting started</a></li>  <li><a HREF="#description">Descriptions of the classes to be accessed by the user</a></li>  <li><a HREF="#supporting">Descriptions of the supporting classes</a></li>  <li><a HREF="#generating">Generating numbers from other distributions</a></li>  <li><a HREF="#other">Other people's code</a></li>  <li><a HREF="#files">Files included in this package</a></li>  <li><a HREF="#class">Class structure</a></li>  <li><a HREF="#todo">To do</a></li>  <li><a href="#history">History</a></li>  <li><a HREF="ol_doc.htm">To online documentation page</a></li></ul><p>This is a C++ library for generating sequences of random numbers from a wide variety ofdistributions. It is particularly appropriate for the situation where one requiressequences of identically distributed random numbers since the set up time for each type ofdistribution is relatively long but it is efficient when generating each new randomnumber. The library includes <i>classes</i> for generating random numbers from a number ofdistributions and is easily extended to be able to generate random numbers from almost anyof the standard distributions. </p><p><i>Newran02</i>, fixes some bugs, includes more extensive testing, has been updated tobe compatible with <i>newmat09</i> and includes some additional distributions. <em>Newran02A</em>includes an updated version of the exception library.</p><p>Comments and bug reports to <a HREF="mailto:robertd@netlink.co.nz">mailto:robertd@netlink.co.nz</a>.</p><p>For updates and notes see <a HREF="http://webnz.com/robert/">http://webnz.com/robert/</a>.</p><p>&nbsp; </p><h2><a NAME="overview"></a><font COLOR="#FF0000">Overview</font></h2><p>The following are the classes for generating random numbers from particulardistributions&nbsp; </p><table BORDER="0" width="100%">  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#uniform">Uniform</a></td>    <td VALIGN="TOP" width="80%">uniform distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#constant">Constant</a></td>    <td VALIGN="TOP" width="80%">return a constant</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#exponential">Exponential</a></td>    <td VALIGN="TOP" width="80%">negative exponential distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#cauchy">Cauchy</a></td>    <td VALIGN="TOP" width="80%">Cauchy distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#normal">Normal</a></td>    <td VALIGN="TOP" width="80%">normal distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#chisq">ChiSq</a></td>    <td VALIGN="TOP" width="80%">non-central chi-squared distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#gamma">Gamma</a></td>    <td VALIGN="TOP" width="80%">gamma distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#pareto">Pareto</a></td>    <td VALIGN="TOP" width="80%">Pareto distribution</td>  </tr>  <tr>    <td VALIGN="TOP" width="20%"><a HREF="#poisson">Poisson</a></td>    <td VALIGN="TOP" width="80%">Poisson distribution</td>  </tr>  <tr>    <td width="20%"><a HREF="#binomial">Binomial</a></td>    <td width="80%">binomial distribution</td>  </tr>  <tr>    <td width="20%"><a HREF="#negativebinomial">NegativeBinomial</a></td>    <td width="80%">negative binomial distribution</td>  </tr></table><p>The following classes are available to the user for generating numbers from otherdistributions &nbsp; </p><table BORDER="0" WIDTH="100%">  <tr>    <td VALIGN="top" width="20%"><a HREF="#posgenx">PosGenX</a></td>    <td VALIGN="top" width="80%">Positive random numbers with a decreasing density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#symgenx">SymGenX</a></td>    <td VALIGN="top" width="80%">Random numbers from a symmetric unimodal density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#asymgenx">AsymGenX</a></td>    <td VALIGN="top" width="80%">Random numbers from an asymmetric unimodal density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#posgen">PosGen</a></td>    <td VALIGN="top" width="80%">Positive random numbers with a decreasing density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#symgen">SymGen</a></td>    <td VALIGN="top" width="80%">Random numbers from a symmetric unimodal density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#asymgen">AsymGen</a></td>    <td VALIGN="top" width="80%">Random numbers from an asymmetric unimodal density</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#discretegen">DiscreteGen</a></td>    <td VALIGN="top" width="80%">Random numbers from a discrete distribution</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#sum">SumRandom</a></td>    <td VALIGN="top" width="80%">Sum and/or product of random numbers</td>  </tr>  <tr>    <td VALIGN="top" width="20%"><a HREF="#mixed">MixedRandom</a></td>    <td VALIGN="top" width="80%">Mixture of random numbers</td>  </tr></table><p>Each of these classes has the following member functions &nbsp; </p><table BORDER="0" WIDTH="75%">  <tr>    <td VALIGN="TOP"><tt>Real Next()</tt></td>    <td VALIGN="TOP">Get a new random number</td>  </tr>  <tr>    <td VALIGN="TOP"><tt>char* Name()</tt></td>    <td VALIGN="TOP">Name of the distribution</td>  </tr>  <tr>    <td VALIGN="TOP"><tt>ExtReal Mean()</tt></td>    <td VALIGN="TOP">Mean of the distribution</td>  </tr>  <tr>    <td VALIGN="TOP"><tt>ExtReal Variance()</tt></td>    <td VALIGN="TOP">Variance of the distribution</td>  </tr></table><p>These 4 functions are declared <i>virtual</i> so it is easy to write simulationprograms that can be run with different distributions. </p><p><i>Real</i> is typedefed to be either <i>float</i> or <i>double</i>. See <aHREF="#customising">customising</a>. Note that <tt>Next()</tt> always returns a <i>Real</i>even for discrete distributions. </p><p><a HREF="#extreal">ExtReal</a> is a class which is, in effect either a <i>Real</i> orone of the following: <i>PlusInfinity</i>, <i>MinusInfinity</i>, <i>Indefinite</i> or <i>Missing</i>.I use <i>ExtReal</i> so that I can return infinite or indefinite values for the mean orvariance of a distribution. </p><p>There are two static functions in the class <i>Random</i>. </p><pre>&nbsp;&nbsp; void Random::Set(double)</pre><p>must be called with an argument between 0 and 1 to set up the base random numbergenerator before <tt>Next()</tt> is called in any class. </p><pre>&nbsp;&nbsp; double Random::Get()</pre><p>returns the current value of the seed. </p><p>There are two classes for doing combinations and permutations. <br>&nbsp; </p><table BORDER="0" WIDTH="100%">  <tr>    <td width="25%"><a HREF="#permutation">RandomPermutation</a></td>    <td width="75%">Draw numbers without replacement</td>  </tr>  <tr>    <td width="25%"><a HREF="#combination">RandomCombination</a></td>    <td width="75%">Draw numbers without replacement and sort</td>  </tr></table><p>Further details of all these classes including the constructors are given <aHREF="#description">below</a>. </p><p>&nbsp; </p><h2><a NAME="starting"></a><font COLOR="#FF0000">Getting started</font></h2><h3><a NAME="customising"></a><font COLOR="#FF0000">Customising</font></h3><p>The file include.h sets a variety of options including several compiler dependentoptions. You may need to edit include.h to get the options you require. If you are using acompiler different from one I have worked with you may have to set up a new section ininclude.h appropriate for your compiler. </p><p>Borland, Turbo, Gnu, Microsoft and Watcom are recognised automatically. If none ofthese are recognised a default set of options is used. These are fine for AT&amp;T, HPUXand Sun C++. If you using a compiler I don't know about, you may have to write a new setof options. </p><p>There is an option in include.h for selecting whether you use compiler supportedexceptions, simulated exceptions, or disable exceptions. Use the option for compilersupported exceptions if and only if you have set the option on your compiler to recogniseexceptions. Disabling exceptions sometimes helps with compilers that are incompatible withmy exception simulation scheme. <br>&nbsp; </p><table BORDER="1" WIDTH="100%">  <tr>    <td ALIGN="CENTER" WIDTH="100%">This version of <i>newran</i> does <i>not</i> do memory    clean-up with the <i>simulated</i> exceptions.</td>  </tr></table><p>If your compiler recognises <i>bool</i> as required by the standard activate thestatement <tt>#define bool_LIB</tt>. This will deactivate my Boolean class. (I haveincluded a command in the make file to do this automatically with Gnu G++).</p><p>Activate the appropriate statement to make the element type <i>Real</i> to mean <i>float</i>or <i>double</i>. </p><p>Activate the <i>namespace</i> option if your want to use namespaces and have a compilerthat <i>really</i> does support them. </p><h3><font COLOR="#FF0000">Compiling</font></h3><p>You will need to compile newran.cpp, myexcept.cpp and extreal.cpp and link theresulting object files to your programs. Your source files which access <i>newran</i> willneed to have newran.h as an include file. </p><h3><font COLOR="#FF0000">Compilers</font></h3><p>I have tested newran02 with the following compilers (all PC ones in 32 bit consolemode)</p><table border="0" width="75%">  <tr>    <td width="50%" valign="top">Borland 5.01A</td>    <td width="50%" valign="top">OK</td>  </tr>  <tr>    <td width="50%" valign="top">Borland 3.1</td>    <td width="50%" valign="top">See notes on 16 bit under <a href="#testing">testing</a></td>  </tr>  <tr>    <td width="50%" valign="top">Microsoft 5.0</td>    <td width="50%" valign="top">OK</td>  </tr>  <tr>    <td width="50%" valign="top">Watcom 10A</td>    <td width="50%" valign="top">OK</td>  </tr>  <tr>    <td width="50%" valign="top">Sun CC 4.2</td>    <td width="50%" valign="top">OK</td>  </tr>  <tr>    <td width="50%" valign="top">Gnu G++ 2.7.2 (Linux), 2.8.0 (Sun)</td>    <td width="50%" valign="top">OK</td>  </tr></table><p>I have included make files for Watcom 10A, CC and Gnu G++. See <a href="#files">files</a>section.</p><h3><a name="testing"></a><font COLOR="#FF0000">Testing</font></h3><p>The files tryrand.cpp, tryrand1.cpp, tryrand2.cpp, tryrand3.cpp, tryrand4.cpp, hist.cpprun the generators in the library and print histograms of the resulting distributions.Sample means and variances are also calculated and can be compared with the populationvalues. The correct results are in tryrand.txt (although there might be slight differencesin the format of the output from different compilers). <br>&nbsp; </p><table BORDER="1" WIDTH="100%">  <tr>    <td WIDTH="100%">If you are compiling on a PC with a 16 bit compiler you will need to set <i>n_large</i>    to be 8000 rather than 1000000 and <i>n</i> to 8000 rather than 200000 in tryrand.cpp.    This will change the output from the test program from that given in tryrand.txt although    the general appearance should be the same.</td>  </tr></table><p>The test program tryrand.cpp includes a simple test for memory leaks. This is valid foronly some compilers. It seems to work for Borland C++ in console mode but not for Gnu G++or Microsoft C++, where it almost always (presumably incorrectly) suggests an error. </p><p>&nbsp; </p><h2><a NAME="description"></a><font COLOR="#FF0000">Descriptions of the classes to beaccessed by the user:</font></h2><h3><a NAME="random"></a><font COLOR="#FF0000">Random:</font></h3><p>This is the basic uniform random number generator, used to drive all the others. TheLewis-Goodman-Miller algorithm is used with Marsaglia mixing. While not perfect, and nowsuperseded, the LGM generator has given me acceptable results in a wide variety ofsimulations. See <i>Numerical Recipes in C</i> by Press, Flannery, Teukolsky, Vetterlingpublished by the Cambridge University Press for details. The LGM generator does pass theMarsaglia <i><a HREF="http://stat.fsu.edu/~geo/diehard.html">diehard</a></i> tests whenyou include the mixing. (It doesn't pass without the mixing). Nevertheless it should beupgraded. Ideally the basic generator should be recoded in assembly language to give themaximum speed to all the generators in this package. You can access the numbers directlyusing <tt>Next()</tt> but I suggest you use class <a HREF="#uniform">Uniform</a> foruniform random numbers and reserve Random for setting the starting seed and as the baseclass for the random number generators. </p><h3><a NAME="uniform"></a><font COLOR="#FF0000">Uniform:</font></h3><p>Return a uniform random number from the range (0, 1). The constructor has noparameters. For example </p><pre>&nbsp;&nbsp; Uniform U;&nbsp;&nbsp; for (int i=0; i&lt;100; i++) cout &lt;&lt; U.Next() &lt;&lt; &quot;\n&quot;;</pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -