diff --git a/models/ecolab_model.cc b/models/ecolab_model.cc index 95bd75d..6d814bd 100644 --- a/models/ecolab_model.cc +++ b/models/ecolab_model.cc @@ -32,26 +32,6 @@ using namespace classdesc; // TODO - move this into main library namespace { - /* Rounding function, randomly round up or down, in the range 0..INT_MAX */ - inline int ROUND(double x) - { - double dum; - if (x<0) x=0; - if (x>INT_MAX-1) x=INT_MAX-1; - return std::fabs(std::modf(x,&dum)) > array_urand.rand() ? - (int)x+1 : (int)x; - } - - inline int ROUND(float x) {return ROUND(double(x));} - - template - inline array ROUND(const E& x) - { - array r(x.size()); - for (size_t i=0; istd::numeric_limits::max()-1) x=std::numeric_limits::max()-1; + return std::fabs(std::modf(x,&dum))*(rand.max()-rand.min()) > (rand()-rand.min()) ? + (int)x+1 : (int)x; + } + +//inline int ROUND(float x) {return ROUND(double(x));} + + template + inline array EcolabPoint::ROUND(const E& x) + { + array r(x.size()); + for (size_t i=0; i n(density); diff --git a/models/ecolab_model.h b/models/ecolab_model.h index 5741dc3..f918e2b 100644 --- a/models/ecolab_model.h +++ b/models/ecolab_model.h @@ -11,6 +11,7 @@ #include using classdesc::Object; +#include #include #include @@ -65,6 +66,10 @@ struct EcolabPoint void condense(const array& mask, size_t mask_true); array mutate(const array&); unsigned nsp() const; ///< number of living species in this cell + /// Rounding function, randomly round up or down, in the range 0..INT_MAX + int ROUND(double x); + template array ROUND(const E& x); + Exclude rand; // random number generator }; struct PanmicticModel: public ModelData, public EcolabPoint, public ecolab::Model @@ -72,6 +77,7 @@ struct PanmicticModel: public ModelData, public EcolabPoint, public ecolab::Mode ConnectionPlot connectionPlot; void updateConnectionPlot() {connectionPlot.update(density,interaction);} void makeConsistent() {ModelData::makeConsistent(density.size());} + void seed(double x) {rand.seed(x);} void generate(unsigned niter); void generate() {generate(1);} void condense(); @@ -93,6 +99,7 @@ class SpatialModel: public ModelData, public EcolabGraph, public eco } array nsp() const; void makeConsistent(); + void seed(double x) {forAll([=](EcolabCell& cell){cell.rand.seed(x);});} void generate(unsigned niter); void generate() {generate(1);} void condense();