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 SST_CORE_RNG_MARSAGLIA_H 00013 #define SST_CORE_RNG_MARSAGLIA_H 00014 #include <sst/core/sst_types.h> 00015 00016 #include <stdint.h> 00017 #include <sys/time.h> 00018 00019 #include "sstrng.h" 00020 00021 #define MARSAGLIA_UINT32_MAX 4294967295U 00022 #define MARSAGLIA_UINT64_MAX 18446744073709551615ULL 00023 #define MARSAGLIA_INT32_MAX 2147483647L 00024 #define MARSAGLIA_INT64_MAX 9223372036854775807LL 00025 00026 namespace SST { 00027 namespace RNG { 00028 /** 00029 \class MarsagliaRNG marsaglia.h "sst/core/rng/marsaglia.h" 00030 00031 Implements a random number generator using the Marsaglia method. This method is 00032 computationally cheap and provides a reasonable distribution of random numbers. If 00033 you need additional strength in the random numbers you may want to consider the 00034 Mersenne RNG. 00035 00036 For more information see the Multiply-with-carry Random Number Generator article 00037 at Wikipedia (http://en.wikipedia.org/wiki/Multiply-with-carry). 00038 */ 00039 class MarsagliaRNG : public SSTRandom { 00040 00041 public: 00042 /** 00043 Creates a new Marsaglia RNG using the initial seeds. 00044 @param[in] initial_z One of the random seed pairs 00045 @param[in] initial_w One of the random seed pairs. 00046 */ 00047 MarsagliaRNG(unsigned int initial_z, 00048 unsigned int initial_w); 00049 00050 /** 00051 Creates a new Marsaglia RNG using random initial seeds (which are 00052 read from the system clock). Note that these will create variation 00053 between runs and between platforms. 00054 */ 00055 MarsagliaRNG(); 00056 00057 /** 00058 Restart the random number generator with new seeds 00059 @param[in] new_z A new Z-seed 00060 @param[in] new_w A new W-seed 00061 */ 00062 void restart(unsigned int new_z, unsigned int new_w); 00063 00064 /** 00065 Generates the next random number as a double in the range 0 to 1. 00066 */ 00067 double nextUniform(); 00068 00069 /** 00070 Generates the next random number as an unsigned 32-bit integer. 00071 */ 00072 uint32_t generateNextUInt32(); 00073 00074 /** 00075 Generates the next random number as an unsigned 64-bit integer. 00076 */ 00077 uint64_t generateNextUInt64(); 00078 00079 /** 00080 Generates the next number as a signed 64-bit integer. 00081 */ 00082 int64_t generateNextInt64(); 00083 00084 /** 00085 Generates the next number as a signed 32-bit integer. 00086 */ 00087 int32_t generateNextInt32(); 00088 00089 private: 00090 /** 00091 Generates the next random number 00092 */ 00093 unsigned int generateNext(); 00094 00095 /** 00096 The Z seed of the Marsaglia generator 00097 */ 00098 unsigned int m_z; 00099 00100 /** 00101 The W seed of the Marsaglia generator 00102 */ 00103 unsigned int m_w; 00104 00105 }; 00106 00107 } //namespace RNG 00108 } //namespace SST 00109 00110 #endif //SST_CORE_RNG_MARSAGLIA_H