1 | /*!\file: randomgenerator.h
|
---|
2 | * \brief prototypes for randomgenerator.h
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifndef _RANDOMGENERATOR_H_
|
---|
6 | #define _RANDOMGENERATOR_H_
|
---|
7 |
|
---|
8 | #undef M_PI
|
---|
9 | #define M_PI 3.141592653589793238462643
|
---|
10 |
|
---|
11 | class rnd_uniform_distribution
|
---|
12 | {
|
---|
13 |
|
---|
14 | private:
|
---|
15 | int a;
|
---|
16 | int c;
|
---|
17 | unsigned int m;
|
---|
18 | unsigned _seed;
|
---|
19 | double a1;
|
---|
20 | double a2;
|
---|
21 |
|
---|
22 | int drnd() { return( _seed = ( a * _seed + c ) % m ); }
|
---|
23 |
|
---|
24 | public:
|
---|
25 |
|
---|
26 | /*constructors, destructors: */
|
---|
27 | rnd_uniform_distribution() : _seed( 0 ), a( 1103515245 ), c( 12345 ), m( 2147483648 ), a1(0.0), a2(1.0) {}
|
---|
28 | rnd_uniform_distribution(double a_1,double a_2) : _seed( 0 ), a( 1103515245 ), c( 12345 ), m( 2147483648 ), a1(a_1), a2(a_2) {}
|
---|
29 | void seed( unsigned int s ) { _seed = s; }
|
---|
30 | unsigned int get_seed() { return _seed; }
|
---|
31 | double generator() { return (a2-a1)*(double) drnd()/ m + a1; }
|
---|
32 |
|
---|
33 | };
|
---|
34 |
|
---|
35 | class rnd_normal_distribution
|
---|
36 | {
|
---|
37 |
|
---|
38 | private:
|
---|
39 | unsigned _seed;
|
---|
40 | double mean;
|
---|
41 | double sdev;
|
---|
42 |
|
---|
43 | public:
|
---|
44 | rnd_normal_distribution() : _seed( 0 ), mean( 0), sdev(1.0) {}
|
---|
45 | rnd_normal_distribution(double m,double s) : _seed( 0 ), mean( m ), sdev(s) {}
|
---|
46 | void seed( unsigned int s ) { _seed = s; }
|
---|
47 | double generator()
|
---|
48 | {
|
---|
49 | rnd_uniform_distribution unifdistri;
|
---|
50 | unifdistri.seed(_seed);
|
---|
51 |
|
---|
52 | double u1 = unifdistri.generator();
|
---|
53 | double u2 = unifdistri.generator();
|
---|
54 |
|
---|
55 | double R = sqrt(-2*log(u1));
|
---|
56 | double theta = 2*M_PI*u2;
|
---|
57 |
|
---|
58 | seed(unifdistri.get_seed());
|
---|
59 |
|
---|
60 | return mean + sdev * (R*cos(theta));
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | };
|
---|
65 |
|
---|
66 | #endif //ifndef _RANDOMGENERATOR_H_
|
---|