27 class uniformRandomNumber;
28 class MersenneTwister;
29 class discretePoisson;
30 class discretePoisson1;
42 virtual double operator()() = 0;
55 #if defined(FASTBIT_USE_DEV_URANDOM)
56 FILE* fptr = fopen(
"/dev/urandom",
"rb");
58 int ierr = fread(&seed,
sizeof(seed), 1, fptr);
59 if (ierr < 1 || seed == 0)
63 #if defined(CLOCK_MONOTONIC) && !defined(__CYGWIN__)
65 if (0 == clock_gettime(CLOCK_MONOTONIC, &tb))
66 seed = (tb.tv_sec ^ tb.tv_nsec);
74 #if defined(CLOCK_MONOTONIC) && !defined(__CYGWIN__)
76 if (0 == clock_gettime(CLOCK_MONOTONIC, &tb))
77 seed = (tb.tv_sec ^ tb.tv_nsec);
93 long nextLong() {
return next();}
94 float nextFloat() {
return 2.3283064365386962890625e-10*
next();}
95 double nextDouble() {
return 2.3283064365386962890625e-10*
next();}
97 unsigned next(
unsigned r) {
return static_cast<unsigned>(r*nextDouble());}
101 for (
int i=0; i<624; i++) {
102 mt[i] = seed & 0xffff0000;
103 seed = 69069 * seed + 1;
104 mt[i] |= (seed & 0xffff0000) >> 16;
105 seed = 69069 * seed + 1;
114 static unsigned mag01[2]={0x0, 0x9908b0df};
117 for (kk=0; kk<227; kk++) {
118 y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
119 mt[kk] = mt[kk+397] ^ (y >> 1) ^ mag01[y & 0x1];
121 for (; kk<623; kk++) {
122 y = (mt[kk]&0x80000000)|(mt[kk+1]&0x7fffffff);
123 mt[kk] = mt[kk-227] ^ (y >> 1) ^ mag01[y & 0x1];
125 y = (mt[623]&0x80000000)|(mt[0]&0x7fffffff);
126 mt[623] = mt[396] ^ (y >> 1) ^ mag01[y & 0x1];
132 y ^= (y << 7) & 0x9d2c5680;
133 y ^= (y << 15) & 0xefc60000;
153 double operator()() {
return next();}
154 double next() {
return -log(urand());}
157 uniformRandomNumber& urand;
167 : urand(ur), has_extra(false), extra(0.0) {}
177 double v1, v2, r, fac;
179 v1 = 2.0 * urand() - 1.0;
180 v2 = 2.0 * urand() - 1.0;
181 r = v1 * v1 + v2 * v2;
182 }
while (r >= 1.0 || r <= 0.0);
183 fac = sqrt((-2.0 * log(r))/r);
208 return (exp(-log(1 - urand())/(alpha)) - 1);
210 return (1.0 / (1.0 - urand()) - 1.0);
223 const double lam=1.0,
long m=0)
224 : min0(m), lambda(lam), urand(ur) {init();}
226 long operator()() {
return next();}
232 x = - lambda * log(u * laminv);
233 k =
static_cast<long>(x + 0.5);
234 if (k <= k0 && k-x <= xm)
236 else if (u >= -exp(laminv*k+laminv2)*lambda-exp(laminv*k))
244 double lambda, laminv, laminv2, xm, ym;
249 if (! (lambda > DBL_MIN))
251 laminv = -1.0 / lambda;
252 laminv2 = 0.5*laminv;
253 k0 =
static_cast<long>(1.0+min0+1.0/(1.0-exp(laminv)));
254 ym = -exp((min0+0.5)*laminv)*lambda - exp(min0*laminv);
255 xm = min0 - log(ym*laminv);
263 : urand(ur) {init();}
265 long operator()() {
return next();}
272 k =
static_cast<long>(x + 0.5);
273 if (k <= k0 && k-x <= xm)
275 else if (u >= -exp(-static_cast<double>(k)-0.5) -
276 exp(-static_cast<double>(k)))
289 k0 =
static_cast<long>(1.0+1.0/(1.0-exp(-1.0)));
290 ym = - exp(-0.5) - 1.0;
302 unsigned long imax = ULONG_MAX)
303 : urand(ur), max0(imax), alpha(a) {init();}
307 unsigned long next() {
310 double ur = (urand)();
313 unsigned long k =
static_cast<unsigned long>(0.5+x);
316 else if (ur >= H(0.5+k) - exp(-log(k+1.0)*alpha))
321 unsigned long k = ((long) (urand() * max0)) % max0;
322 double freq = pow((1.0+k), -alpha);
323 while (urand() >= freq) {
324 k = ((long) (urand() * max0)) % max0;
325 freq = pow((1.0+k), -alpha);
333 uniformRandomNumber& urand;
335 double alpha, alpha1, alphainv, hx0, hxm, ss;
338 double H(
double x) {
return (exp(alpha1*log(1.0+x)) * alphainv);}
339 double Hinv(
double x) {
return exp(alphainv*log(alpha1*x)) - 1.0;}
344 if (! (alpha >= 0.0))
349 alpha1 = 1.0 - alpha;
350 alphainv = 1.0 / alpha1;
352 hx0 = H(0.5) - 1.0 - hxm;
353 ss = 1 - Hinv(H(1.5)-exp(-alpha*log(2.0)));
370 unsigned long imax = ULONG_MAX) :
371 max0(imax), urand(ur) {init();}
375 unsigned long next() {
377 double ur = (urand)();
380 unsigned long k =
static_cast<unsigned long>(0.5+x);
383 else if (ur >= H(0.5+k) - 1.0/((1.0+x)*(1.0+x)))
392 uniformRandomNumber& urand;
395 double H(
double x) {
return -1.0 / (1.0 + x);}
396 double Hinv(
double x) {
return (- 1.0 / x) - 1.0;}
399 hx0 = - 5.0/3.0 - hxm;
400 ss = 1 - Hinv(H(1.5)-0.25);
411 card(imax+1), cpd(imax+1), urand(ur) {init();}
415 unsigned long next() {
416 double ur = (urand)();
417 if (ur <= cpd[0])
return 0;
419 unsigned long i, j, k;
426 else if (cpd[k] < ur)
440 const unsigned long card;
441 std::vector<double> cpd;
442 uniformRandomNumber& urand;
446 const unsigned n = cpd.size();
447 if (n < 2 || n > 1024*1024 || card != n)
448 throw "imax must be in [2, 1000000]";
451 for (
unsigned i = 1; i < n; ++i)
452 cpd[i] = cpd[i-1] + 1.0 / (1.0 + i);
453 double ss = 1.0 / cpd.back();
454 for (
unsigned i = 0; i < n; ++i)
458 #endif // IBIS_TWISTER_H
Continuous Gaussian distribution.
Definition: twister.h:163
Mersenne Twister.
Definition: twister.h:47
Specialized version of the Poisson distribution exp(-x).
Definition: twister.h:260
double operator()()
Operator that returns the next random numbers.
Definition: twister.h:169
A specialized version of the Zipf distribution f(x) = 1/(1+x)^2.
Definition: twister.h:367
MersenneTwister()
Constructor.
Definition: twister.h:53
randomPoisson(uniformRandomNumber &ur)
Constructor. Must be supplied with a uniform random number generator.
Definition: twister.h:152
MersenneTwister(unsigned seed)
Constructor. Uses a user specified integer as seed.
Definition: twister.h:87
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
Continuous Poisson distribution.
Definition: twister.h:149
randomZipf(uniformRandomNumber &ur, double a=1)
Constructor. Must be supplied with a uniform random number generator.
Definition: twister.h:202
unsigned long operator()()
Return a discrete random number in the range of [0, imax].
Definition: twister.h:306
Continuous Zipf distribution.
Definition: twister.h:199
randomGaussian(uniformRandomNumber &ur)
Constructor. Must be supplied with a uniform random number generator.
Definition: twister.h:166
Discrete random number with Poisson distribution exp(-x/lambda).
Definition: twister.h:220
unsigned next(unsigned r)
Return integers in the range of [0, r)
Definition: twister.h:97
unsigned next()
Generate the next random integer in the range of 0-(2^{32}-1).
Definition: twister.h:111
double operator()()
Operator that returns the next random number.
Definition: twister.h:204
unsigned long operator()()
Return a discrete random number in the range of [0, imax].
Definition: twister.h:374
int nextInt()
Next integer.
Definition: twister.h:92
virtual double operator()()
Return a floating-point value in the range of [0, 1).
Definition: twister.h:90
double next()
Next random number.
Definition: twister.h:206
double next()
Next random number.
Definition: twister.h:171
Discrete Zipf distribution.
Definition: twister.h:299
void setSeed(unsigned seed)
Initializing the array with a seed.
Definition: twister.h:100
unsigned long operator()()
Return a discrete random number in the range of [0, imax].
Definition: twister.h:414
A specialized case of the Zipf distribution f(x) = 1/(1+x).
Definition: twister.h:408