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

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

NEW: add pseudo-random number generators + use in solutionsequence_sampling.cpp

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