Generated on Tue Jan 19 2021 06:15:49 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  * 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:
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:
68  Rnd(void);
71  Rnd(const Rnd& r);
74  Rnd& operator =(const Rnd& r);
77  ~Rnd(void);
80  Rnd(unsigned int s);
83  void seed(unsigned int s);
86  void time(void);
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
The shared handle.
unsigned int seed(void) const
Return current seed.
Definition: rnd.hpp:120
#define forceinline
Definition: config.hpp:185
void time(void)
Set current seed based on time (initializes if needed)
Definition: rnd.cpp:75
Rnd(void)
Default constructor that does not initialize the generator.
Definition: rnd.cpp:57
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:96
Rnd & operator=(const Rnd &r)
Assignment operator.
Definition: rnd.cpp:64
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
SharedHandle::Object * object(void) const
Access to the shared object.
Template for linear congruential generators.
Definition: random.hpp:46
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
unsigned int operator()(unsigned int n)
Return a random integer from the interval [0..n)
Definition: rnd.hpp:125
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
void hw(void)
Set current seed to hardware-based random number (initializes if needed)
Definition: rnd.cpp:79
Gecode toplevel namespace
Random number generator.
Definition: rnd.hpp:42
~Rnd(void)
Destructor.
Definition: rnd.cpp:68