Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36namespace Gecode { namespace Support {
37
45 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
47 private:
49 static const unsigned int max = 1UL<<31;
51 unsigned int s;
53 unsigned int next(void);
54 public:
56 void seed(unsigned int s);
58 LinearCongruentialGenerator(unsigned int s = 1);
60 unsigned int seed(void) const;
62 unsigned int operator ()(unsigned int n);
64 size_t size(void) const;
65 };
66
67 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
68 forceinline unsigned int
69 LinearCongruentialGenerator<m,a,q,r>::next(void) {
70 s = a*(s%q) - r*(s/q);
71 unsigned int res = s;
72 if (s==0) s = 1;
73 return res;
74 }
75 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
76 forceinline void
78 s = _s % m;
79 if (s == 0) s = 1;
80 }
81 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
87 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
88 forceinline unsigned int
90 return s;
91 }
92 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
93 forceinline unsigned int
95 unsigned int x1 = next() & ((1<<16)-1);
96 unsigned int x2 = next() & ((1<<16)-1);
97 if (n < 2) return 0;
98 double d = static_cast<double>(((x1<<16) | x2) % max) / max;
99 unsigned int val = static_cast<unsigned int>(n * d);
100 return (val < n) ? val : (n-1);
101 }
102 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
103 forceinline size_t
107
108
121
122}}
123
124// STATISTICS: support-any
Template for linear congruential generators.
Definition random.hpp:46
size_t size(void) const
Report size occupied.
Definition random.hpp:104
unsigned int operator()(unsigned int n)
Returns a random integer from the interval [0..n)
Definition random.hpp:94
LinearCongruentialGenerator(unsigned int s=1)
Construct the generator instance with seed s.
Definition random.hpp:84
void seed(unsigned int s)
Set the current seed to s.
Definition random.hpp:77
unsigned int seed(void) const
Return current seed.
Definition random.hpp:89
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:120
Support algorithms and datastructures
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
#define forceinline
Definition config.hpp:194