The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mt_rng.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[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 #ifndef MT_RNG_HPP_INCLUDED
16 #define MT_RNG_HPP_INCLUDED
17 
18 #include <boost/cstdint.hpp>
19 #include <boost/random/mersenne_twister.hpp>
20 
21 using boost::uint32_t;
22 
23 class config;
24 
25 namespace rand_rng
26 {
27 
28 /*
29  This class provides an interface, similar to simple_rng, to the
30  boost mt19937 generator.
31 */
32 
33 class mt_rng
34 {
35 public:
36  mt_rng();
37  explicit mt_rng(const config& cfg);
38  explicit mt_rng(boost::uint32_t seed);
39  /** Get a new random number. */
41 
42  /**
43  * Same as uint32_t version, but uses a stringstream to convert given
44  * hex string.
45  * @param seed A hex string. Should not have 0x leading.
46  * @param call_count Value to set internal call counter to after seeding.
47  */
48  void seed_random(const std::string & seed, const unsigned int call_count = 0);
49 
50  /**
51  * Resets the random to the 0 calls and the seed to the random
52  * this way we stay in the same sequence but don't have a lot
53  * calls. Used when moving to the next scenario.
54  */
55  void rotate_random();
56 
59  unsigned int get_random_calls() const { return random_calls_; }
60 
61  //Comparisons, mainly used for testing
62  bool operator== (const mt_rng &other) const;
63  bool operator!= (const mt_rng &other) const
64  { return !operator==(other); }
65 
66 private:
67  /** Initial seed for the pool. */
69 
70  /** State for the random pool (boost mersenne twister random generator). */
71  boost::mt19937 mt_;
72 
73  /** Number of time a random number is generated. */
74  unsigned int random_calls_;
75 
76  /** On my local version of boost::random, I can use mt_.discard to discard a number of rng results.
77  In older versions this seems to be unavailable. I'm implementing as a private method of mt_rng,
78  following description here: http://www.boost.org/doc/libs/1_51_0/doc/html/boost/random/mersenne_twister_engine.html#id1408119-bb
79  */
80  void discard(const unsigned int call_count);
81 
82  /**
83  * Seeds the random pool. This is the old version, I would like to mark this private.
84  *
85  * @param seed The initial value for the random engine.
86  * @param call_count Upon loading we need to restore the state at saving
87  * so set the number of times a random number is
88  * generated for replays the orginal value is
89  * required.
90  */
91  void seed_random(const uint32_t seed, const unsigned int call_count = 0);
92 };
93 
94 } // ends rand_rng namespace
95 
96 #endif
void rotate_random()
Resets the random to the 0 calls and the seed to the random this way we stay in the same sequence but...
Definition: mt_rng.cpp:74
uint32_t random_seed_
Initial seed for the pool.
Definition: mt_rng.hpp:68
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
void discard(const unsigned int call_count)
On my local version of boost::random, I can use mt_.discard to discard a number of rng results...
Definition: mt_rng.cpp:108
void seed_random(const std::string &seed, const unsigned int call_count=0)
Same as uint32_t version, but uses a stringstream to convert given hex string.
Definition: mt_rng.cpp:88
std::string get_random_seed_str() const
Definition: mt_rng.cpp:99
unsigned int get_random_calls() const
Definition: mt_rng.hpp:59
bool operator==(const mt_rng &other) const
Definition: mt_rng.cpp:57
bool operator!=(const mt_rng &other) const
Definition: mt_rng.hpp:63
uint32_t get_random_seed() const
Definition: mt_rng.hpp:57
unsigned int random_calls_
Number of time a random number is generated.
Definition: mt_rng.hpp:74
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
boost::mt19937 mt_
State for the random pool (boost mersenne twister random generator).
Definition: mt_rng.hpp:71