3 <title>Random Numbers</title>
4 <link rel="stylesheet" type="text/css" href="pythia.css"/>
5 <link rel="shortcut icon" href="pythia32.gif"/>
9 <h2>Random Numbers</h2>
11 This page describes the random-number generator in PYTHIA and
12 how it can be replaced by an external one.
14 <h3>Internal random numbers</h3>
16 The <code>Rndm</code> class generates random numbers, using the
17 Marsaglia-Zaman-Tsang algorithm [<a href="Bibliography.html" target="page">Mar90</a>].
20 Random numbers <code>R</code> uniformly distributed in
21 <code>0 < R < 1</code> are obtained with
25 There are also methods to generate according to an exponential, to
26 <i>x * exp(-x)</i>, to a Gaussian, or picked among a set of
27 possibilites, which make use of <code>flat()</code>.
30 If the random number generator is not initialized before, it will be
31 so the first time it is asked to generate a random number, and then
32 with the default seed, 19780503. This means that, by default, all runs
33 will use identically the same random number sequence. This is
34 convenient for debugging purposes, but dangerous if you intend to
35 run several "identical" jobs to boost statistics. You can initialize,
36 or reinitialize, with your own choice of seed with a
40 Here values <code>0 < seed < 900 000 000</code> gives so many
41 different random number sequences, while <code>seed = 0</code> will call
42 the <code>Stdlib time(0)</code> function to provide a "random"
43 <code>seed</code>, and <code>seed < 0</code> will revert back to
44 the default <code>seed</code>.
47 The <code>Pythia</code> class defines <a href="RandomNumberSeed.html" target="page">a
48 flag and a mode</a>, that allows the <code>seed</code> to be set in
49 the <code>Pythia::init</code> call. That would be the standard way for a
50 user to pick the random number sequence in a run.
52 <h3>External random numbers</h3>
54 <code>RndmEngine</code> is a base class for the external handling of
55 random-number generation. The user-written derived class is called
56 if a pointer to it has been handed in with the
57 <code>pythia.rndmEnginePtr()</code> method. Since the default
58 Marsaglia-Zaman-Tsang algorithm is quite good, chances are that any
59 replacement would be a step down, but this may still be required by
60 consistency with other program elements in big experimental frameworks.
63 There is only one pure virtual method in <code>RndmEngine</code>, to
64 generate one random number flat in the range between 0 and 1:
66 virtual double flat() = 0;
68 Note that methods for initialization are not provided in the base
69 class, in part since input parameters may be specific to the generator
70 used, in part since initialization can as well be taken care of
71 externally to the <code>Pythia</code> code.
74 An example illustrating how to run with an external random number
75 generator is provided in <code>main24.cc</code>.
79 We here collect a more complete and formal overview of the methods.
81 <a name="method1"></a>
82 <p/><strong>Rndm::Rndm() </strong> <br/>
83 construct a random number generator, but does not initialize it.
86 <a name="method2"></a>
87 <p/><strong>Rndm::Rndm(int seed) </strong> <br/>
88 construct a random number generator, and initialize it for the
92 <a name="method3"></a>
93 <p/><strong>bool Rndm::rndmEnginePtr( RndmEngine* rndmPtr) </strong> <br/>
94 pass in pointer for external random number generation.
97 <a name="method4"></a>
98 <p/><strong>void Rndm::init(int seed = 0) </strong> <br/>
99 initialize, or reinitialize, the random number generator for the given
100 seed number. Not necessary if the seed was already set in the constructor.
103 <a name="method5"></a>
104 <p/><strong>double Rndm::flat() </strong> <br/>
105 generate next random number uniformly between 0 and 1.
108 <a name="method6"></a>
109 <p/><strong>double Rndm::exp() </strong> <br/>
110 generate random numbers according to <i>exp(-x)</i>.
113 <a name="method7"></a>
114 <p/><strong>double Rndm::xexp() </strong> <br/>
115 generate random numbers according to <i>x exp(-x)</i>.
118 <a name="method8"></a>
119 <p/><strong>double Rndm::gauss() </strong> <br/>
120 generate random numbers according to <i>exp(-x^2/2)</i>.
123 <a name="method9"></a>
124 <p/><strong>pair<double, double> Rndm::gauss2() </strong> <br/>
125 generate a pair of random numbers according to
126 <i>exp( -(x^2 + y^2) / 2)</i>. Is faster than two calls
127 to <code>gauss()</code>.
130 <a name="method10"></a>
131 <p/><strong>int Rndm::pick(const vector<double>& prob) </strong> <br/>
132 pick one option among vector of (positive) probabilities.
135 <a name="method11"></a>
136 <p/><strong>bool Rndm::dumpState(string fileName) </strong> <br/>
137 save the current state of the random number generator to a binary
138 file. This involves two integers and 100 double-precision numbers.
139 Intended for debug purposes. Note that binary files may be
140 platform-dependent and thus not transportable.
143 <a name="method12"></a>
144 <p/><strong>bool Rndm::readState(string fileName) </strong> <br/>
145 set the state of the random number generator by reading in a binary
146 file saved by the above command. Comments as above.
149 <a name="method13"></a>
150 <p/><strong>virtual double RndmEngine::flat() </strong> <br/>
151 if you want to construct an external random number generator
152 (or generator interface) then you must implement this method
153 in your class derived from the <code>RndmEningen</code> base class,
154 to give a random number between 0 and 1.
160 <!-- Copyright (C) 2010 Torbjorn Sjostrand -->