00001 // Copyright 2009-2015 Sandia Corporation. Under the terms 00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. 00003 // Government retains certain rights in this software. 00004 // 00005 // Copyright (c) 2009-2015, Sandia Corporation 00006 // All rights reserved. 00007 // 00008 // This file is part of the SST software package. For license 00009 // information, see the LICENSE file in the top level directory of the 00010 // distribution. 00011 00012 #ifndef _H_SST_CORE_RNG_POISSON 00013 #define _H_SST_CORE_RNG_POISSON 00014 00015 #include "math.h" 00016 00017 #include "distrib.h" 00018 #include "mersenne.h" 00019 00020 using namespace SST::RNG; 00021 00022 namespace SST { 00023 namespace RNG { 00024 00025 /** 00026 \class SSTPoissonDistribution poisson.h "sst/core/rng/poisson.h" 00027 00028 Creates an Poisson distribution for use within SST. This distribution is the same across 00029 platforms and compilers. 00030 */ 00031 class SSTPoissonDistribution : public SSTRandomDistribution { 00032 00033 public: 00034 /** 00035 Creates an Poisson distribution with a specific lambda 00036 \param mn The lambda of the Poisson distribution 00037 */ 00038 SSTPoissonDistribution(const double mn) : 00039 SSTRandomDistribution(), lambda(mn) { 00040 00041 baseDistrib = new MersenneRNG(); 00042 deleteDistrib = true; 00043 } 00044 00045 /** 00046 Creates an Poisson distribution with a specific lambda and a base random number generator 00047 \param lambda The lambda of the Poisson distribution 00048 \param baseDist The base random number generator to take the distribution from. 00049 */ 00050 SSTPoissonDistribution(const double mn, SSTRandom* baseDist) : 00051 SSTRandomDistribution(), lambda(mn) { 00052 00053 baseDistrib = baseDist; 00054 deleteDistrib = false; 00055 } 00056 00057 /** 00058 Destroys the Poisson distribution 00059 */ 00060 ~SSTPoissonDistribution() { 00061 if(deleteDistrib) { 00062 delete baseDistrib; 00063 } 00064 } 00065 00066 /** 00067 Gets the next (random) double value in the distribution 00068 \return The next random double from the distribution 00069 */ 00070 double getNextDouble() { 00071 const double L = exp(-lambda); 00072 double p = 1.0; 00073 int k = 0; 00074 00075 do { 00076 k++; 00077 p *= baseDistrib->nextUniform(); 00078 } while(p > L); 00079 00080 return k - 1; 00081 } 00082 00083 /** 00084 Gets the lambda with which the distribution was created 00085 \return The lambda which the user created the distribution with 00086 */ 00087 double getLambda() { 00088 return lambda; 00089 } 00090 00091 protected: 00092 /** 00093 Sets the lambda of the Poisson distribution. 00094 */ 00095 const double lambda; 00096 /** 00097 Sets the base random number generator for the distribution. 00098 */ 00099 SSTRandom* baseDistrib; 00100 00101 /** 00102 Controls whether the base distribution should be deleted when this class is destructed. 00103 */ 00104 bool deleteDistrib; 00105 00106 }; 00107 00108 } 00109 } 00110 00111 #endif