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

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen