Generated on Thu Apr 11 13:59:20 2019 for Gecode by doxygen 1.6.3

random.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2005
00009  *     Mikael Lagerkvist, 2005
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 namespace Gecode { namespace Support {
00037 
00045   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00046   class LinearCongruentialGenerator {
00047   private:
00049     static const unsigned int max = 1UL<<31;
00051     unsigned int s;
00053     unsigned int next(void);
00054   public:
00056     void seed(unsigned int s);
00058     LinearCongruentialGenerator(unsigned int s = 1);
00060     unsigned int seed(void) const;
00062     unsigned int operator ()(unsigned int n);
00064     size_t size(void) const;
00065   };
00066 
00067   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00068   forceinline unsigned int
00069   LinearCongruentialGenerator<m,a,q,r>::next(void) {
00070     s = a*(s%q) - r*(s/q);
00071     unsigned int res = s;
00072     if (s==0) s = 1;
00073     return res;
00074   }
00075   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00076   forceinline void
00077   LinearCongruentialGenerator<m,a,q,r>::seed(unsigned int _s) {
00078     s = _s % m;
00079     if (s == 0) s = 1;
00080   }
00081   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00082   forceinline
00083   LinearCongruentialGenerator<m,a,q,r>::
00084   LinearCongruentialGenerator(unsigned int _s) {
00085     seed(_s);
00086   }
00087   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00088   forceinline unsigned int
00089   LinearCongruentialGenerator<m,a,q,r>::seed(void) const {
00090     return s;
00091   }
00092   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00093   forceinline unsigned int
00094   LinearCongruentialGenerator<m,a,q,r>::operator ()(unsigned int n) {
00095     unsigned int x1 = next() & ((1<<16)-1);
00096     unsigned int x2 = next() & ((1<<16)-1);
00097     if (n < 2) return 0;
00098     double d = static_cast<double>(((x1<<16) | x2) % max) / max;
00099     unsigned int val = static_cast<unsigned int>(n * d);
00100     return (val < n) ? val : (n-1);
00101   }
00102   template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
00103   forceinline size_t
00104   LinearCongruentialGenerator<m,a,q,r>::size(void) const {
00105     return sizeof(LinearCongruentialGenerator<m,a,q,r>);
00106   }
00107 
00108 
00119   typedef LinearCongruentialGenerator<2147483647, 48271, 44488, 3399>
00120   RandomGenerator;
00121 
00122 }}
00123 
00124 // STATISTICS: support-any