-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrandom.h
More file actions
231 lines (180 loc) · 4.47 KB
/
crandom.h
File metadata and controls
231 lines (180 loc) · 4.47 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Copyright (C) 2009 Alexander G. Pronchenkov. All rights reserved.
*
* The new BSD License is applied to this software.
*/
/* This code is based on the library rvgs.c (Steve Park & Dave Geyer) */
#ifndef __crandom_h__
#define __crandom_h__
#ifdef __cplusplus
extern "C" {
#endif
/**
* Interface for random variates generator form uniformly distribute
*/
struct cRandom {
/**
* Returns the next pseudorandom, uniformly distributed double value
* between 0.0 and 1.0 from this random number generator's sequence.
*
* Range: 0 <= x < 1
*/
double (* next)(void * that);
/**
* Releases resources of a cRandom object
*/
void (* release)(void * that);
};
/**
* Create a new cRandom object (dSFMT based)
*/
struct cRandom * dSFMTRandomNew(void);
/**
* Create a new cRandom object (dSFMT based)
*
* Initialize it by 32-bit integer.
*/
struct cRandom * dSFMTRandomNewBySeed(int seed);
/**
* Create a new cRandom object (dSFMT based)
*
* Initialize it by array.
*/
struct cRandom * dSFMTRandomNewByArray(int * array, int arrayLength);
/***********************
* With finite support *
***********************/
/**
* Returns 1 with probability p or 0 with probability 1 - p.
* NOTE: use 0.0 < p < 1.0
*
* Range: 0, 1
* Mean: p
* Variance: p * (1 - p)
*/
int bernoulli(struct cRandom * crandom, double p);
/**
* Returns a binomial distributed integer between 0 and n inclusive.
* NOTE: use n > 0 and 0.0 < p < 1.0
*
* Range: 0, ..., n
* Mean: n * p
* Variance: n * p * (1 - p)
*/
int binomial(struct cRandom * crandom, int n, double p);
/**
* Returns an discrete uniform distributed integer between a and b inclusive.
* NOTE: use a < b
*
* Range: a, ..., b
* Mean: (a + b) / 2
* Variance: (sqr(b - a + 1) - 1) / 12
*/
int equilikely(struct cRandom * crandom, int a, int b);
/**
* Returns a geometric distributed non-negative integer.
* NOTE: use 0.0 < p < 1.0
*
* Range: 0, ...
* Mean: p / (1 - p)
* Variance: p / sqr(1 - p)
*/
int geometric(struct cRandom * crandom, double p);
/**
* Returns a Pascal distributed non-negative integer.
* NOTE: use n > 0 and 0.0 < p < 1.0
*
* Range: 0, ...
* Mean: n * p / (1 - p)
* Variance: n * p / sqr(1 - p)
*/
int pascal(struct cRandom * crandom, int n, double p);
/**
* Returns a Poisson distributed non-negative integer.
* NOTE: use m > 0
*
* Range: 0, ...
* Mean: m
* Variance: m
*/
int Poisson(struct cRandom * crandom, double m);
/*************************
* With infinite support *
*************************/
/**
* Returns a uniformly distributed real number between a and b.
* NOTE: use a < b
*
* Range: a < x < b
* Mean: (a + b) / 2
* Variance: sqr(b - a) / 12
*/
double uniform(struct cRandom * crandom, double a, double b);
/**
* Returns an exponentially distributed positive real number.
* NOTE: use m > 0.0
*
* Range: 0 < x
* Mean: m
* Variance: sqr(m)
*/
double exponential(struct cRandom * crandom, double m);
/**
* Returns an Erlang distributed positive real number.
* NOTE: use n > 0 and b > 0.0
*
* Range: 0 < x
* Mean: n * b
* Variance: n * sqr(b)
*/
double erlang(struct cRandom * crandom, int n, double b);
/**
* Returns a normal (Gaussian) distributed real number.
* NOTE: use s > 0.0
*
* Range: all x
* Mean: m
* Variance: sqr(s)
*/
double normal(struct cRandom * crandom, double m, double s);
/**
* Returns a lognormal distributed positive real number.
* NOTE: use b > 0.0
*
* Range: 0 < x
* Mean: exp(a + sqr(b) / 2)
* Variance: (exp(sqr(b) - 1) * exp(2 * a + sqr(b))
*/
double lognormal(struct cRandom * crandom, double a, double b);
/**
* Returns a chi-square distributed positive real number.
* NOTE: use n > 0
*
* Range: 0 < x
* Mean: n
* Variance: 2 * n
*/
double chisquare(struct cRandom * crandom, int n);
/**
* Returns a student-t distributed real number.
* NOTE: use n > 0
*
* Range: all x
* Mean: 0 (when n > 1)
* Variance: n / (n - 2) (when n > 2)
*/
double student(struct cRandom * crandom, int n);
/**
* Returns a power-law distributed positive real number
* NOTE: use k < -1, 0 < c
* NOTE: Please attention, this distribution does not precisely defined
*
* Range: ((-k - 1) / c) ^ (k + 1) <= x
* Mean: existed (when k < -2)
* Variance: existed (when k < -2)
*/
double power_law(struct cRandom * crandom, double k, double c);
#ifdef __cplusplus
}
#endif
#endif /*_crandom_h__*/