Changeset 26603


Ignore:
Timestamp:
11/10/21 19:55:28 (3 years ago)
Author:
bulthuis
Message:

CHG: create namespace for pseudo-random number generator

Location:
issm/trunk-jpl/src/c
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/shared/Random/randomgenerator.cpp

    r26602 r26603  
    1111#define M_PI 3.141592653589793238462643
    1212
    13 rnd_uniform_distribution::rnd_uniform_distribution(){/*{{{*/
     13namespace rnd{
    1414
    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(){/*{{{*/
    2516
    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        }
    4244
    4345
    44 rnd_normal_distribution::rnd_normal_distribution(){/*{{{*/
     46        normal_distribution::normal_distribution(){/*{{{*/
    4547
    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){/*{{{*/
    5355
    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        }
    5980        /*}}}*/
    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));
    7681
    7782}
    78 /*}}}*/
  • issm/trunk-jpl/src/c/shared/Random/randomgenerator.h

    r26602 r26603  
    99#define M_PI 3.141592653589793238462643
    1010
    11 class rnd_uniform_distribution
    12 {
     11namespace rnd{
    1312
    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  {
    2115
    22   public:
     16    private:
     17      int a;
     18      int c;
     19      unsigned int m;
     20      unsigned _seed;
     21      double lbound;
     22      double ubound;
    2323
    24     /*constructors, destructors: */
    25     rnd_uniform_distribution();
    26     rnd_uniform_distribution(double a_1,double a_2);
    27     ~rnd_uniform_distribution();
     24    public:
    2825
    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();
    3230
    33 };
     31      void seed( unsigned int s );
     32      unsigned int get_seed();
     33      double generator();
    3434
    35 class rnd_normal_distribution
    36 {
     35  };
    3736
    38   private:
    39     unsigned _seed;
    40     double mean;
    41     double sdev;
     37  class normal_distribution
     38  {
    4239
    43   public:
     40    private:
     41      unsigned _seed;
     42      double mean;
     43      double sdev;
    4444
    45     /*constructors, destructors: */
    46     rnd_normal_distribution();
    47     rnd_normal_distribution(double m,double s);
    48     ~rnd_normal_distribution();
     45    public:
    4946
    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();
    5251
    53 };
     52      void seed( unsigned int s );
     53      double generator();
     54
     55  };
     56
     57}
    5458
    5559#endif //ifndef _RANDOMGENERATOR_H_
  • issm/trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp

    r26600 r26603  
    1818
    1919        /*Define seed*/
    20         rnd_normal_distribution distribution;
     20        rnd::normal_distribution distribution;
    2121        if(seed<0){
    2222                std::random_device rd;
Note: See TracChangeset for help on using the changeset viewer.