The Battle for Wesnoth  1.15.2+dev
mt_rng.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include <cstdint>
18 #include <random>
19 
20 class config;
21 
22 namespace randomness
23 {
24 
25 /*
26  This class provides an interface, similar to simple_rng, to the mt19937 generator.
27 */
28 
29 class mt_rng
30 {
31 public:
32  mt_rng();
33  explicit mt_rng(const config& cfg);
34  explicit mt_rng(uint32_t seed);
35  /** Get a new random number. */
36  uint32_t get_next_random();
37 
38  /**
39  * Same as uint32_t version, but uses a stringstream to convert given
40  * hex string.
41  * @param seed A hex string. Should not have 0x leading.
42  * @param call_count Value to set internal call counter to after seeding.
43  */
44  void seed_random(const std::string & seed, const unsigned int call_count = 0);
45 
46  /**
47  * Resets the random to the 0 calls and the seed to the random
48  * this way we stay in the same sequence but don't have a lot
49  * calls. Used when moving to the next scenario.
50  */
51  void rotate_random();
52 
53  uint32_t get_random_seed() const { return random_seed_; }
54  std::string get_random_seed_str() const;
55  unsigned int get_random_calls() const { return random_calls_; }
56 
57  //Comparisons, mainly used for testing
58  bool operator== (const mt_rng &other) const;
59  bool operator!= (const mt_rng &other) const
60  { return !operator==(other); }
61 
62 private:
63  /** Initial seed for the pool. */
64  uint32_t random_seed_;
65 
66  /** State for the random pool (mersenne twister random generator). */
67  std::mt19937 mt_;
68 
69  /** Number of time a random number is generated. */
70  unsigned int random_calls_;
71 
72  /**
73  * Seeds the random pool. This is the old version, I would like to mark this private.
74  *
75  * @param seed The initial value for the random engine.
76  * @param call_count Upon loading we need to restore the state at saving
77  * so set the number of times a random number is
78  * generated for replays the original value is
79  * required.
80  */
81  void seed_random(const uint32_t seed, const unsigned int call_count = 0);
82 };
83 
84 } // ends randomness namespace
std::mt19937 mt_
State for the random pool (mersenne twister random generator).
Definition: mt_rng.hpp:67
uint32_t random_seed_
Initial seed for the pool.
Definition: mt_rng.hpp:64
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:62
unsigned int random_calls_
Number of time a random number is generated.
Definition: mt_rng.hpp:70
bool operator==(const mt_rng &other) const
Definition: mt_rng.cpp:56
std::string get_random_seed_str() const
Definition: mt_rng.cpp:99
bool operator!=(const mt_rng &other) const
Definition: mt_rng.hpp:59
uint32_t get_random_seed() const
Definition: mt_rng.hpp:53
unsigned int get_random_calls() const
Definition: mt_rng.hpp:55
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:73
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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68