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

📄 stocc.htm

📁 很好用的随机数产生器
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<title>Non-uniform random number generators</title>
<style><!--
body{font-family:verdana,arial,sans-serif;background-color:#CCFFCC;color:#000000;}
a,p{font-family:verdana,arial,sans-serif}
a:link{color:#0000CC; text-decoration:underline}
a:visited{color:#990099; text-decoration:underline}
a:active,a:hover{color:#6666FF; text-decoration:underline}
h1{text-align:Center;font-size:150%;font-weight:700;}
h2{text-align:Left;font-size:130%;font-weight:600;padding-top:1em}
pre  {font-family: 'courier new',courier; color:#000000; background-color:#FFFFCC; 
      border: 1px solid black; padding:0.5em }
dt   {font-weight:bold;}
dd   {font-weight:normal; padding-bottom:1em}
li   {padding-bottom:1em}
@media print {body{font-family:arial,sans-serif;background-color:#FFFFFF;color:#000000}}
@media print {a,a:link,a:visited {text-decoration:none}}
--></style>
</head>

<body>

<h1>Non-uniform random number generators</h1>

<p align="center">by Agner Fog</p>
<p>The file <a href="stocc.zip">stocc.zip</a> contains a C++ class library of non-uniform random number generators
providing variates with the following distributions: normal,
bernoulli, poisson, binomial, hypergeometric, Wallenius' noncentral hypergeometric,
and Fisher's noncentral hypergeometric distribution. Most distributions are
available in both univariate and multivariate versions. There is also a function to
shuffle numbers.</p>
<p>Most of these functions are fast and accurate, even for extreme values of the
parameters.</p>
<p>The functions are based on the uniform random number generators in <a href="randomc.zip">randomc.zip</a>
or <a href="randoma.zip">randoma.zip</a>.</p>
<h2>File list</h2>
<p>The archive <a href="stocc.zip">stocc.zip</a> contains the following
files:</p>
<dl>
  <dt>stocc.htm</dt>
  <dd>This file. Instructions</dd>
  <dt>stocc.h</dt>
  <dd>C++ header file containing class definitions.<br>
    You must <code>#include</code> this in all C++ files that use this library.</dd>
  <dt>stoc1.cpp</dt>
  <dd>C++ source code for the non-uniform random number generators for the most
    common probability distributions.</dd>
  <dt>stoc2.cpp</dt>
  <dd>Alternative code for the distributions poisson, binomial and hypergeometric.
    Optimized for situations where these functions are called repeatedly with
    the same parameters.</dd>
  <DT>stoc3.cpp</DT>
  <DD>Source code for generating random variables with the univariate and
    multivariate Wallenius' and Fisher's noncentral hypergeometric distributions.</DD>
  <DT>userintf.cpp</DT>
  <DD>System-dependent user interface code</DD>
  <DT>fnchyppr.cpp wnchyppr.cpp erfres.cpp</DT>
  <DD>Additional source code used by stoc3.cpp</DD>
  <dt>ex-stoc.cpp</dt>
  <dd>Example showing how to use this class library to make random numbers with
    various distributions.</dd>
  <dt>ex-lib.cpp</dt>
  <dd>Example showing how to use a combined random number generator from the
    assembly language library as base for the non-uniform random number
    functions.</dd>
  <dt>ex-cards.cpp</dt>
  <dd>Example showing how to shuffle a list of items randomly. Shuffles a deck
    of cards.</dd>
  <dt>ex-lotto.cpp</dt>
  <dd>Example showing how to generate a sequence of random integers where no
    number occurs more than once.</dd>
  <dt>  testpois.cpp,&nbsp; testbino.cpp,&nbsp; testhype.cpp, testfnch.cpp, testwnch.cpp,
    testmfnc.cpp, testmwnc.cpp</dt>
  <dd>Test programs for testing the poisson, binomial, hypergeometric, and
    univariate and multivariate Fisher's and Wallenius' noncentral
    hypergeometric distribution generators. Also useful for calculating mean and variance of
    these distributions.</dd>
  <dt>distrib.pdf</dt>
  <dd>Definition of the statistical distributions that can be generated with
    this package.</dd>
  <dt>sampmet.pdf</dt>
  <dd>Sampling methods. Theoretical explanation of the methods used for
    generating variates with these distributions.</dd>
</dl>
<h2>Instructions</h2>
<p>Unpack the files <a href="randomc.zip">randomc.zip</a> and <a href="stocc.zip">stocc.zip</a>
into a new directory. Make sure all the unpacked files are in the same
directory. To get started, just compile one of the example programs for console
mode using any C++ compiler.</p>
<p>The C++ class <code>StochasticLib</code> can be derived from any of the
random number generator classes in <a href="randomc.zip">randomc.zip</a>. Choose
the one you like best. The examples use the &quot;Mersenne Twister&quot; class <code>TRandomMersenne</code>&nbsp;
because this is the random number generator that has the fewest portability
problems. To use a different random number generator as base class, just change
the&nbsp;<br>
&nbsp;
<code>#define RANDOM_GENERATOR TRandomMersenne</code>&nbsp;<br>
directive in the file stocc.h or
make such a define before the&nbsp;<br>
&nbsp;
<code>#include &quot;stocc.h&quot;<br>
</code>statement in <i>all</i> your
modules.</p>
<p>The C++ class <code>StochasticLib2</code> is derived from&nbsp;<code>StochasticLib</code>.
It contains alternative versions of the functions poisson, binomial and
hypergeometric.&nbsp; <code>StochasticLib </code>is the best choice if the
parameters to these functions often wary in your program,&nbsp;<code>StochasticLib2</code>&nbsp;
is faster when these functions are called many times with the same parameters.</p>
<p>The C++ class <code>StochasticLib3</code> is derived from&nbsp;<code>StochasticLib</code>
or <code>StochasticLib2</code>. It adds the Fisher's and Wallenius' noncentral
hypergeometric distributions, and the multivariate versions of these, to the
library.</p>
<p>The header file stocc.h must be included in all modules that use this class
library. The source code file stoc1.cpp can be included in your project, either
as an <code>#include</code>, or as a separate module. If you are using&nbsp;<code>StochasticLib2</code>,&nbsp;
then you need both stoc1.cpp and stoc2.cpp. If you are using&nbsp;<code>StochasticLib3</code>,&nbsp;
then you need both stoc1.cpp and stoc3.cpp.</p>
<p>It is recommended that you make only one instance of&nbsp;<code>StochasticLib
</code>or&nbsp; <code>StochasticLib2 </code>or&nbsp;<code>StochasticLib3</code>. These stochastic library classes
inherit the uniform random number functions from their base class. Therefore,
one object of <code>StochasticLib3</code> gives you access to all the uniform and non-uniform random
number generators.
Example:</p>
<pre>#include &quot;stocc.h&quot;                // header file for class library
#include &lt;time.h&gt;                 // define time function

void main () {
  int seed = time(0);             // seed for random number generator
  StochasticLib sto(seed);        // make instance of class
  int i = sto.IRandom(0, 99);     // make random integer with uniform distribution
  int b = sto.Binomial(0.2, 100); // make random integer with binomial distribution
  ...
  }</pre>

<p>See the file ex-stoc.cpp for a more detailed example.</p>

<p>See the file ex-lib.cpp for an example of how to use an assembly language
uniform random number generator as base for the non-uniform random number
generators.</p>
<p>If you make more than one instance of these classes then make sure they don't
have the same seed.</p>

<h2>Portability</h2>
<p>The C++ class library is supposed to work with all C++ compilers and all
operating systems. It has been tested on several different systems.</p>
<p>There are, however, a few system differences that you need to be aware of:</p>
<ol>
  <li>Error messages. There is no portable way of writing error messages.
    Systems with a graphical user interface (e.g. Windows) need a pop-up message
    box, while console mode programs and other line oriented systems need output to the standard output or error
    output. Therefore, you may have to modify the function <code>FatalError</code>&nbsp;
    in the file userintf.cpp to fit your system. This function is called by the library functions in case of illegal parameter
    values or other fatal errors. Experience shows that these
    error messages are very useful when debugging a program that uses the
    non-uniform random variate generators. You may even enhance the <code>FatalError</code>
    function to output additional debug information about the state of your
    program.&nbsp;</li>
  <li>Program exit. Windows-like environments may require that the program waits
    for the user to press a key before exiting, in order to prevent the output
    screen image from disappearing. Therefore, you may have to modify the function
    <code>EndOfProgram</code>
    in userintf.cpp to fit your system if you experience this problem.</li>
  <li>Multithreaded programs. The functions are not re-entrant. This
    is because they rely on static variables to remember the last parameters, so
    that they don't have to re-do set-up calculations that are the same
    as last time the function was called.<br>

⌨️ 快捷键说明

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