Generated on Sat Feb 7 2015 02:01:28 for Gecode by doxygen 1.8.9.1
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: 2012-09-07 11:29:57 +0200 (Fri, 07 Sep 2012) $ by $Author: schulte $
11  * $Revision: 13061 $
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  public:
54  IMP(unsigned int s);
56  virtual ~IMP(void);
59  virtual SharedHandle::Object* copy(void) const;
60  };
61  public:
63  Rnd(void);
65  GECODE_KERNEL_EXPORT Rnd(unsigned int s);
67  Rnd(const Rnd& r);
70  void seed(unsigned int s);
72  void time(void);
74  void hw(void);
76  unsigned int seed(void) const;
78  unsigned int operator ()(unsigned int n);
80  bool initialized(void) const;
81  };
82 
84  Rnd::IMP::IMP(unsigned int s)
85  : rg(s) {}
86 
88  Rnd::IMP::~IMP(void) {}
89 
91  Rnd::Rnd(void) {}
93  Rnd::Rnd(const Rnd& r)
94  : SharedHandle(r) {}
95  inline void
96  Rnd::time(void) {
97  seed(static_cast<unsigned int>(::time(NULL)));
98  }
99  inline void
100  Rnd::hw(void) {
101  seed(Support::hwrnd());
102  }
103  forceinline unsigned int
104  Rnd::seed(void) const {
105  const IMP* i = static_cast<const IMP*>(object());
106  return i->rg.seed();
107  }
108  forceinline unsigned int
109  Rnd::operator ()(unsigned int n) {
110  IMP* i = static_cast<IMP*>(object());
111  return i->rg(n);
112  }
113  forceinline bool
114  Rnd::initialized(void) const {
115  return object() != NULL;
116  }
117 
118 }
119 
120 // STATISTICS: kernel-other
The shared handle.
Definition: core.hpp:79
unsigned int seed(void) const
Return current seed.
Definition: rnd.hpp:104
void time(void)
Set current seed based on time (initializes if needed)
Definition: rnd.hpp:96
Rnd(void)
Default constructor that does not initialize the generator.
Definition: rnd.hpp:91
bool initialized(void) const
Test whether generator has been properly initialized.
Definition: rnd.hpp:114
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Template for linear congruential generators.
Definition: random.hpp:50
#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:109
void hw(void)
Set current seed to hardware-based random number (initializes if needed)
Definition: rnd.hpp:100
SharedHandle::Object * object(void) const
Access to the shared object.
Definition: core.hpp:2619
#define forceinline
Definition: config.hpp:132
The shared object.
Definition: core.hpp:88
Gecode toplevel namespace
Random number generator.
Definition: rnd.hpp:46
unsigned int hwrnd(void)
Return hardware generated random number.
Definition: hw-rnd.cpp:63