m2etis  0.4
i6eRandom.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_UTILS_RANDOM_H__
23 #define __M2ETIS_UTILS_RANDOM_H__
24 
25 #include <cmath>
26 #include <random>
27 
28 #include "m2etis/util/Exceptions.h"
29 
30 #include "boost/date_time.hpp"
31 #include "boost/thread/mutex.hpp"
32 
33 namespace m2etis {
34 namespace util {
35 
39  class Random {
40  public:
44  static unsigned int rand() {
45  boost::mutex::scoped_lock l(lock);
46  return random.getRand();
47  }
48 
52  static unsigned int rand(unsigned int max) {
53  if (max == 0) {
54  M2ETIS_THROW_API("i6eRandom", "max value has to be grater than zero")
55  }
56  return rand() % max;
57  }
58 
62  static unsigned int rand(unsigned int min, unsigned int max) {
63  if (min >= max) {
64  M2ETIS_THROW_API("i6eRandom", "max value has to be greater than min value")
65  }
66  return (rand() % (max - min)) + min;
67  }
68 
69  private:
73  static Random random;
74 
78  std::minstd_rand * _linear;
79 
83  static boost::mutex lock;
84 
88  Random() : _linear(new std::minstd_rand(17)) {
89  }
90 
94  ~Random() {
95  delete _linear;
96  }
97 
101  unsigned int getRand() { return (*_linear)(); }
102  };
103 
104 } /* namespace util */
105 } /* namespace m2etis */
106 
107 #endif /* __M2ETIS_UTILS_RANDOM_H__ */
108 
STL namespace.
static unsigned int rand(unsigned int min, unsigned int max)
returns a random number in the range between min and max
Definition: i6eRandom.h:62
static unsigned int rand()
returns a random number in range of unsigned int
Definition: i6eRandom.h:44
static unsigned int rand(unsigned int max)
returns a random number in the range between 0 and max
Definition: i6eRandom.h:52
creates random numbers
Definition: i6eRandom.h:39
#define M2ETIS_THROW_API(module, message)
throws on wrong API usage
Definition: Exceptions.h:40