i6engine  1.0
i6eRandom.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_RANDOM_H__
26 #define __I6ENGINE_UTILS_RANDOM_H__
27 
28 #include <mutex>
29 #include <random>
30 
33 
34 #include "boost/date_time.hpp"
35 
36 namespace i6e {
37 namespace utils {
38 
42  class ISIXE_UTILS_API Random : public Singleton<Random> {
43  friend class Singleton<Random>;
44 
45  public:
49  ~Random() {
50  delete _linear;
51  }
52 
56  uint32_t rand() const {
57  return getRand();
58  }
59 
63  uint32_t rand(uint32_t max) const {
64  if (max == 0) {
65  ISIXE_THROW_API("i6eRandom", "max value has to be grater than zero")
66  }
67  return getRand() % max;
68  }
69 
73  uint32_t rand(uint32_t min, uint32_t max) const {
74  if (min >= max) {
75  ISIXE_THROW_API("i6eRandom", "max value has to be greater than min value")
76  }
77  return (getRand() % (max - min)) + min;
78  }
79 
83  void setSeed(uint32_t seed) {
84  std::lock_guard<std::mutex> l(_lock);
85  _linear->seed(seed);
86  }
87 
88  private:
92  std::minstd_rand * _linear;
93 
97  mutable std::mutex _lock;
98 
102  uint32_t getRand() const {
103  std::lock_guard<std::mutex> l(_lock);
104  return (*_linear)();
105  }
106 
110  Random() : _linear(new std::minstd_rand(static_cast<unsigned int>(boost::posix_time::time_period(boost::posix_time::time_from_string("2010-03-19 14:23:10"), boost::posix_time::microsec_clock::universal_time()).length().total_microseconds()))), _lock() {
111  }
112  };
113 
114 } /* namespace utils */
115 } /* namespace i6e */
116 
117 #endif /* __I6ENGINE_UTILS_RANDOM_H__ */
118 
Derive from this templated class to make a class a singleton. Refer to the Singleton Design Pattern i...
Definition: Singleton.h:44
#define ISIXE_THROW_API(module, message)
Definition: Exceptions.h:45
uint32_t rand(uint32_t max) const
returns a random number in the range between 0 and max ([0;max[)
Definition: i6eRandom.h:63
~Random()
destructor
Definition: i6eRandom.h:49
creates random numbers
Definition: i6eRandom.h:42
#define ISIXE_UTILS_API
uint32_t rand() const
returns a random number in range of unsigned int
Definition: i6eRandom.h:56
uint32_t rand(uint32_t min, uint32_t max) const
returns a random number in the range between min and max ([min; max[)
Definition: i6eRandom.h:73
void setSeed(uint32_t seed)
sets a random seed value
Definition: i6eRandom.h:83