library of assembled shared sources

http://lass.cocamware.com

random.h

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 /** @defgroup Random
00046  *
00047  *  A set of random number generators.
00048  *
00049  *  - @ref RandomStandard : uses the C standard function rand().
00050  *  - @ref RandomParkMiller : Minimal Standard generator by Park and Miller.
00051  *  - @ref RandomMT19937 : uses a mersenne twister MT19937.
00052  */
00053 
00054 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_H
00055 #define LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_H
00056 
00057 #include "num_common.h"
00058 #include <cstdlib>
00059 #include "../util/call_traits.h"
00060 
00061 namespace lass
00062 {
00063 namespace num
00064 {
00065 
00066 /** Uses the C standard library function rand().
00067  *  @ingroup Random
00068  *
00069  *  @note this one isn't thread safe since state is stored externally.
00070  */
00071 class LASS_DLL RandomStandard
00072 {
00073 public:
00074     typedef int TValue;             /**< type of return value. */
00075     static const TValue max;        /**< maximum return value. */
00076 
00077     const TValue operator()() const;
00078     const TValue operator()(TValue supremum) const;
00079 
00080     template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
00081     template <typename InputIterator> void setState(InputIterator first, InputIterator last);
00082 
00083 
00084 };
00085 
00086 
00087 
00088 /** Minimal Standard generator by Park and Miller
00089  *  @ingroup Random
00090  *
00091  *  RandomParkMiller is the LASS implementation of the Minimal Standard generator by Park and Miller
00092  *
00093  *  <i>Park and Miller, "Random Number Generators: Good ones are hard to find", Communications of the
00094  *  ACM, October 1988, Volume 31, No 10, pages 1192-1201.</i>
00095  */
00096 class LASS_DLL RandomParkMiller
00097 {
00098 public:
00099 
00100     typedef num::Tuint32 TValue;    /**< type of return value. */
00101     static const TValue max;        /**< maximum return value. */
00102 
00103     RandomParkMiller();
00104     explicit RandomParkMiller(TValue seed);
00105 
00106     void seed(TValue seed);
00107 
00108     const TValue operator()();
00109     const TValue operator()(TValue supremum);
00110 
00111     template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
00112     template <typename InputIterator> void setState(InputIterator first, InputIterator last);
00113 
00114 private:
00115 
00116     enum
00117     {
00118         multiplier_ = 16807,    
00119         modulus_ = 2147483647,
00120         seedMask_ = 123456789,
00121         schrageQuotient_ = modulus_ / multiplier_,
00122         schrageRest_ = modulus_ % multiplier_,
00123     };
00124 
00125     TValue buffer_;
00126 };
00127 
00128 
00129 /** implemenents a mersenne twister MT19937.
00130  *  @ingroup Random
00131  *
00132  *  RandomMT19937 is the LASS implementation of the Mersenne twister.  Mersenne Twister(MT) is a
00133  *  pseudorandom number generator developped by Makoto Matsumoto and Takuji Nishimura (alphabetical
00134  *  order) during 1996-1997. MT has the following merits:
00135  *
00136  *  - It is designed with consideration on the flaws of various existing generators.
00137  *  - Far longer period and far higher order of equidistribution than any other implemented
00138  *    generators. (It is proved that the period is 2^19937-1, and 623-dimensional equidistribution
00139  *    property is assured.)
00140  *  - Fast generation. (Although it depends on the system, it is reported that MT is sometimes
00141  *    faster than the standard ANSI-C library in a system with pipeline and cache memory.)
00142  *  - Efficient use of the memory. (The implementation consumes only 624 words of working area.)
00143  *
00144  *  <i>M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed uniform
00145  *  pseudorandom number generator", ACM Trans. on Modeling and Computer Simulation Vol. 8, No. 1,
00146  *  January pp.3-30 1998</i>, http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
00147  *
00148  *  Our version is implemented after the MT19937 standard code of 2002/1/26 (mt19937ar.c)
00149  */
00150 class LASS_DLL RandomMT19937
00151 {
00152 public:
00153 
00154     typedef num::Tuint32 TValue;   /**< type of return value. */
00155     static const TValue max;        /**< maximum return value. */
00156 
00157     RandomMT19937();
00158     explicit RandomMT19937(TValue seed);
00159     template <typename ForwardIterator> RandomMT19937(ForwardIterator first, ForwardIterator last);
00160 
00161     void seed(TValue seed);
00162     template <typename ForwardIterator> void seed(ForwardIterator first, ForwardIterator last);
00163 
00164     const TValue operator()();
00165     const TValue operator()(TValue supremum);
00166 
00167     template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
00168     template <typename InputIterator> void setState(InputIterator first, InputIterator last);
00169 
00170 private:
00171 
00172     LASS_META_ASSERT(sizeof(TValue) * lass::bitsPerByte == 32,
00173         MersenneTwister_is_designed_to_be_a_32_bits_random_number_generator);
00174 
00175     enum
00176     {
00177         stateSize_  = 624,          /**< size of state vector */
00178         shiftSize_  = 397,
00179     };
00180 
00181     void reload();
00182     const TValue twist(TValue a, TValue b, TValue c) const;
00183 
00184     TValue state_[stateSize_];      /**< the array for the state vector. */
00185     TValue index_;                     /**< index in state vector. */
00186 
00187     static TValue wordMask_;
00188     static TValue lowerMask_;
00189     static TValue upperMask_;
00190 };
00191 
00192 
00193 
00194 }
00195 
00196 }
00197 
00198 #include "random.inl"
00199 
00200 #endif
00201 
00202 // 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