Generated on for Gecode by doxygen 1.17.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.dev>
5 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Mikael Zayenz Lagerkvist, 2005
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
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
36#include <algorithm>
37
38namespace Gecode { namespace Support {
39
47 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
49 private:
51 static constexpr unsigned int max_value = 1UL<<31;
53 unsigned int s;
55 unsigned int u(unsigned int n);
57 unsigned long long int ull(unsigned long long int n);
58 public:
60 void seed(unsigned int s);
62 LinearCongruentialGenerator(unsigned int s = 1);
64 unsigned int seed(void) const;
66 unsigned int next(void);
68 template<class Type>
69 Type operator ()(Type n);
71 int operator ()(int n);
73 unsigned int operator ()(unsigned int n);
75 long long int operator ()(long long int n);
77 size_t size(void) const;
78
79 // Interface for conforming to C++ UniformRandomBitGenerator
81 typedef unsigned int result_type;
83 static constexpr result_type min() { return 0; }
85 static constexpr result_type max() { return max_value; }
87 result_type operator()() { return next(); }
88 };
89
90 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
91 forceinline unsigned int
93 s = a*(s%q) - r*(s/q);
94 unsigned int res = s;
95 if (s==0) s = 1;
96 return res;
97 }
98 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
99 forceinline void
101 s = _s % m;
102 if (s == 0) s = 1;
103 }
104 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
105 forceinline
110 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
111 forceinline unsigned int
113 return s;
114 }
115
116 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
117 forceinline unsigned int
118 LinearCongruentialGenerator<m,a,q,r>::u(unsigned int n) {
119 unsigned int x1 = next() & ((1U<<16)-1U);
120 unsigned int x2 = next() & ((1U<<16)-1U);
121 if (n < 2)
122 return 0;
123 double d = static_cast<double>(((x1<<16) | x2) % max_value) / max_value;
124 unsigned int val = static_cast<unsigned int>(n * d);
125 return (val < n) ? val : (n-1);
126 }
127 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
128 forceinline unsigned long long int
129 LinearCongruentialGenerator<m,a,q,r>::ull(unsigned long long int n) {
130 if (n <= UINT_MAX)
131 return u(static_cast<unsigned int>(n));
132 unsigned long long int x1 = next() & ((1LLU<<16)-1LLU);
133 unsigned long long int x2 = next() & ((1LLU<<16)-1LLU);
134 unsigned long long int x3 = next() & ((1LLU<<16)-1LLU);
135 unsigned long long int x4 = next() & ((1LLU<<16)-1LLU);
136 if (n < 2)
137 return 0;
138 return ((x1 << 48) | (x2 << 32) | (x3 << 16) | x4) % n;
139 }
140
141 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
142 template<class Type>
143 forceinline Type
145 return static_cast<Type>(ull(static_cast<unsigned long long int>(n)));
146 }
147 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
148 forceinline unsigned int
150 return u(n);
151 }
152 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
153 forceinline int
155 return (n < 0) ? 0 :
156 static_cast<int>(u(static_cast<unsigned int>(n)));
157 }
158 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
159 forceinline long long int
161 return (n < 0) ? 0 :
162 static_cast<long long int>
163 (ull(static_cast<unsigned long long int>(n)));
164 }
165 template<unsigned int m, unsigned int a, unsigned int q, unsigned int r>
166 forceinline size_t
170
171
184
185}}
186
187// STATISTICS: support-any
Template for linear congruential generators.
Definition random.hpp:48
size_t size(void) const
Report size occupied.
Definition random.hpp:167
static constexpr result_type max()
Maximum value that may be produced when no bound is specified.
Definition random.hpp:85
unsigned int result_type
Type of the produced values.
Definition random.hpp:81
result_type operator()()
Returns a random integer from the interval .
Definition random.hpp:87
static constexpr result_type min()
Minimum value that may be produced when no bound is specified.
Definition random.hpp:83
LinearCongruentialGenerator(unsigned int s=1)
Construct the generator instance with seed s.
Definition random.hpp:107
unsigned int next(void)
Generate next number in series.
Definition random.hpp:92
void seed(unsigned int s)
Set the current seed to s.
Definition random.hpp:100
unsigned int seed(void) const
Return current seed.
Definition random.hpp:112
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:183
Support algorithms and datastructures
Gecode toplevel namespace