1 | <html> |
---|
2 | <head> |
---|
3 | <title>Random Numbers</title> |
---|
4 | <link rel="stylesheet" type="text/css" href="pythia.css"/> |
---|
5 | <link rel="shortcut icon" href="pythia32.gif"/> |
---|
6 | </head> |
---|
7 | <body> |
---|
8 | |
---|
9 | <script language=javascript type=text/javascript> |
---|
10 | function stopRKey(evt) { |
---|
11 | var evt = (evt) ? evt : ((event) ? event : null); |
---|
12 | var node = (evt.target) ? evt.target :((evt.srcElement) ? evt.srcElement : null); |
---|
13 | if ((evt.keyCode == 13) && (node.type=="text")) |
---|
14 | {return false;} |
---|
15 | } |
---|
16 | |
---|
17 | document.onkeypress = stopRKey; |
---|
18 | </script> |
---|
19 | <?php |
---|
20 | if($_POST['saved'] == 1) { |
---|
21 | if($_POST['filepath'] != "files/") { |
---|
22 | echo "<font color='red'>SETTINGS SAVED TO FILE</font><br/><br/>"; } |
---|
23 | else { |
---|
24 | echo "<font color='red'>NO FILE SELECTED YET.. PLEASE DO SO </font><a href='SaveSettings.php'>HERE</a><br/><br/>"; } |
---|
25 | } |
---|
26 | ?> |
---|
27 | |
---|
28 | <form method='post' action='RandomNumbers.php'> |
---|
29 | |
---|
30 | <h2>Random Numbers</h2> |
---|
31 | |
---|
32 | This page describes the random-number generator in PYTHIA and |
---|
33 | how it can be replaced by an external one. |
---|
34 | |
---|
35 | <h3>Internal random numbers</h3> |
---|
36 | |
---|
37 | The <code>Rndm</code> class generates random numbers, using the |
---|
38 | Marsaglia-Zaman-Tsang algorithm [<a href="Bibliography.php" target="page">Mar90</a>]. |
---|
39 | |
---|
40 | <p/> |
---|
41 | Random numbers <code>R</code> uniformly distributed in |
---|
42 | <code>0 < R < 1</code> are obtained with |
---|
43 | <pre> |
---|
44 | Rndm::flat(); |
---|
45 | </pre> |
---|
46 | There are also methods to generate according to an exponential, to |
---|
47 | <i>x * exp(-x)</i>, to a Gaussian, or picked among a set of |
---|
48 | possibilites, which make use of <code>flat()</code>. |
---|
49 | |
---|
50 | <p/> |
---|
51 | If the random number generator is not initialized before, it will be |
---|
52 | so the first time it is asked to generate a random number, and then |
---|
53 | with the default seed, 19780503. This means that, by default, all runs |
---|
54 | will use identically the same random number sequence. This is |
---|
55 | convenient for debugging purposes, but dangerous if you intend to |
---|
56 | run several "identical" jobs to boost statistics. You can initialize, |
---|
57 | or reinitialize, with your own choice of seed with a |
---|
58 | <pre> |
---|
59 | Rndm::init(seed); |
---|
60 | </pre> |
---|
61 | Here values <code>0 < seed < 900 000 000</code> gives so many |
---|
62 | different random number sequences, while <code>seed = 0</code> will call |
---|
63 | the <code>Stdlib time(0)</code> function to provide a "random" |
---|
64 | <code>seed</code>, and <code>seed < 0</code> will revert back to |
---|
65 | the default <code>seed</code>. |
---|
66 | |
---|
67 | <p/> |
---|
68 | The <code>Pythia</code> class defines <?php $filepath = $_GET["filepath"]; |
---|
69 | echo "<a href='RandomNumberSeed.php?filepath=".$filepath."' target='page'>";?>a |
---|
70 | flag and a mode</a>, that allows the <code>seed</code> to be set in |
---|
71 | the <code>Pythia::init</code> call. That would be the standard way for a |
---|
72 | user to pick the random number sequence in a run. |
---|
73 | |
---|
74 | <h3>External random numbers</h3> |
---|
75 | |
---|
76 | <code>RndmEngine</code> is a base class for the external handling of |
---|
77 | random-number generation. The user-written derived class is called |
---|
78 | if a pointer to it has been handed in with the |
---|
79 | <code>pythia.rndmEnginePtr()</code> method. Since the default |
---|
80 | Marsaglia-Zaman-Tsang algorithm is quite good, chances are that any |
---|
81 | replacement would be a step down, but this may still be required by |
---|
82 | consistency with other program elements in big experimental frameworks. |
---|
83 | |
---|
84 | <p/> |
---|
85 | There is only one pure virtual method in <code>RndmEngine</code>, to |
---|
86 | generate one random number flat in the range between 0 and 1: |
---|
87 | <pre> |
---|
88 | virtual double flat() = 0; |
---|
89 | </pre> |
---|
90 | Note that methods for initialization are not provided in the base |
---|
91 | class, in part since input parameters may be specific to the generator |
---|
92 | used, in part since initialization can as well be taken care of |
---|
93 | externally to the <code>Pythia</code> code. |
---|
94 | |
---|
95 | <p/> |
---|
96 | An example illustrating how to run with an external random number |
---|
97 | generator is provided in <code>main23.cc</code>. |
---|
98 | |
---|
99 | <h3>The methods</h3> |
---|
100 | |
---|
101 | We here collect a more complete and formal overview of the methods. |
---|
102 | |
---|
103 | <a name="method1"></a> |
---|
104 | <p/><strong>Rndm::Rndm() </strong> <br/> |
---|
105 | construct a random number generator, but does not initialize it. |
---|
106 | |
---|
107 | |
---|
108 | <a name="method2"></a> |
---|
109 | <p/><strong>Rndm::Rndm(int seed) </strong> <br/> |
---|
110 | construct a random number generator, and initialize it for the |
---|
111 | given seed number. |
---|
112 | |
---|
113 | |
---|
114 | <a name="method3"></a> |
---|
115 | <p/><strong>bool Rndm::rndmEnginePtr( RndmEngine* rndmPtr) </strong> <br/> |
---|
116 | pass in pointer for external random number generation. |
---|
117 | |
---|
118 | |
---|
119 | <a name="method4"></a> |
---|
120 | <p/><strong>void Rndm::init(int seed = 0) </strong> <br/> |
---|
121 | initialize, or reinitialize, the random number generator for the given |
---|
122 | seed number. Not necessary if the seed was already set in the constructor. |
---|
123 | |
---|
124 | |
---|
125 | <a name="method5"></a> |
---|
126 | <p/><strong>double Rndm::flat() </strong> <br/> |
---|
127 | generate next random number uniformly between 0 and 1. |
---|
128 | |
---|
129 | |
---|
130 | <a name="method6"></a> |
---|
131 | <p/><strong>double Rndm::exp() </strong> <br/> |
---|
132 | generate random numbers according to <i>exp(-x)</i>. |
---|
133 | |
---|
134 | |
---|
135 | <a name="method7"></a> |
---|
136 | <p/><strong>double Rndm::xexp() </strong> <br/> |
---|
137 | generate random numbers according to <i>x exp(-x)</i>. |
---|
138 | |
---|
139 | |
---|
140 | <a name="method8"></a> |
---|
141 | <p/><strong>double Rndm::gauss() </strong> <br/> |
---|
142 | generate random numbers according to <i>exp(-x^2/2)</i>. |
---|
143 | |
---|
144 | |
---|
145 | <a name="method9"></a> |
---|
146 | <p/><strong>pair<double, double> Rndm::gauss2() </strong> <br/> |
---|
147 | generate a pair of random numbers according to |
---|
148 | <i>exp( -(x^2 + y^2) / 2)</i>. Is faster than two calls |
---|
149 | to <code>gauss()</code>. |
---|
150 | |
---|
151 | |
---|
152 | <a name="method10"></a> |
---|
153 | <p/><strong>int Rndm::pick(const vector<double>& prob) </strong> <br/> |
---|
154 | pick one option among vector of (positive) probabilities. |
---|
155 | |
---|
156 | |
---|
157 | <a name="method11"></a> |
---|
158 | <p/><strong>bool Rndm::dumpState(string fileName) </strong> <br/> |
---|
159 | save the current state of the random number generator to a binary |
---|
160 | file. This involves two integers and 100 double-precision numbers. |
---|
161 | Intended for debug purposes. Note that binary files may be |
---|
162 | platform-dependent and thus not transportable. |
---|
163 | |
---|
164 | |
---|
165 | <a name="method12"></a> |
---|
166 | <p/><strong>bool Rndm::readState(string fileName) </strong> <br/> |
---|
167 | set the state of the random number generator by reading in a binary |
---|
168 | file saved by the above command. Comments as above. |
---|
169 | |
---|
170 | |
---|
171 | <a name="method13"></a> |
---|
172 | <p/><strong>virtual double RndmEngine::flat() </strong> <br/> |
---|
173 | if you want to construct an external random number generator |
---|
174 | (or generator interface) then you must implement this method |
---|
175 | in your class derived from the <code>RndmEningen</code> base class, |
---|
176 | to give a random number between 0 and 1. |
---|
177 | |
---|
178 | |
---|
179 | </body> |
---|
180 | </html> |
---|
181 | |
---|
182 | <!-- Copyright (C) 2012 Torbjorn Sjostrand --> |
---|