sp_random.h
Go to the documentation of this file.
1 /** @file sp_random.h Evaluating random variables */
2 
3 /*
4  FAU Discrete Event System Simulator
5 
6  Copyright (C) 2007 Christoph Doerr
7  Exclusive copyright is granted to Thomas Moor
8 
9 */
10 
11 #ifndef FAUDES_SP_RANDOM_H
12 #define FAUDES_SP_RANDOM_H
13 
14 #include "tp_timeinterval.h"
15 #include "sp_executor.h"
16 
17 
18 namespace faudes {
19 
20 /**
21 
22 @defgroup SimRandomVariables Random Variables
23 
24 @ingroup SimulatorPlugin
25 
26 Sampling or evaluating random variables for simulation
27 
28 This module implements the evaluation (also known as sampling) of random
29 variables with various distributions. It specialises on restricted support
30 PDFs, since this is required for the ProposingExecutor.
31 
32 Random variables and their simulation is a highly involved topic and we give credit
33 to the sources from which this module stems:
34 
35 1)
36 Implementation of a random number generator from Stave Park and Dave Geyer, which
37 we use in original form up to minor cosmetic changes.
38 
39 2)
40 Inverse gaussian CDF by rational approxomation coefficients, presumably by Peter J, Acjlam,
41 which we use in its original form up to minor cosmetic changes.
42 
43 
44 3)
45 Gaussian CDF by an aproximation that we found in "Handbook of Mathematical Functions" by
46 Abromowitz and Stegun.
47 
48 All sources were available freely and we did not find any restricting licensing terms.
49 Thanks!
50 
51 
52 ---------------------------------------------------------------------
53 
54 Regarding 1), from the header of rngs.c
55 
56 This is an ANSI C library for multi-stream random number generation.
57 The use of this library is recommended as a replacement for the ANSI C
58 rand() and srand() functions, particularly in simulation applications
59 where the statistical 'goodness' of the random number generator is
60 important. The library supplies 256 streams of random numbers; use
61 SelectStream(s) to switch between streams indexed s = 0,1,...,255.
62 
63 The streams must be initialized. The recommended way to do this is by
64 using the function PlantSeeds(x) with the value of x used to initialize
65 the default stream and all other streams initialized automatically with
66 values dependent on the value of x. The following convention is used
67 to initialize the default stream: \n
68 if x > 0 then x is the state \n
69 if x < 0 then the state is obtained from the system clock \n
70 if x = 0 then the state is to be supplied interactively. \n
71 
72 The generator used in this library is a so-called 'Lehmer random number
73 generator' which returns a pseudo-random number uniformly distributed
74 0.0 and 1.0. The period is (m - 1) where m = 2,147,483,647 and the
75 smallest and largest possible values are (1 / m) and 1 - (1 / m)
76 respectively. For more details see:
77 
78  "Random Number Generators: Good Ones Are Hard To Find" \n
79  Steve Park and Keith Miller \n
80  Communications of the ACM, October 1988 \n
81 
82 Name : rngs.c (Random Number Generation - Multiple Streams) \n
83 Authors : Steve Park & Dave Geyer \n
84 Language : ANSI C \n
85 Latest Revision : 09-22-98
86 
87 ---------------------------------------------------------------------
88 
89 Regarding 2), from the header of rngs.c
90 
91 This function returns an approximation of the inverse cumulative
92 standard normal distribution function. I.e., given P, it returns
93 an approximation to the X satisfying P = Pr{Z <= X} where Z is a
94 random variable from the standard normal distribution.
95 
96 The algorithm uses a minimax approximation by rational functions
97 and the result has a relative error whose absolute value is less
98 than 1.15e-9.
99 
100 Author: Peter J. Acklam \n
101 Time-stamp: 2002-06-09 18:45:44 +0200 \n
102 E-mail: jacklam at math dot uio dor no \n
103 WWW URL: http www dot math dot uio dot no /~jacklam \n
104 
105 C implementation adapted from Peter's Perl version \n
106 
107 ---------------------------------------------------------------------
108 
109 Regarding 3), found as code example in Wikipedia
110 
111 ---------------------------------------------------------------------
112 
113 
114 */
115 
116 /**
117  * Use this function to set the state of all the random number generator
118  * streams by "planting" a sequence of states (seeds), one per stream,
119  * with all states dictated by the state of the default stream.
120  * The sequence of planted states is separated one from the next by
121  * 8,367,782 calls to ran().
122  */
123 void ran_plant_seeds(long x);
124 
125 /**
126  * Use this function to set the current random number generator
127  * stream -- that stream from which the next random number will come.
128  */
129 void ran_select_stream(int index);
130 
131 /**
132 * Put a seed
133 * @param seed
134 * Random generator seed
135 */
136 void ran_put_seed(long seed);
137 
138 /**
139 * Initialize random generator
140 * @param seed
141 * Random generator seed
142 */
143 void ran_init(long seed);
144 
145 /**
146 * Run random generator
147 * Random Number Generator
148 * (for more details see "Random Number Generators: Good Ones Are Hard To Find"
149 * Steve Park and Keith Miller
150 * Communications of the ACM, October 1988)
151 * @return
152 * Random value in [0,1) ( excluding 1 (?))
153 */
154 double ran(void);
155 
156 
157 /**
158 * Sample a random variable uniformly on interval [a;b)
159 * Distribution: f(t) dt= {1/(b-a)} dt for t, a <=t< b, else 0
160 * @param a
161 * Lower bound
162 * @param b
163 * Upper bound
164 * @return
165 * Random value
166 */
167 double ran_uniform(double a, double b);
168 
169 /**
170 * Sample a discrete random variable uniformly on interval [a;b)
171 * Distribution: p(n) = 1/(b-a-1)
172 * @param a
173 * Lower bound
174 * @param b
175 * Upper bound
176 * @return
177 * Random value
178 */
179 long ran_uniform_int(long a, long b);
180 
181 /**
182 * Sample a random variable exponentially
183 * Distribution: f(t) dt = 1/mu exp(-t/mu) dt for t>=0
184 * @param mu
185 * mu
186 * @return
187 * Random variabe
188 */
189 double ran_exponential(double mu);
190 
191 /**
192 * Sample a random variable exponentially on a restricted interval
193 * Distribution: f(t) dt = 1/mu exp(-t/mu) dt for t>=0
194 * @param mu
195 * mu
196 * @param tossLB
197 * Lower interval bound
198 * @param tossUB
199 * Upper interval bound
200 */
201 double ran_exponential(double mu, Time::Type tossLB, Time::Type tossUB);
202 
203 /**
204 * Sample a random variable gaussian distributed on a restricted interval
205 * Distribution: f(t) = 1 / sqrt(2 pi sigma^2) * exp( -1/2 ((t-mu)/sigma)^2) for t>=0
206 * @param mu
207 * mu
208 * @param sigma
209 * sigma
210 * @param tossLB
211 * Lower interval bound
212 * @param tossUB
213 * Upper interval bound
214 */
215 double ran_gauss(double mu, double sigma, Time::Type tossLB, Time::Type tossUB);
216 
217 /**
218 * Help function: calculate gaussian CDF
219 * using an approximation from
220 * Abromowitz and Stegun: Handbook of Mathematical Functions
221 * @param x
222 * @return CDF(x)
223 */
224 double ran_gaussian_cdf_P(double x);
225 
226 /** @} doxygen group */
227 
228 
229 } // namespace
230 
231 
232 #define FAUDES_STOCHRAN_H
233 #endif

libFAUDES 2.28a --- 2016.09.13 --- c++ api documentaion by doxygen