The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
random_new.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "random_new.hpp"
16 #include "log.hpp"
17 
18 
19 #include <cassert>
20 #include <stdlib.h>
21 #include <boost/random/mersenne_twister.hpp>
22 #include <time.h>
23 
24 static lg::log_domain log_random("random");
25 #define DBG_RND LOG_STREAM(debug, log_random)
26 #define LOG_RND LOG_STREAM(info, log_random)
27 #define WRN_RND LOG_STREAM(warn, log_random)
28 #define ERR_RND LOG_STREAM(err, log_random)
29 
30 namespace {
31 
32  class rng_default : public random_new::rng
33  {
34  public:
35  rng_default()
36  : gen_(time(nullptr))
37  {
38  }
39  protected:
40  virtual uint32_t next_random_impl()
41  {
42  return gen_();
43  }
44  private:
45  boost::mt19937 gen_;
46  };
47 }
48 
49 namespace random_new
50 {
51 
53 
55  : random_calls_(0)
56  {
57 
58  }
59 
61  {
62 
63  }
64 
66  {
67  static rng* def = new rng_default();
68  return *def;
69  }
70 
71  unsigned int rng::get_random_calls()
72  {
73  return random_calls_;
74  }
75 
77  {
78  random_calls_++;
79  return next_random_impl();
80  }
81 
82  /**
83  * This code is based on the boost implementation of uniform_smallint.
84  * http://www.boost.org/doc/libs/1_55_0/boost/random/uniform_smallint.hpp
85  * Using that code would be ideal, except that boost, and C++11, do not
86  * guarantee that it will work the same way on all platforms, or that the
87  * results may not be different in future versions of the library.
88  * The simplified version I have written should work the same on all
89  * platforms, which is the most important thing for us.
90  * The existence of "modulo bias" seems less important when we have moved
91  * to boost::mt19937, since it guarantees that there are no "bad bits"
92  * and has a very large range.
93  *
94  * If a standard cross platform version becomes available then this should
95  * be replaced.
96  */
98  {
99  assert(max >= 0);
100  return static_cast<int> (next_random() % (static_cast<uint32_t>(max)+1));
101  }
102 }
static lg::log_domain log_random("random")
virtual uint32_t next_random_impl()=0
rng * generator
This generator is automatically synced during synced context.
Definition: random_new.cpp:52
unsigned int get_random_calls()
Provides the number of random calls to the rng in this context.
Definition: random_new.cpp:71
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
static rng & default_instance()
Definition: random_new.cpp:65
virtual ~rng()
Definition: random_new.cpp:60
uint32_t next_random()
Provides the next random draw.
Definition: random_new.cpp:76
unsigned int random_calls_
Definition: random_new.hpp:55
this class does not give synced random results derived classes might do.
Definition: random_new.hpp:27
Standard logging facilities (interface).
int get_random_int_in_range_zero_to(int max)
Does the hard work of get_random_int.
Definition: random_new.cpp:97