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