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
19namespace faudes {
20
21/**
22
23@defgroup SimRandomVariables Random Variables
24
25@ingroup SimulatorPlugin
26
27Sampling or evaluating random variables for simulation
28
29This module implements the evaluation (also known as sampling) of random
30variables with various distributions. It specialises on restricted support
31PDFs, since this is required for the ProposingExecutor.
32
33Random variables and their simulation is a highly involved topic and we give credit
34to the sources from which this module stems:
35
361)
37Implementation of a random number generator from Stave Park and Dave Geyer, which
38we use in original form up to minor cosmetic changes.
39
402)
41Inverse gaussian CDF by rational approxomation coefficients, presumably by Peter J, Acjlam,
42which we use in its original form up to minor cosmetic changes.
43
44
453)
46Gaussian CDF by an aproximation that we found in "Handbook of Mathematical Functions" by
47Abromowitz and Stegun.
48
49All sources were available freely and we did not find any restricting licensing terms.
50Thanks!
51
52
53---------------------------------------------------------------------
54
55Regarding 1), from the header of rngs.c
56
57This is an ANSI C library for multi-stream random number generation.
58The use of this library is recommended as a replacement for the ANSI C
59rand() and srand() functions, particularly in simulation applications
60where the statistical 'goodness' of the random number generator is
61important. The library supplies 256 streams of random numbers; use
62SelectStream(s) to switch between streams indexed s = 0,1,...,255.
63
64The streams must be initialized. The recommended way to do this is by
65using the function PlantSeeds(x) with the value of x used to initialize
66the default stream and all other streams initialized automatically with
67values dependent on the value of x. The following convention is used
68to initialize the default stream: \n
69if x > 0 then x is the state \n
70if x < 0 then the state is obtained from the system clock \n
71if x = 0 then the state is to be supplied interactively. \n
72
73The generator used in this library is a so-called 'Lehmer random number
74generator' which returns a pseudo-random number uniformly distributed
750.0 and 1.0. The period is (m - 1) where m = 2,147,483,647 and the
76smallest and largest possible values are (1 / m) and 1 - (1 / m)
77respectively. 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
83Name : rngs.c (Random Number Generation - Multiple Streams) \n
84Authors : Steve Park & Dave Geyer \n
85Language : ANSI C \n
86Latest Revision : 09-22-98
87
88---------------------------------------------------------------------
89
90Regarding 2), from the header of rngs.c
91
92This function returns an approximation of the inverse cumulative
93standard normal distribution function. I.e., given P, it returns
94an approximation to the X satisfying P = Pr{Z <= X} where Z is a
95random variable from the standard normal distribution.
96
97The algorithm uses a minimax approximation by rational functions
98and the result has a relative error whose absolute value is less
99than 1.15e-9.
100
101Author: Peter J. Acklam \n
102Time-stamp: 2002-06-09 18:45:44 +0200 \n
103E-mail: jacklam at math dot uio dor no \n
104WWW URL: http www dot math dot uio dot no /~jacklam \n
105
106C implementation adapted from Peter's Perl version \n
107
108---------------------------------------------------------------------
109
110Regarding 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 */
125void 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 */
131void ran_select_stream(int index);
132
133/**
134* Put a seed
135* @param seed
136* Random generator seed
137*/
138void ran_put_seed(long seed);
139
140/**
141* Initialize random generator
142* @param seed
143* Random generator seed
144*/
145void 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*/
156double 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*/
169double 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*/
181long 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*/
191double 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*/
203double 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*/
217double 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*/
226double ran_gaussian_cdf_P(double x);
227
228/** @} doxygen group */
229
230
231} // namespace
232
233
234#define FAUDES_STOCHRAN_H
235#endif
double ran_gauss(double mu, double sigma, Time::Type tossLB, Time::Type tossUB)
long ran_uniform_int(long a, long b)
Definition sp_random.cpp:98
void ran_put_seed(long seed)
Definition sp_random.cpp:55
double ran_exponential(double mu)
void ran_plant_seeds(long x)
Definition sp_random.cpp:35
void ran_select_stream(int index)
Definition sp_random.cpp:60
void ran_init(long seed)
Definition sp_random.cpp:67
double ran(void)
Definition sp_random.cpp:75
double ran_gaussian_cdf_P(double x)
double ran_uniform(double a, double b)
Definition sp_random.cpp:91

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen