NCBI C++ Toolkit Cross Reference

C++/src/util/random_gen.cpp


  1 /*  $Id: random_gen.cpp 103491 2007-05-04 17:18:18Z kazimird $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Authors: Clifford Clausen, Denis Vakatov, Jim Ostell, Jonathan Kans,
 27  *          Greg Schuler
 28  * Contact: Clifford Clausen
 29  *
 30  * File Description:
 31  *   CRandom is a lagged Fibonacci (LFG) random number generator (RNG)
 32  *   with lags 33 and 13, modulus 2^31, and operation '+'. It is a slightly
 33  *   modified version of Nlm_Random() found in the NCBI C toolkit.
 34  *   It generates uniform random numbers between 0 and 2^31 - 1 (inclusive).
 35  *
 36  *   CRandom has been tested using the Diehard RNG test package
 37  *   developed by George Marsaglia, Prof., Dept of Statistics, Florida
 38  *   State University. CRandom in particular, and LFG type RNGs in general, 
 39  *   cannot pass all of the Diehard RNG tests. Specifically, it fails the
 40  *   "Birthday" test as do other LFG RNGs. CRandom performs as well as
 41  *   other LFG RNGS, as provided in the Diehard test package. The LFG
 42  *   class of RNGs was chosen as the RNG for the NCBI C++ Toolkit as it
 43  *   provides the best tradeoff between time to generate a random number
 44  *   and performance on tests for randomness.
 45  *   
 46  *   For a download of Diehard software and documentation
 47  *   see http://stat.fsu.edu/~geo/diehard.html. 
 48  *
 49  *   For further information also see
 50  *   http://random.mat.sbg.ac.at/,  
 51  *   http://csep1.phy.ornl.gov/rn/rn.html, and
 52  *   http://www.agner.org/random/.
 53  *
 54  *   Some relevant papers are:
 55  *   1. Hellekalek, P.: "Inversive pseudorandom number generators: concepts
 56  *   results, and links", In Alexopoulos, C and Kang, K, and Lilegdon, WR,
 57  *   and Goldsman, D, editor(s), Proceedings of the 1995 Winter Simulation
 58  *   Conference, pp 255-262, 1995.
 59  *   2. Leeb, H: "Random Numbers for Computer Simulation", Master's thesis,
 60  *   University of Salzburg, 1995.
 61  *   3. Marsaglia, G., "A Current View of Random Number Generators",
 62  *   Proceedings of 16th Symposium on the Interface, Atlanta, 1984, Elsevier
 63  *   Press.
 64  *   4. Marsaglia, G. "Monkey Tests for Random Number Generators", Computers
 65  *   & Mathematics with Applications, Vol 9, pp. 1-10, 1993.
 66  *
 67  *   For a list of other published papers, see
 68  *   http://random.mat.sbg.ac.at/literature and
 69  *   http://www.evensen.org/marsaglia/.
 70  *
 71  *   class CRandom:: 
 72  */
 73 
 74 #include <ncbi_pch.hpp>
 75 #include <util/random_gen.hpp>
 76 
 77 
 78 BEGIN_NCBI_SCOPE
 79 
 80 
 81 const size_t CRandom::kStateSize = sizeof(CRandom::sm_State)
 82     / sizeof(CRandom::sm_State[0]);
 83 
 84 const size_t kStateOffset = 12;
 85 
 86 const CRandom::TValue CRandom::sm_State[kStateSize] = {
 87     0xd53f1852,  0xdfc78b83,  0x4f256096,  0xe643df7,
 88     0x82c359bf,  0xc7794dfa,  0xd5e9ffaa,  0x2c8cb64a,
 89     0x2f07b334,  0xad5a7eb5,  0x96dc0cde,  0x6fc24589,
 90     0xa5853646,  0xe71576e2,  0xdae30df,   0xb09ce711,
 91     0x5e56ef87,  0x4b4b0082,  0x6f4f340e,  0xc5bb17e8,
 92     0xd788d765,  0x67498087,  0x9d7aba26,  0x261351d4,
 93     0x411ee7ea,  0x393a263,   0x2c5a5835,  0xc115fcd8,
 94     0x25e9132c,  0xd0c6e906,  0xc2bc5b2d,  0x6c065c98,
 95     0x6e37bd55
 96 };
 97 
 98 
 99 CRandom::CRandom(void)
100 {
101     Reset();
102 }
103 
104 
105 CRandom::CRandom(TValue seed)
106 {
107     SetSeed(seed);
108 }
109 
110 
111 void CRandom::Reset(void)
112 {
113     _ASSERT(sizeof(sm_State) / sizeof(sm_State[0]) == kStateSize);
114     _ASSERT(kStateOffset < kStateSize);
115 
116     for (size_t i = 0;  i < kStateSize;  ++i) {
117         m_State[i] = sm_State[i];
118     }
119 
120     m_RJ = &m_State[kStateOffset];
121     m_RK = &m_State[kStateSize - 1];
122 }
123 
124 
125 void CRandom::SetSeed(TValue seed)
126 {
127     _ASSERT(kStateOffset < kStateSize);
128 
129     m_State[0] = m_Seed = seed;
130 
131     // linear congruential initializer
132     for (size_t i = 1;  i < kStateSize;  ++i) {
133         m_State[i] = 1103515245 * m_State[i-1] + 12345;
134     }
135 
136     m_RJ = &m_State[kStateOffset];
137     m_RK = &m_State[kStateSize - 1];
138 
139     for (size_t i = 0;  i < 10 * kStateSize;  ++i) {
140         GetRand();
141     }
142 }
143 
144 
145 END_NCBI_SCOPE
146 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.