source: issm/trunk-jpl/src/c/shared/Random/randomgenerator.h@ 26591

Last change on this file since 26591 was 26591, checked in by bulthuis, 3 years ago

BUG: Try to fix bugs pseudo-random number generators

  • Property svn:executable set to *
File size: 1.6 KB
Line 
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
11class 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 public:
23
24 /*constructors, destructors: */
25 rnd_uniform_distribution() : _seed( 0 ), a( 1103515245 ), c( 12345 ), m( 2147483648 ), lbound(0.0), ubound(1.0) {}
26 rnd_uniform_distribution(double a_1,double a_2) : _seed( 0 ), a( 1103515245 ), c( 12345 ), m( 2147483648 ), lbound(a_1), ubound(a_2) {}
27 ~rnd_uniform_distribution(){}
28 void seed( unsigned int s ) { _seed = s; }
29 unsigned int get_seed() { return _seed; }
30 double generator(){
31 _seed = ( a * _seed + c ) % m;
32 return (ubound-lbound)*(double) _seed/ m + lbound;
33 }
34
35};
36
37class rnd_normal_distribution
38{
39
40 private:
41 unsigned _seed;
42 double mean;
43 double sdev;
44
45 public:
46 rnd_normal_distribution() : _seed( 0 ), mean( 0), sdev(1.0) {}
47 rnd_normal_distribution(double m,double s) : _seed( 0 ), mean( m ), sdev(s) {}
48 ~rnd_normal_distribution(){}
49 void seed( unsigned int s ) { _seed = s; }
50 double generator()
51 {
52 rnd_uniform_distribution unifdistri;
53 unifdistri.seed(_seed);
54
55 double u1 = unifdistri.generator();
56 double u2 = unifdistri.generator();
57
58 double R = sqrt(-2*log(u1));
59 double theta = 2*M_PI*u2;
60
61 seed(unifdistri.get_seed());
62
63 return mean + sdev * (R*cos(theta));
64
65 }
66
67};
68
69#endif //ifndef _RANDOMGENERATOR_H_
Note: See TracBrowser for help on using the repository browser.