main page
modules
namespaces
classes
files
Gecode home
Generated on Mon Jul 27 2020 00:00:00 for Gecode by
doxygen
1.8.18
gecode
kernel
data
rnd.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
*
6
* Copyright:
7
* Christian Schulte, 2008
8
*
9
* This file is part of Gecode, the generic constraint
10
* development environment:
11
* http://www.gecode.org
12
*
13
* Permission is hereby granted, free of charge, to any person obtaining
14
* a copy of this software and associated documentation files (the
15
* "Software"), to deal in the Software without restriction, including
16
* without limitation the rights to use, copy, modify, merge, publish,
17
* distribute, sublicense, and/or sell copies of the Software, and to
18
* permit persons to whom the Software is furnished to do so, subject to
19
* the following conditions:
20
*
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
23
*
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
*
32
*/
33
34
#include <ctime>
35
36
namespace
Gecode
{
37
42
class
Rnd
:
public
SharedHandle
{
43
private
:
45
class
IMP :
public
SharedHandle::Object
{
46
protected
:
48
GECODE_KERNEL_EXPORT
static
Support::Mutex
m;
50
Support::RandomGenerator
rg;
51
public
:
53
IMP(
unsigned
int
s);
55
unsigned
int
seed
(
void
)
const
;
57
void
seed
(
unsigned
int
s);
59
unsigned
int
operator ()
(
unsigned
int
n
);
61
virtual
~IMP(
void
);
62
};
64
void
_seed(
unsigned
int
s);
65
public
:
67
GECODE_KERNEL_EXPORT
68
Rnd
(
void
);
70
GECODE_KERNEL_EXPORT
71
Rnd
(
const
Rnd
&
r
);
73
GECODE_KERNEL_EXPORT
74
Rnd
&
operator =
(
const
Rnd
&
r
);
76
GECODE_KERNEL_EXPORT
77
~Rnd
(
void
);
79
GECODE_KERNEL_EXPORT
80
Rnd
(
unsigned
int
s);
82
GECODE_KERNEL_EXPORT
83
void
seed
(
unsigned
int
s);
85
GECODE_KERNEL_EXPORT
86
void
time
(
void
);
88
GECODE_KERNEL_EXPORT
89
void
hw
(
void
);
91
unsigned
int
seed
(
void
)
const
;
93
unsigned
int
operator ()
(
unsigned
int
n
);
94
};
95
96
forceinline
unsigned
int
97
Rnd::IMP::seed(
void
)
const
{
98
unsigned
int
s;
99
const_cast<
Rnd::IMP&
>
(*this).m.acquire();
100
s = rg.seed();
101
const_cast<
Rnd::IMP&
>
(*this).m.release();
102
return
s;
103
}
104
forceinline
void
105
Rnd::IMP::seed(
unsigned
int
s) {
106
m.acquire();
107
rg.seed(s);
108
m.release();
109
}
110
forceinline
unsigned
int
111
Rnd::IMP::operator ()(
unsigned
int
n
) {
112
unsigned
int
r
;
113
m.acquire();
114
r
=rg(
n
);
115
m.release();
116
return
r
;
117
}
118
119
forceinline
unsigned
int
120
Rnd::seed
(
void
)
const
{
121
const
IMP*
i
=
static_cast<
const
IMP*
>
(
object
());
122
return
i
->seed();
123
}
124
forceinline
unsigned
int
125
Rnd::operator ()
(
unsigned
int
n
) {
126
IMP*
i
=
static_cast<
IMP*
>
(
object
());
127
return
(*
i
)(
n
);
128
}
129
130
}
131
132
// STATISTICS: kernel-other
Gecode::Rnd::Rnd
Rnd(void)
Default constructor that does not initialize the generator.
Definition:
rnd.cpp:57
Gecode::Rnd::~Rnd
~Rnd(void)
Destructor.
Definition:
rnd.cpp:68
Gecode::SharedHandle::object
SharedHandle::Object * object(void) const
Access to the shared object.
Definition:
shared-object.hpp:102
Gecode::Rnd::operator=
Rnd & operator=(const Rnd &r)
Assignment operator.
Definition:
rnd.cpp:64
Gecode::SharedHandle::Object
The shared object.
Definition:
shared-object.hpp:53
Gecode::Rnd::seed
unsigned int seed(void) const
Return current seed.
Definition:
rnd.hpp:120
Gecode
Gecode toplevel namespace
Gecode::Rnd::operator()
unsigned int operator()(unsigned int n)
Return a random integer from the interval [0..n)
Definition:
rnd.hpp:125
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
Gecode::Rnd
Random number generator.
Definition:
rnd.hpp:42
GECODE_KERNEL_EXPORT
#define GECODE_KERNEL_EXPORT
Definition:
kernel.hh:70
Gecode::Rnd::hw
void hw(void)
Set current seed to hardware-based random number (initializes if needed)
Definition:
rnd.cpp:79
forceinline
#define forceinline
Definition:
config.hpp:185
Gecode::SharedHandle
The shared handle.
Definition:
shared-object.hpp:46
Gecode::Support::Mutex
A mutex for mutual exclausion among several threads.
Definition:
thread.hpp:96
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
Gecode::Rnd::time
void time(void)
Set current seed based on time (initializes if needed)
Definition:
rnd.cpp:75