Warning, /include/Geant4/tools/rtausmeui is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_rtausmeui
0005 #define tools_rtausmeui
0006
0007 // G.Barrand : not so clear if 0 and tools::uint32_max() are included.
0008 // A simple program shows that "if(r.shoot()==tools::uint32_max())" shows hits.
0009 // (But we did not see hits for "if(r.shoot()==0)"). (See tools/tests/rand.cpp).
0010
0011 // tausme is for Tausworthe maxmally equidistributed.
0012
0013 // From logic and code of CERN-ROOT/TRandom2 class.
0014
0015 // Random number generator class based on the maximally quidistributed combined
0016 // Tausworthe generator by L'Ecuyer.
0017 //
0018 // The period of the generator is 2**88 (about 10**26) and it uses only 3 words
0019 // for the state.
0020 //
0021 // For more information see:
0022 // P. L'Ecuyer, Mathematics of Computation, 65, 213 (1996)
0023 // P. L'Ecuyer, Mathematics of Computation, 68, 225 (1999)
0024 //
0025 // The publication are available online at
0026 // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
0027 // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
0028
0029 #ifdef TOOLS_MEM
0030 #include "mem"
0031 #include "S_STRING"
0032 #endif
0033
0034 namespace tools {
0035
0036 class rtausmeui {
0037 #ifdef TOOLS_MEM
0038 TOOLS_SCLASS(tools::rtausmeui)
0039 #endif
0040 public:
0041 rtausmeui(unsigned int a_seed = 1):m_seed(0),m_seed1(0),m_seed2(0){
0042 #ifdef TOOLS_MEM
0043 mem::increment(s_class().c_str());
0044 #endif
0045 set_seed(a_seed);
0046 }
0047 virtual ~rtausmeui(){
0048 #ifdef TOOLS_MEM
0049 mem::decrement(s_class().c_str());
0050 #endif
0051 }
0052 public:
0053 rtausmeui(const rtausmeui& a_from):m_seed(a_from.m_seed),m_seed1(a_from.m_seed1),m_seed2(a_from.m_seed2){
0054 #ifdef TOOLS_MEM
0055 mem::increment(s_class().c_str());
0056 #endif
0057 }
0058 rtausmeui& operator=(const rtausmeui& a_from) {
0059 m_seed = a_from.m_seed;
0060 m_seed1 = a_from.m_seed1;
0061 m_seed2 = a_from.m_seed2;
0062 return *this;
0063 }
0064 public:
0065 void set_seed(unsigned int a_seed) {
0066 m_seed = a_seed?a_seed:1;
0067
0068 // Generate m_seed[1,2] needed for the generator state using
0069 // a linear congruential generator
0070 // The only condition, stated at the end of the 1999 L'Ecuyer paper is that the seeds
0071 // must be greater than 1,7 and 15.
0072
0073 m_seed = LCG(m_seed);
0074 if (m_seed < 2) m_seed += 2UL;
0075 m_seed1 = LCG(m_seed);
0076 if (m_seed1 < 8) m_seed1 += 8UL;
0077 m_seed2 = LCG(m_seed1);
0078 if (m_seed2 < 16) m_seed2 += 16UL;
0079
0080 // "warm it up" by calling it 6 times
0081 for (unsigned int i = 0; i < 6; ++i) shoot();
0082 }
0083 unsigned int seed() const {return m_seed;}
0084
0085 unsigned int shoot() {
0086 // TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers
0087 // Use a mask of 0xffffffffUL to make in work on 64 bit machines
0088 // Periodicity of about 10**26
0089
0090 unsigned int y;
0091
0092 do {
0093 m_seed = TAUSWORTHE (m_seed, 13, 19, 4294967294UL, 12);
0094 m_seed1 = TAUSWORTHE (m_seed1, 2, 25, 4294967288UL, 4);
0095 m_seed2 = TAUSWORTHE (m_seed2, 3, 11, 4294967280UL, 17);
0096
0097 y = m_seed ^ m_seed1 ^ m_seed2;
0098 } while(!y);
0099
0100 return y;
0101 }
0102 protected:
0103 static unsigned int LCG(unsigned int a_n) {
0104 return ((69069 * a_n) & 0xffffffffUL); // linear congurential generator
0105 }
0106 static unsigned int TAUSWORTHE(unsigned int a_s,unsigned int a_a,unsigned int a_b,unsigned int a_c,unsigned int a_d) {
0107 return (((a_s & a_c) << a_d) & 0xffffffffUL ) ^ ((((a_s << a_a) & 0xffffffffUL )^ a_s) >> a_b);
0108 }
0109 protected:
0110 unsigned int m_seed;
0111 unsigned int m_seed1;
0112 unsigned int m_seed2;
0113 };
0114
0115 }
0116
0117 #endif