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