Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <ctime>
39 
40 namespace Gecode {
41 
46  class Rnd : public SharedHandle {
47  private:
49  class IMP : public SharedHandle::Object {
50  protected:
55  public:
57  IMP(unsigned int s);
59  unsigned int seed(void) const;
61  void seed(unsigned int s);
63  unsigned int operator ()(unsigned int n);
65  virtual ~IMP(void);
66  };
68  void _seed(unsigned int s);
69  public:
72  Rnd(void);
75  Rnd(const Rnd& r);
78  Rnd& operator =(const Rnd& r);
81  ~Rnd(void);
84  Rnd(unsigned int s);
87  void seed(unsigned int s);
90  void time(void);
93  void hw(void);
95  unsigned int seed(void) const;
97  unsigned int operator ()(unsigned int n);
98  };
99 
100  forceinline unsigned int
101  Rnd::IMP::seed(void) const {
102  unsigned int s;
103  const_cast<Rnd::IMP&>(*this).m.acquire();
104  s = rg.seed();
105  const_cast<Rnd::IMP&>(*this).m.release();
106  return s;
107  }
108  forceinline void
109  Rnd::IMP::seed(unsigned int s) {
110  m.acquire();
111  rg.seed(s);
112  m.release();
113  }
114  forceinline unsigned int
115  Rnd::IMP::operator ()(unsigned int n) {
116  unsigned int r;
117  m.acquire();
118  r=rg(n);
119  m.release();
120  return r;
121  }
122 
123  forceinline unsigned int
124  Rnd::seed(void) const {
125  const IMP* i = static_cast<const IMP*>(object());
126  return i->seed();
127  }
128  forceinline unsigned int
129  Rnd::operator ()(unsigned int n) {
130  IMP* i = static_cast<IMP*>(object());
131  return (*i)(n);
132  }
133 
134 }
135 
136 // STATISTICS: kernel-other
The shared handle.
unsigned int seed(void) const
Return current seed.
Definition: rnd.hpp:124
#define forceinline
Definition: config.hpp:182
void time(void)
Set current seed based on time (initializes if needed)
Definition: rnd.cpp:79
Rnd(void)
Default constructor that does not initialize the generator.
Definition: rnd.cpp:61
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:105
Rnd & operator=(const Rnd &r)
Assignment operator.
Definition: rnd.cpp:68
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
SharedHandle::Object * object(void) const
Access to the shared object.
Template for linear congruential generators.
Definition: random.hpp:50
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:74
unsigned int operator()(unsigned int n)
Return a random integer from the interval [0..n)
Definition: rnd.hpp:129
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
void hw(void)
Set current seed to hardware-based random number (initializes if needed)
Definition: rnd.cpp:83
Gecode toplevel namespace
Random number generator.
Definition: rnd.hpp:46
~Rnd(void)
Destructor.
Definition: rnd.cpp:72