Changeset 26577 for issm/trunk-jpl/src/c/shared/Random/randomgenerator.cpp
- Timestamp:
- 11/09/21 12:50:43 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Random/randomgenerator.cpp
r26576 r26577 6 6 #include "./randomgenerator.h" 7 7 8 class uniform_distribution 9 { 8 #undef M_PI 9 #define M_PI 3.141592653589793238462643 10 10 11 };12 11 13 class normal_distribution 14 { 12 uniform_distribution_rnd::uniform_distribution_rnd(){/*{{{*/ 15 13 16 }; 14 a = 1103515245; // BSD Formula 15 c = 12345; // BSD Formula 16 m = 2147483648; // BSD Formula 17 _seed = 0; 18 lbound = 0.0; 19 ubound = 1.0; 20 return; 21 } 22 /*}}}*/ 23 uniform_distribution_rnd::uniform_distribution_rnd(double lower,double upper){/*{{{*/ 24 25 a = 1103515245; // BSD Formula 26 c = 12345; // BSD Formula 27 m = 2147483648; // BSD Formula 28 _seed = 0; 29 lbound = lower; 30 ubound = upper; 31 return; 32 } 33 /*}}}*/ 34 uniform_distribution_rnd::~uniform_distribution_rnd(){} 35 void uniform_distribution_rnd::seed( unsigned int s ) { _seed = s; } 36 unsigned int uniform_distribution_rnd::get_seed() { return _seed; } 37 double uniform_distribution_rnd::generator() { 38 _seed = ( a * _seed + c ) % m ; 39 return (ubound-lbound)*(double) _seed/ m + lbound; 40 } 41 42 43 normal_distribution_rnd::normal_distribution_rnd(){/*{{{*/ 44 45 _seed = 0; 46 mean = 0; 47 sdev = 1.0; 48 return; 49 } 50 /*}}}*/ 51 normal_distribution_rnd::normal_distribution_rnd(double m,double s){/*{{{*/ 52 53 _seed = 0; 54 mean = m; 55 sdev = s; 56 return; 57 } 58 /*}}}*/ 59 normal_distribution_rnd::~normal_distribution_rnd(){} 60 void normal_distribution_rnd::seed( unsigned int s ) { _seed = s; } 61 double normal_distribution_rnd::generator(){/*{{{*/ 62 63 uniform_distribution_rnd unifdistri; 64 unifdistri.seed(_seed); 65 66 double u1 = unifdistri.generator(); 67 double u2 = unifdistri.generator(); 68 69 double R = sqrt(-2*log(u1)); 70 double theta = 2*M_PI*u2; 71 72 seed(unifdistri.get_seed()); 73 74 return mean + sdev * (R*cos(theta)); 75 76 } 77 /*}}}*/
Note:
See TracChangeset
for help on using the changeset viewer.