Changeset 26577


Ignore:
Timestamp:
11/09/21 12:50:43 (3 years ago)
Author:
bulthuis
Message:

BUG: Try to fix bugs on tests

Location:
issm/trunk-jpl/src/c/shared/Random
Files:
2 edited

Legend:

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

    r26576 r26577  
    66#include "./randomgenerator.h"
    77
    8 class uniform_distribution
    9 {
     8#undef M_PI
     9#define M_PI 3.141592653589793238462643
    1010
    11 };
    1211
    13 class normal_distribution
    14 {
     12uniform_distribution_rnd::uniform_distribution_rnd(){/*{{{*/
    1513
    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        /*}}}*/
     23uniform_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        /*}}}*/
     34uniform_distribution_rnd::~uniform_distribution_rnd(){}
     35void uniform_distribution_rnd::seed( unsigned int s ) { _seed = s; }
     36unsigned int uniform_distribution_rnd::get_seed() { return _seed; }
     37double uniform_distribution_rnd::generator() {
     38                _seed = ( a * _seed + c ) % m ;
     39                return (ubound-lbound)*(double) _seed/ m + lbound;
     40}
     41
     42
     43normal_distribution_rnd::normal_distribution_rnd(){/*{{{*/
     44
     45                _seed = 0;
     46                mean   = 0;
     47                sdev  = 1.0;
     48                return;
     49}
     50/*}}}*/
     51normal_distribution_rnd::normal_distribution_rnd(double m,double s){/*{{{*/
     52
     53                _seed = 0;
     54                mean   = m;
     55                sdev  = s;
     56                return;
     57}
     58        /*}}}*/
     59normal_distribution_rnd::~normal_distribution_rnd(){}
     60void normal_distribution_rnd::seed( unsigned int s ) { _seed = s; }
     61double 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        /*}}}*/
  • issm/trunk-jpl/src/c/shared/Random/randomgenerator.h

    r26576 r26577  
    1212{
    1313
    14   private:
     14    private:
    1515
    16     unsigned int a;
    17     unsigned int c;
    18     unsigned int m;
    19     unsigned _seed;
    20     double a1;
    21     double a2;
     16      unsigned int a;       //multiplier of the linear congruential generator
     17      unsigned int c;       //increment of the linear congruential generator
     18      unsigned int m;       // modulo of the linear congruential generator
     19      unsigned int _seed;   // seed value
     20      double lbound;        // lower bound of uniform distribution
     21      double ubound;        // upper bound of uniform distribution
    2222
    23     int drnd() { return( _seed = ( a * _seed + c ) % m ); }
     23    public:
    2424
    25   public:
     25      /*constructors, destructors: */
     26      uniform_distribution_rnd();
     27      uniform_distribution_rnd(double a_1, double a_2);
     28      ~uniform_distribution_rnd();
    2629
    27     /*constructors, destructors: */
    28     uniform_distribution_rnd();
    29     uniform_distribution_rnd(double a_1, double a_2);
    30     ~uniform_distribution_rnd();
    31 
    32     void seed( unsigned int s ) { _seed = s; }
    33     unsigned int get_seed() { return _seed; }
    34     double rnd() { return (a2-a1)*(double) drnd()/ m + a1; }
     30      void seed( unsigned int s );
     31      unsigned int get_seed();
     32      double generator();
    3533
    3634};
     
    3937{
    4038
    41   private:
    42     unsigned _seed;
    43     double mean;
    44     double sdev;
     39    private:
     40      unsigned int _seed; // seed value
     41      double mean;        // mean value
     42      double sdev;        // standard deviation
    4543
    46   public:
    47     normal_distribution_rnd() : _seed( 0 ), mean( 0), sdev(1.0) {}
    48     normal_distribution_rnd(double m,double s) : _seed( 0 ), mean( m ), sdev(s) {}
    49     void seed( unsigned int s ) { _seed = s; }
    50     double rnd();
     44    public:
     45
     46      /*constructors, destructors: */
     47      normal_distribution_rnd();
     48      normal_distribution_rnd(double m,double s);
     49      ~normal_distribution_rnd();
     50
     51      void seed( unsigned int s );
     52      double generator();
    5153
    5254};
Note: See TracChangeset for help on using the changeset viewer.