<chapter name="Random Numbers"> 
     
<h2>Random Numbers</h2> 
 
This page describes the random-number generator in PYTHIA and 
how it can be replaced by an external one. 
  
<h3>Internal random numbers</h3> 
 
The <code>Rndm</code> class generates random numbers, using the 
Marsaglia-Zaman-Tsang algorithm <ref>Mar90</ref>. 
 
<p/> 
Random numbers <code>R</code> uniformly distributed in 
<code>0 &lt; R &lt; 1</code> are obtained with 
<pre> 
   Rndm::flat(); 
</pre> 
There are also methods to generate according to an exponential, to 
<ei>x * exp(-x)</ei>, to a Gaussian, or picked among a set of 
possibilities, which make use of <code>flat()</code>. 
 
<p/> 
If the random number generator is not initialized before, it will be 
so the first time it is asked to generate a random number, and then 
with the default seed, 19780503. This means that, by default, all runs 
will use identically the same random number sequence. This is 
convenient for debugging purposes, but dangerous if you intend to 
run several "identical" jobs to boost statistics. You can initialize, 
or reinitialize, with your own choice of seed with a 
<pre> 
   Rndm::init(seed); 
</pre> 
Here values <code>0 &lt; seed &lt; 900 000 000</code> gives so many 
different random number sequences, while <code>seed = 0</code> will call 
the <code>Stdlib time(0)</code> function to provide a "random" 
<code>seed</code>, and <code>seed &lt; 0</code> will revert back to 
the default <code>seed</code>. 
 
<p/> 
The <code>Pythia</code> class defines <aloc href="RandomNumberSeed">a 
flag and a mode</aloc>, that allows the <code>seed</code> to be set in 
the <code>Pythia::init</code> call. That would be the standard way for a 
user to pick the random number sequence in a run. 
 
<h3>External random numbers</h3> 
 
<code>RndmEngine</code> is a base class for the external handling of 
random-number generation. The user-written derived class is called 
if a pointer to it has been handed in with the 
<code>pythia.rndmEnginePtr()</code> method. Since the default 
Marsaglia-Zaman-Tsang algorithm is quite good, chances are that any 
replacement would be a step down, but this may still be required by 
consistency with other program elements in big experimental frameworks. 
 
<p/> 
There is only one pure virtual method in <code>RndmEngine</code>, to 
generate one random number flat in the range between 0 and 1: 
<pre> 
  virtual double flat() = 0; 
</pre> 
Note that methods for initialization are not provided in the base 
class, in part since input parameters may be specific to the generator 
used, in part since initialization can as well be taken care of 
externally to the <code>Pythia</code> code. 
 
<p/> 
An example illustrating how to run with an external random number 
generator is provided in <code>main23.cc</code>. 
 
<h3>The methods</h3> 
 
We here collect a more complete and formal overview of the methods. 
    
<method name="Rndm::Rndm()"> 
construct a random number generator, but does not initialize it. 
</method> 
    
<method name="Rndm::Rndm(int seed)"> 
construct a random number generator, and initialize it for the 
given seed number. 
</method> 
    
<method name="bool Rndm::rndmEnginePtr( RndmEngine* rndmPtr)"> 
pass in pointer for external random number generation. 
</method> 
    
<method name="void Rndm::init(int seed = 0)"> 
initialize, or reinitialize, the random number generator for the given 
seed number. Not necessary if the seed was already set in the constructor. 
</method> 
    
<method name="double Rndm::flat()"> 
generate next random number uniformly between 0 and 1. 
</method> 
    
<method name="double Rndm::exp()"> 
generate random numbers according to <ei>exp(-x)</ei>. 
</method> 
    
<method name="double Rndm::xexp()"> 
generate random numbers according to <ei>x exp(-x)</ei>. 
</method> 
    
<method name="double Rndm::gauss()"> 
generate random numbers according to <ei>exp(-x^2/2)</ei>. 
</method> 
    
<method name="pair&lt;double, double&gt; Rndm::gauss2()"> 
generate a pair of random numbers according to 
<ei>exp( -(x^2 + y^2) / 2)</ei>. Is faster than two calls 
to <code>gauss()</code>. 
</method> 
    
<method name="int Rndm::pick(const vector<double>& prob)"> 
pick one option among vector of (positive) probabilities. 
</method> 
    
<method name="bool Rndm::dumpState(string fileName)"> 
save the current state of the random number generator to a binary 
file. This involves two integers and 100 double-precision numbers. 
Intended for debug purposes. Note that binary files may be 
platform-dependent and thus not transportable. 
</method> 
    
<method name="bool Rndm::readState(string fileName)"> 
set the state of the random number generator by reading in a binary 
file saved by the above command. Comments as above. 
</method> 
    
<method name="virtual double RndmEngine::flat()"> 
if you want to construct an external random number generator 
(or generator interface) then you must implement this method 
in your class derived from the <code>RndmEningen</code> base class, 
to give a random number between 0 and 1. 
</method> 
 
</chapter> 
 
<!-- Copyright (C) 2014 Torbjorn Sjostrand --> 
