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 namespace tools {
0030
0031 class rtausmeui {
0032 public:
0033 rtausmeui(unsigned int a_seed = 1):m_seed(0),m_seed1(0),m_seed2(0){
0034 set_seed(a_seed);
0035 }
0036 virtual ~rtausmeui(){
0037 }
0038 public:
0039 rtausmeui(const rtausmeui& a_from):m_seed(a_from.m_seed),m_seed1(a_from.m_seed1),m_seed2(a_from.m_seed2){
0040 }
0041 rtausmeui& operator=(const rtausmeui& a_from) {
0042 m_seed = a_from.m_seed;
0043 m_seed1 = a_from.m_seed1;
0044 m_seed2 = a_from.m_seed2;
0045 return *this;
0046 }
0047 public:
0048 void set_seed(unsigned int a_seed) {
0049 m_seed = a_seed?a_seed:1;
0050
0051 // Generate m_seed[1,2] needed for the generator state using
0052 // a linear congruential generator
0053 // The only condition, stated at the end of the 1999 L'Ecuyer paper is that the seeds
0054 // must be greater than 1,7 and 15.
0055
0056 m_seed = LCG(m_seed);
0057 if (m_seed < 2) m_seed += 2UL;
0058 m_seed1 = LCG(m_seed);
0059 if (m_seed1 < 8) m_seed1 += 8UL;
0060 m_seed2 = LCG(m_seed1);
0061 if (m_seed2 < 16) m_seed2 += 16UL;
0062
0063 // "warm it up" by calling it 6 times
0064 for (unsigned int i = 0; i < 6; ++i) shoot();
0065 }
0066 unsigned int seed() const {return m_seed;}
0067
0068 unsigned int shoot() {
0069 // TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers
0070 // Use a mask of 0xffffffffUL to make in work on 64 bit machines
0071 // Periodicity of about 10**26
0072
0073 unsigned int y;
0074
0075 do {
0076 m_seed = TAUSWORTHE (m_seed, 13, 19, 4294967294UL, 12);
0077 m_seed1 = TAUSWORTHE (m_seed1, 2, 25, 4294967288UL, 4);
0078 m_seed2 = TAUSWORTHE (m_seed2, 3, 11, 4294967280UL, 17);
0079
0080 y = m_seed ^ m_seed1 ^ m_seed2;
0081 } while(!y);
0082
0083 return y;
0084 }
0085 protected:
0086 static unsigned int LCG(unsigned int a_n) {
0087 return ((69069 * a_n) & 0xffffffffUL); // linear congurential generator
0088 }
0089 static unsigned int TAUSWORTHE(unsigned int a_s,unsigned int a_a,unsigned int a_b,unsigned int a_c,unsigned int a_d) {
0090 return (((a_s & a_c) << a_d) & 0xffffffffUL ) ^ ((((a_s << a_a) & 0xffffffffUL )^ a_s) >> a_b);
0091 }
0092 protected:
0093 unsigned int m_seed;
0094 unsigned int m_seed1;
0095 unsigned int m_seed2;
0096 };
0097
0098 }
0099
0100 #endif