main page
modules
namespaces
classes
files
Gecode home
Generated on Mon Feb 8 2021 00:00:00 for Gecode by
doxygen
1.8.20
gecode
support
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
36
namespace
Gecode
{
namespace
Support {
37
45
template
<
unsigned
int
m,
unsigned
int
a,
unsigned
int
q,
unsigned
int
r>
46
class
LinearCongruentialGenerator
{
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
77
LinearCongruentialGenerator<m,a,q,r>::seed
(
unsigned
int
_s) {
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>
82
forceinline
83
LinearCongruentialGenerator<m,a,q,r>::
84
LinearCongruentialGenerator
(
unsigned
int
_s) {
85
seed(_s);
86
}
87
template
<
unsigned
int
m,
unsigned
int
a,
unsigned
int
q,
unsigned
int
r>
88
forceinline
unsigned
int
89
LinearCongruentialGenerator<m,a,q,r>::seed
(
void
)
const
{
90
return
s;
91
}
92
template
<
unsigned
int
m,
unsigned
int
a,
unsigned
int
q,
unsigned
int
r>
93
forceinline
unsigned
int
94
LinearCongruentialGenerator<m,a,q,r>::operator ()
(
unsigned
int
n
) {
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
104
LinearCongruentialGenerator<m,a,q,r>::size
(
void
)
const
{
105
return
sizeof
(
LinearCongruentialGenerator<m,a,q,r>
);
106
}
107
108
119
typedef
LinearCongruentialGenerator<2147483647, 48271, 44488, 3399>
120
RandomGenerator
;
121
122
}}
123
124
// STATISTICS: support-any
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition:
arithmetic.cpp:49
Gecode::Support::LinearCongruentialGenerator::seed
unsigned int seed(void) const
Return current seed.
Definition:
random.hpp:89
Gecode::Support::RandomGenerator
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition:
random.hpp:120
Gecode::Support::LinearCongruentialGenerator::LinearCongruentialGenerator
LinearCongruentialGenerator(unsigned int s=1)
Construct the generator instance with seed s.
Definition:
random.hpp:84
Gecode
Gecode toplevel namespace
Gecode::r
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition:
set.hh:767
Gecode::Support::LinearCongruentialGenerator
Template for linear congruential generators.
Definition:
random.hpp:46
a
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
Gecode::Support::LinearCongruentialGenerator::size
size_t size(void) const
Report size occupied.
Definition:
random.hpp:104
forceinline
#define forceinline
Definition:
config.hpp:192
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Gecode::Support::LinearCongruentialGenerator::operator()
unsigned int operator()(unsigned int n)
Returns a random integer from the interval [0..n)
Definition:
random.hpp:94