-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRunif.c
149 lines (122 loc) · 2.67 KB
/
LRunif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*!
\file LRunif.c
\brief The uniform distribution on the [a,b) interval
The pseudo-random numbers are uniformly distributed on an interval
from the lower bound "a" upto "b" - the interval [a,b).
\manonly
PDF u(x) = { 0 if x < a || x >= b
{ 1/(b-a) if a <= x < b
CDF U(x) = { 0 if x < a
{ (x-a)/(b-a) if a <= x < b
{ 1 if b <= x
\endmanonly
\f{eqnarray*}{
\mbox{PDF } u(x) &=
\left\{ \begin{array}{ll}
0, & x < a \mbox{ or } x \ge b, \\
\frac{1}{b-a}, & a \le x < b , \\
\end{array} \right.
\\
\mbox{CDF } U(x) &=
\left\{ \begin{array}{ll}
0, & x < a , \\
\frac{x-a}{b-a}, & a \le x < b , \\
1, & x \ge b .
\end{array} \right.
\f}
Default:
- \e a = 0
- \e b = 1
\see urand/urand.c
*/
/*
* Copyright 2019 R.K. Owen, Ph.D.
* License see lgpl.md (Gnu Lesser General Public License)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "libran.h"
/*!
@brief LRd_unif_RAN(LR_obj *o) - double random uniform distribution
@param o LR_obj object
@return double
*/
double LRd_unif_RAN(LR_obj *o) {
double x, diff = (o->b.d - o->a.d);
x = o->ud(o);
return o->a.d + x * diff;
}
/*!
@brief LRd_unif_PDF(LR_obj *o, double x) - double uniform probablity distribution function
@param o LR_obj object
@param x value
@return double PDF at x
*/
double LRd_unif_PDF(LR_obj *o, double x) {
double diff = (o->b.d - o->a.d);
if (x < o->a.d || x >= o->b.d) {
return 0.0;
} else {
return 1.0 / diff;
}
}
/*!
@brief LRd_unif_CDF(LR_obj *o, double x) - double uniform cumulative distribution function
@param o LR_obj object
@param x value
@return double CDF at x
*/
double LRd_unif_CDF(LR_obj *o, double x) {
double diff = (o->b.d - o->a.d);
if (x < o->a.d) {
return 0.0;
} else if (x >= o->b.d) {
return 1.0;
} else {
return (x - o->a.d) / diff;
}
}
/*!
@brief LRf_unif_RAN(LR_obj *o) - float random uniform distribution
@param o LR_obj object
@return float
*/
float LRf_unif_RAN(LR_obj *o) {
float x, diff = (o->b.f - o->a.f);
x = o->uf(o);
return o->a.f + x * diff;
}
/*!
@brief LRf_unif_PDF(LR_obj *o, float x) - float uniform probablity distribution function
@param o LR_obj object
@param x value
@return float PDF at x
*/
float LRf_unif_PDF(LR_obj *o, float x) {
float diff = (o->b.f - o->a.f);
if (x < o->a.f || x >= o->b.f) {
return 0.0;
} else {
return 1.0 / diff;
}
}
/*!
@brief LRf_unif_CDF(LR_obj *o, float x) - float uniform cumulative distribution function
@param o LR_obj object
@param x value
@return float CDF at x
*/
float LRf_unif_CDF(LR_obj *o, float x) {
float diff = (o->b.f - o->a.f);
if (x < o->a.f) {
return 0.0;
} else if (x >= o->b.f) {
return 1.0;
} else {
return (x - o->a.f) / diff;
}
}
#ifdef __cplusplus
}
#endif