library of assembled shared sources

http://lass.cocamware.com

random.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 #include "num_common.h"
00046 #include "random.h"
00047 
00048 namespace lass
00049 {
00050 namespace num
00051 {
00052 
00053 // --- RandomStandard ------------------------------------------------------------------------------
00054 
00055 const RandomStandard::TValue RandomStandard::max = RAND_MAX;
00056 
00057 
00058 
00059 
00060 // --- RandomParkMiller ----------------------------------------------------------------------------
00061 
00062 const RandomParkMiller::TValue RandomParkMiller::max = RandomParkMiller::modulus_;
00063 
00064 
00065 
00066 /** default constructor.
00067  *  will seed with default value
00068  */
00069 RandomParkMiller::RandomParkMiller()
00070 {
00071     seed(0);
00072 }
00073 
00074 
00075 
00076 /** default constructor.
00077  *  will seed with default value
00078  */
00079 RandomParkMiller::RandomParkMiller(TValue seed)
00080 {
00081     this->seed(seed);
00082 }
00083 
00084 
00085 
00086 void RandomParkMiller::seed(TValue seed)
00087 {
00088     buffer_ = seed ^ seedMask_;
00089 }
00090 
00091 
00092 
00093 /** draw a random number
00094  */
00095 const RandomParkMiller::TValue RandomParkMiller::operator ()()
00096 {
00097     TValue k = buffer_ / schrageQuotient_;
00098     buffer_ = multiplier_ * (buffer_ - k * schrageQuotient_) - k * schrageRest_;
00099     return buffer_;
00100 }
00101 
00102 
00103 
00104 // --- RandomMT19937 -------------------------------------------------------------------------------
00105 
00106 const RandomMT19937::TValue RandomMT19937::max = 0xffffffff;
00107 
00108 /** default constructor.
00109  *  will seed with default value on first use.
00110  */
00111 RandomMT19937::RandomMT19937():
00112     index_(stateSize_ + 1)
00113 {
00114 }
00115 
00116 
00117 
00118 /** construct with seed.
00119  */
00120 RandomMT19937::RandomMT19937(TValue seed)
00121 {
00122     this->seed(seed);
00123 }
00124 
00125 
00126 
00127 /** initializes with a seed.
00128  */
00129 void RandomMT19937::seed(TValue seed)
00130 {
00131     state_[0] = seed; // & 0xffffffff;
00132     for (unsigned i = 1; i < stateSize_; ++i)
00133     {
00134         state_[i] = (1812433253 * (state_[i - 1] ^ (state_[i - 1] >> 30)) + i);
00135         //state_[i] &= 0xffffffff;
00136     }
00137     index_ = stateSize_;
00138 }
00139 
00140 
00141 
00142 /** draw a random number 
00143  */
00144 const RandomMT19937::TValue RandomMT19937::operator()()
00145 {
00146     if (index_ >= stateSize_)
00147     {
00148         reload();
00149     }
00150 
00151     TValue y = state_[index_++];
00152 
00153     // Tempering
00154     //
00155     y ^= (y >> 11);
00156     y ^= (y << 7) & 0x9d2c5680;
00157     y ^= (y << 15) & 0xefc60000;
00158     y ^= (y >> 18);
00159 
00160     return y;
00161 }
00162 
00163 
00164 // private
00165 
00166 /** generate N words at a time
00167  */
00168 void RandomMT19937::reload()
00169 {
00170 
00171     // if seed() has not been called, a default initial seed is used.
00172     //
00173     if (index_ == stateSize_ + 1)
00174     {
00175         seed(5489);
00176     }
00177 
00178     int k;
00179     for (k = 0; k < stateSize_ - shiftSize_; ++k)
00180     {
00181         state_[k] = twist(state_[k], state_[k + 1], state_[k + shiftSize_]);
00182     }
00183     for (; k < stateSize_ - 1; ++k)
00184     {
00185         state_[k] = twist(state_[k], state_[k + 1], state_[k + shiftSize_ - stateSize_]);
00186     }
00187     state_[stateSize_ - 1] = twist(state_[stateSize_ - 1], state_[0], state_[shiftSize_ - 1]);
00188     index_ = 0;
00189 }
00190 
00191 
00192 inline const RandomMT19937::TValue RandomMT19937::twist(TValue a, TValue b, TValue c) const
00193 {
00194     static const TValue magic01[2] = { 0x0, 0x9908b0df }; // magic01[x] = x * magic_ for x = 0, 1
00195 
00196     const TValue y = (a & 0x80000000) | (b & 0x7fffffff);
00197     return c ^ (y >> 1) ^ magic01[y & 0x1];
00198 }
00199 
00200 
00201 
00202 }
00203 
00204 }
00205 
00206 // EOF

Generated on Mon Nov 10 14:21:07 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo