The Battle for Wesnoth  1.17.0-dev
mt_rng.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "mt_rng.hpp"
17 #include "seed_rng.hpp"
18 #include "config.hpp"
19 #include "log.hpp"
20 #include <sstream>
21 #include <iomanip>
22 static lg::log_domain log_random("random");
23 #define DBG_RND LOG_STREAM(debug, log_random)
24 #define LOG_RND LOG_STREAM(info, log_random)
25 #define WRN_RND LOG_STREAM(warn, log_random)
26 #define ERR_RND LOG_STREAM(err, log_random)
27 
28 
29 namespace randomness
30 {
31 
33  random_seed_(seed_rng::next_seed()),
34  mt_(random_seed_),
35  random_calls_(0)
36 {
37 }
38 
39 
40 mt_rng::mt_rng(uint32_t seed)
41  : random_seed_(seed)
42  , mt_(random_seed_)
43  , random_calls_(0)
44 {
45 }
46 
47 
48 mt_rng::mt_rng(const config& cfg) :
49  random_seed_(42),
50  mt_(random_seed_), //we don't have the seed at construction time, we have to seed after construction in this case. Constructing an mt19937 is somewhat expensive, apparently has about 2kb of private memory.
51  random_calls_(0)
52 {
53  config::attribute_value seed = cfg["random_seed"];
54  seed_random(seed.str(), cfg["random_calls"].to_int(0));
55 }
56 
57 bool mt_rng::operator== (const mt_rng & other) const {
58  return random_seed_ == other.random_seed_
59  && random_calls_ == other.random_calls_
60  && mt_ == other.mt_;
61 }
62 
64 {
65  uint32_t result = mt_();
66  ++random_calls_;
67  DBG_RND << "pulled user random " << result
68  << " for call " << random_calls_
69  << " with seed " << std::hex << random_seed_ << std::endl;
70 
71  return result;
72 }
73 
75 {
76  seed_random(mt_(),0);
77 }
78 
79 void mt_rng::seed_random(const uint32_t seed, const unsigned int call_count)
80 {
81  random_seed_ = seed;
82  mt_.seed(random_seed_);
83  mt_.discard(call_count);
84  random_calls_ = call_count;
85  DBG_RND << "Seeded random with " << std::hex << random_seed_ << std::dec << " with "
86  << random_calls_ << " calls." << std::endl;
87 }
88 
89 void mt_rng::seed_random(const std::string & seed_str, const unsigned int call_count)
90 {
91  uint32_t new_seed;
92  std::istringstream s(seed_str);
93  if (!(s >> std::hex >> new_seed)) {
94  new_seed = 42;
95  DBG_RND << "Failed to seed a random number generator using seed string '" << seed_str << "', it could not be parsed to hex. Seeding with 42.\n";
96  }
97  seed_random(new_seed, call_count);
98 }
99 
100 std::string mt_rng::get_random_seed_str() const {
101  std::stringstream stream;
102  stream << std::setfill('0');
103  stream << std::setw(sizeof(uint32_t)*2);
104  stream << std::hex;
105  stream << random_seed_;
106  return stream.str();
107 }
108 
109 } // ends randomness namespace
std::mt19937 mt_
State for the random pool (mersenne twister random generator).
Definition: mt_rng.hpp:68
uint32_t random_seed_
Initial seed for the pool.
Definition: mt_rng.hpp:65
Variant for storing WML attributes.
#define DBG_RND
Definition: mt_rng.cpp:23
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
unsigned int random_calls_
Number of time a random number is generated.
Definition: mt_rng.hpp:71
bool operator==(const mt_rng &other) const
Definition: mt_rng.cpp:57
Definitions for the interface to Wesnoth Markup Language (WML).
std::string get_random_seed_str() const
Definition: mt_rng.cpp:100
static map_location::DIRECTION s
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
static lg::log_domain log_random("random")
uint32_t next_seed()
Definition: seed_rng.cpp:32
Standard logging facilities (interface).
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:89
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::string str(const std::string &fallback="") const