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 lbound;
|
---|
20 | double ubound;
|
---|
21 |
|
---|
22 | int drnd() { return( _seed = ( a * _seed + c ) % m ); }
|
---|
23 |
|
---|
24 | public:
|
---|
25 |
|
---|
26 | /*constructors, destructors: */
|
---|
27 | rnd_uniform_distribution();
|
---|
28 | rnd_uniform_distribution(double a_1,double a_2);
|
---|
29 | ~rnd_uniform_distribution();
|
---|
30 |
|
---|
31 | void seed( unsigned int s ) { _seed = s; }
|
---|
32 | unsigned int get_seed() { return _seed; }
|
---|
33 | double generator() { return (ubound-lbound)*(double) drnd()/ m + lbound; }
|
---|
34 |
|
---|
35 | };
|
---|
36 |
|
---|
37 | class rnd_normal_distribution
|
---|
38 | {
|
---|
39 |
|
---|
40 | private:
|
---|
41 | unsigned _seed;
|
---|
42 | double mean;
|
---|
43 | double sdev;
|
---|
44 |
|
---|
45 | public:
|
---|
46 |
|
---|
47 | /*constructors, destructors: */
|
---|
48 | rnd_normal_distribution();
|
---|
49 | rnd_normal_distribution(double m,double s);
|
---|
50 | ~rnd_normal_distribution();
|
---|
51 |
|
---|
52 | void seed( unsigned int s ) { _seed = s; }
|
---|
53 | double generator()
|
---|
54 | {
|
---|
55 | rnd_uniform_distribution unifdistri;
|
---|
56 | unifdistri.seed(_seed);
|
---|
57 |
|
---|
58 | double u1 = unifdistri.generator();
|
---|
59 | double u2 = unifdistri.generator();
|
---|
60 |
|
---|
61 | double R = sqrt(-2*log(u1));
|
---|
62 | double theta = 2*M_PI*u2;
|
---|
63 |
|
---|
64 | seed(unifdistri.get_seed());
|
---|
65 |
|
---|
66 | return mean + sdev * (R*cos(theta));
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | };
|
---|
71 |
|
---|
72 | #endif //ifndef _RANDOMGENERATOR_H_
|
---|