Changeset 26603
- Timestamp:
- 11/10/21 19:55:28 (3 years ago)
- Location:
- issm/trunk-jpl/src/c
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Random/randomgenerator.cpp
r26602 r26603 11 11 #define M_PI 3.141592653589793238462643 12 12 13 rnd_uniform_distribution::rnd_uniform_distribution(){/*{{{*/ 13 namespace rnd{ 14 14 15 a = 1103515245; // BSD Formula 16 c = 12345; // BSD Formula 17 m = 2147483648; // BSD Formula 18 _seed = 0; 19 lbound = 0.0; 20 ubound = 1.0; 21 return; 22 } 23 /*}}}*/ 24 rnd_uniform_distribution::rnd_uniform_distribution(double lower,double upper){/*{{{*/ 15 uniform_distribution::uniform_distribution(){/*{{{*/ 25 16 26 a = 1103515245; // BSD Formula 27 c = 12345; // BSD Formula 28 m = 2147483648; // BSD Formula 29 _seed = 0; 30 lbound = lower; 31 ubound = upper; 32 return; 33 } 34 /*}}}*/ 35 rnd_uniform_distribution::~rnd_uniform_distribution(){} 36 void rnd_uniform_distribution::seed( unsigned int s ) { _seed = s; } 37 unsigned int rnd_uniform_distribution::get_seed() { return _seed; } 38 double rnd_uniform_distribution::generator() { 39 _seed = ( a * _seed + c ) % m ; 40 return (ubound-lbound)*(double) _seed/ m + lbound; 41 } 17 a = 1103515245; // BSD Formula 18 c = 12345; // BSD Formula 19 m = 2147483648; // BSD Formula 20 _seed = 0; 21 lbound = 0.0; 22 ubound = 1.0; 23 return; 24 } 25 /*}}}*/ 26 uniform_distribution::uniform_distribution(double lower,double upper){/*{{{*/ 27 28 a = 1103515245; // BSD Formula 29 c = 12345; // BSD Formula 30 m = 2147483648; // BSD Formula 31 _seed = 0; 32 lbound = lower; 33 ubound = upper; 34 return; 35 } 36 /*}}}*/ 37 uniform_distribution::~uniform_distribution(){} 38 void uniform_distribution::seed( unsigned int s ) { _seed = s; } 39 unsigned int uniform_distribution::get_seed() { return _seed; } 40 double uniform_distribution::generator() { 41 _seed = ( a * _seed + c ) % m ; 42 return (ubound-lbound)*(double) _seed/ m + lbound; 43 } 42 44 43 45 44 rnd_normal_distribution::rnd_normal_distribution(){/*{{{*/46 normal_distribution::normal_distribution(){/*{{{*/ 45 47 46 _seed = 0;47 mean = 0;48 sdev = 1.0;49 return;50 }51 /*}}}*/52 rnd_normal_distribution::rnd_normal_distribution(double m,double s){/*{{{*/48 _seed = 0; 49 mean = 0; 50 sdev = 1.0; 51 return; 52 } 53 /*}}}*/ 54 normal_distribution::normal_distribution(double m,double s){/*{{{*/ 53 55 54 _seed = 0; 55 mean = m; 56 sdev = s; 57 return; 58 } 56 _seed = 0; 57 mean = m; 58 sdev = s; 59 return; 60 } 61 /*}}}*/ 62 normal_distribution::~normal_distribution(){} 63 void normal_distribution::seed( unsigned int s ) { _seed = s; } 64 double normal_distribution::generator(){/*{{{*/ 65 66 rnd::uniform_distribution unifdistri; 67 unifdistri.seed(_seed); 68 69 double u1 = unifdistri.generator(); 70 double u2 = unifdistri.generator(); 71 72 double R = sqrt(-2*log(u1)); 73 double theta = 2*M_PI*u2; 74 75 seed(unifdistri.get_seed()); 76 77 return mean + sdev * (R*cos(theta)); 78 79 } 59 80 /*}}}*/ 60 rnd_normal_distribution::~rnd_normal_distribution(){}61 void rnd_normal_distribution::seed( unsigned int s ) { _seed = s; }62 double rnd_normal_distribution::generator(){/*{{{*/63 64 rnd_uniform_distribution unifdistri;65 unifdistri.seed(_seed);66 67 double u1 = unifdistri.generator();68 double u2 = unifdistri.generator();69 70 double R = sqrt(-2*log(u1));71 double theta = 2*M_PI*u2;72 73 seed(unifdistri.get_seed());74 75 return mean + sdev * (R*cos(theta));76 81 77 82 } 78 /*}}}*/ -
issm/trunk-jpl/src/c/shared/Random/randomgenerator.h
r26602 r26603 9 9 #define M_PI 3.141592653589793238462643 10 10 11 class rnd_uniform_distribution 12 { 11 namespace rnd{ 13 12 14 private: 15 int a; 16 int c; 17 unsigned int m; 18 unsigned _seed; 19 double lbound; 20 double ubound; 13 class uniform_distribution 14 { 21 15 22 public: 16 private: 17 int a; 18 int c; 19 unsigned int m; 20 unsigned _seed; 21 double lbound; 22 double ubound; 23 23 24 /*constructors, destructors: */ 25 rnd_uniform_distribution(); 26 rnd_uniform_distribution(double a_1,double a_2); 27 ~rnd_uniform_distribution(); 24 public: 28 25 29 void seed( unsigned int s ); 30 unsigned int get_seed(); 31 double generator(); 26 /*constructors, destructors: */ 27 uniform_distribution(); 28 uniform_distribution(double a_1,double a_2); 29 ~uniform_distribution(); 32 30 33 }; 31 void seed( unsigned int s ); 32 unsigned int get_seed(); 33 double generator(); 34 34 35 class rnd_normal_distribution 36 { 35 }; 37 36 38 private: 39 unsigned _seed; 40 double mean; 41 double sdev; 37 class normal_distribution 38 { 42 39 43 public: 40 private: 41 unsigned _seed; 42 double mean; 43 double sdev; 44 44 45 /*constructors, destructors: */ 46 rnd_normal_distribution(); 47 rnd_normal_distribution(double m,double s); 48 ~rnd_normal_distribution(); 45 public: 49 46 50 void seed( unsigned int s ); 51 double generator(); 47 /*constructors, destructors: */ 48 normal_distribution(); 49 normal_distribution(double m,double s); 50 ~normal_distribution(); 52 51 53 }; 52 void seed( unsigned int s ); 53 double generator(); 54 55 }; 56 57 } 54 58 55 59 #endif //ifndef _RANDOMGENERATOR_H_ -
issm/trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp
r26600 r26603 18 18 19 19 /*Define seed*/ 20 rnd _normal_distribution distribution;20 rnd::normal_distribution distribution; 21 21 if(seed<0){ 22 22 std::random_device rd;
Note:
See TracChangeset
for help on using the changeset viewer.