Generated on Sat Feb 7 2015 02:01:31 for Gecode by doxygen 1.8.9.1
random.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2005
9  * Mikael Lagerkvist, 2005
10  *
11  * Last modified:
12  * $Date: 2012-09-07 11:29:57 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
13  * $Revision: 13061 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 namespace Gecode { namespace Support {
41 
49  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
51  private:
53  static const unsigned int max = 1UL<<31;
55  unsigned int s;
57  unsigned int next(void);
58  public:
60  void seed(unsigned int s);
62  LinearCongruentialGenerator(unsigned int s = 1);
64  unsigned int seed(void) const;
66  unsigned int operator ()(unsigned int n);
68  size_t size(void) const;
69  };
70 
71  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
72  forceinline unsigned int
74  s = a*(s%q) - r*(s/q);
75  unsigned int res = s;
76  if (s==0) s = 1;
77  return res;
78  }
79  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
80  forceinline void
82  s = _s % m;
83  if (s == 0) s = 1;
84  }
85  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
88  LinearCongruentialGenerator(unsigned int _s) {
89  seed(_s);
90  }
91  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
92  forceinline unsigned int
94  return s;
95  }
96  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
97  forceinline unsigned int
99  unsigned int x1 = next() & ((1<<16)-1);
100  unsigned int x2 = next() & ((1<<16)-1);
101  if (n < 2) return 0;
102  double d = static_cast<double>(((x1<<16) | x2) % max) / max;
103  unsigned int val = static_cast<unsigned int>(n * d);
104  return (val < n) ? val : (n-1);
105  }
106  template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
107  forceinline size_t
110  }
111 
112 
125 
126 }}
127 
128 // STATISTICS: support-any
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
Gecode::IntSet d(v, 7)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
unsigned int operator()(unsigned int n)
Returns a random integer from the interval [0..n)
Definition: random.hpp:98
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
LinearCongruentialGenerator(unsigned int s=1)
Construct the generator instance with seed s.
Definition: random.hpp:88
Template for linear congruential generators.
Definition: random.hpp:50
size_t size(void) const
Report size occupied.
Definition: random.hpp:108
unsigned int seed(void) const
Return current seed.
Definition: random.hpp:93
#define forceinline
Definition: config.hpp:132
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition: random.hpp:124
Gecode toplevel namespace
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.