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_UNIFORM 00013 #define _H_SST_CORE_RNG_UNIFORM 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 SSTUniformDistribution uniform.h "sst/core/rng/uniform.h" 00027 00028 Creates a Uniform distribution for use within SST. This distribution is the same across 00029 platforms and compilers. 00030 */ 00031 class SSTUniformDistribution : public SSTRandomDistribution { 00032 00033 public: 00034 /** 00035 Creates an uniform distribution with a specific number of bins 00036 \param probsCount Number of probability bins in this distribution 00037 */ 00038 SSTUniformDistribution(const uint32_t probsCount) : 00039 SSTRandomDistribution(), 00040 probCount(probsCount) { 00041 00042 baseDistrib = new MersenneRNG(); 00043 deleteDistrib = true; 00044 } 00045 00046 /** 00047 Creates a Uniform distribution with a specific number of bins and user supplied 00048 random number generaotr 00049 \param probsCount Number of probability bins in the distribution 00050 \param baseDist The base random number generator to take the distribution from. 00051 */ 00052 SSTUniformDistribution(const uint32_t probsCount, SSTRandom* baseDist) : 00053 SSTRandomDistribution(), 00054 probCount(probsCount) { 00055 00056 baseDistrib = baseDist; 00057 deleteDistrib = false; 00058 } 00059 00060 /** 00061 Destroys the distribution and will delete locally allocated RNGs 00062 */ 00063 ~SSTUniformDistribution() { 00064 if(deleteDistrib) { 00065 delete baseDistrib; 00066 } 00067 } 00068 00069 /** 00070 Gets the next (random) double value in the distribution 00071 \return The next random double from the distribution, this is the double converted of the index where the probability is located 00072 */ 00073 double getNextDouble() { 00074 const double nextD = baseDistrib->nextUniform(); 00075 const double probPerBin = 1.0 / (double)(probCount); 00076 00077 for(uint32_t i = 0; i < probCount; ++i) { 00078 if(nextD < ( ((double) i) * probPerBin )) { 00079 return i; 00080 } 00081 } 00082 00083 return probCount; 00084 } 00085 00086 protected: 00087 /** 00088 Sets the base random number generator for the distribution. 00089 */ 00090 SSTRandom* baseDistrib; 00091 00092 /** 00093 Controls whether the base distribution should be deleted when this class is destructed. 00094 */ 00095 bool deleteDistrib; 00096 00097 /** 00098 Count of discrete probabilities 00099 */ 00100 uint32_t probCount; 00101 00102 }; 00103 00104 } 00105 } 00106 00107 #endif