library of assembled shared sources

http://lass.cocamware.com

random.inl

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 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_INL
00047 
00048 #include "num_common.h"
00049 #include "random.h"
00050 
00051 namespace lass
00052 {
00053 namespace num
00054 {
00055 
00056 // --- RandomStandard ------------------------------------------------------------------------------
00057 
00058 /** draw a random number
00059  */
00060 inline const RandomStandard::TValue RandomStandard::operator ()() const
00061 {
00062     return rand();
00063 }
00064 
00065 
00066 
00067 /** draw a random number remapped to range [0, supremum)
00068  *  @deprecated this is A VERY BAD WAY TO DO IT!
00069  */
00070 inline const RandomStandard::TValue RandomStandard::operator ()(const TValue supremum) const
00071 {
00072     return rand() % supremum;
00073 }
00074 
00075 
00076 
00077 template <typename OutputIterator>
00078 OutputIterator RandomStandard::getState(OutputIterator first) const
00079 {
00080     return first;
00081 }
00082 
00083 
00084 
00085 template <typename InputIterator>
00086 void RandomStandard::setState(InputIterator first, InputIterator last)
00087 {
00088     LASS_ASSERT(first == last);
00089 }
00090 
00091 
00092 
00093 // --- RandomParkMiller ----------------------------------------------------------------------------
00094 
00095 /** draw a random number remapped to range [0, supremum)
00096  *  @deprecated this is A VERY BAD WAY TO DO IT!
00097  */
00098 inline const RandomParkMiller::TValue RandomParkMiller::operator ()(const TValue supremum)
00099 {
00100     return (*this)() % supremum;
00101 }
00102 
00103 
00104 
00105 template <typename OutputIterator>
00106 OutputIterator RandomParkMiller::getState(OutputIterator first) const
00107 {
00108     *first = buffer_;
00109     return first;
00110 }
00111 
00112 
00113 
00114 template <typename InputIterator>
00115 void RandomParkMiller::setState(InputIterator first, InputIterator last)
00116 {
00117     LASS_ASSERT(first != last);
00118     buffer_ = *first++;
00119     LASS_ASSERT(first == last);
00120 }
00121 
00122 
00123 
00124 // --- RandomMT19937 -------------------------------------------------------------------------------
00125 
00126 /** construct by a range of initializing keys
00127  *  Requirements:
00128  *      @arg ForwardIterator is a forward iterator to elements of type TValue.  It must really be a
00129  *           forward iterator and not just an input iterator, since it must be possible to iterator
00130  *           over the same range more than once.
00131  */
00132 template <typename ForwardIterator>
00133 RandomMT19937::RandomMT19937(ForwardIterator first, ForwardIterator last)
00134 {
00135     seed(first, last);
00136 }
00137 
00138 
00139 
00140 /** initialize by a range of initializing keys
00141  *  Requirements:
00142  *      @arg ForwardIterator is a forward iterator to elements of type TValue.  It must really be a
00143  *           forward iterator and not just an input iterator, since it must be possible to iterator
00144  *           over the same range more than once.
00145  */
00146 template <typename ForwardIterator>
00147 void RandomMT19937::seed(ForwardIterator first, ForwardIterator last)
00148 {
00149     LASS_META_ASSERT(sizeof(TValue) * lass::bitsPerByte == 32,
00150         if_TValue_is_32_bits_then_the_wordMasks_are_not_necessary);
00151 
00152     seed(19650218UL);
00153 
00154     const size_t keySize = std::distance(first, last);
00155     int i = 1;
00156     int j = 0;
00157     ForwardIterator key = first;
00158     for (size_t k = (stateSize_ > keySize ? stateSize_ : keySize); k > 0; --k)
00159     {
00160         state_[i] = (state_[i] ^ ((state_[i - 1] ^ (state_[i - 1] >> 30)) * 1664525UL)) + *key + j;
00161         //state_[i] &= wordMask_;
00162 
00163         ++i;
00164         if (i >= stateSize_)
00165         {
00166             state_[0] = state_[stateSize_ - 1];
00167             i = 1;
00168         }
00169 
00170         ++key;
00171         ++j;
00172         if (key == last)
00173         {
00174             key = first;
00175             j = 0;
00176         }
00177         LASS_ASSERT(static_cast<int>(keySize) >= 0);
00178         LASS_ASSERT(j < static_cast<int>(keySize));
00179     }
00180     for (size_t k = stateSize_ - 1; k > 0; --k)
00181     {
00182         state_[i] = (state_[i] ^ ((state_[i - 1] ^ (state_[i - 1] >> 30)) * 1566083941UL)) - i;
00183         //state_[i] &= wordMask_;
00184         ++i;
00185         if (i >= stateSize_)
00186         {
00187             state_[0] = state_[stateSize_ - 1];
00188             i = 1;
00189         }
00190     }
00191 
00192     state_[0] = 0x80000000;
00193 }
00194 
00195 
00196 
00197 /** draw a random number remapped to range [0, supremum)
00198  *  @deprecated this is A VERY BAD WAY TO DO IT!
00199  */
00200 inline const RandomMT19937::TValue RandomMT19937::operator ()(const TValue supremum)
00201 {
00202     return (*this)() % supremum;
00203 }
00204 
00205 
00206 
00207 template <typename OutputIterator>
00208 OutputIterator RandomMT19937::getState(OutputIterator first) const
00209 {
00210     *first++ = index_;
00211     return std::copy(state_, state_ + stateSize_, first);
00212 }
00213 
00214 
00215 
00216 template <typename InputIterator>
00217 void RandomMT19937::setState(InputIterator first, InputIterator last)
00218 {
00219     LASS_ASSERT(first != last);
00220     index_ = *first++;
00221     LASS_ASSERT(first != last);
00222     TValue* end = std::copy(first, last, state_);
00223     LASS_ASSERT(end == state_ + stateSize_);
00224 }
00225 
00226 
00227 
00228 }
00229 
00230 }
00231 
00232 #endif
00233 
00234 // 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