The Battle for Wesnoth  1.19.0-dev
random_synced.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by David White <dave@whitevine.net>
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 "random_synced.hpp"
17 #include "log.hpp"
18 
19 static lg::log_domain log_random("random");
20 #define DBG_RND LOG_STREAM(debug, log_random)
21 #define LOG_RND LOG_STREAM(info, log_random)
22 #define WRN_RND LOG_STREAM(warn, log_random)
23 #define ERR_RND LOG_STREAM(err, log_random)
24 
25 namespace randomness
26 {
27  synced_rng::synced_rng(std::function<std::string()> seed_generator)
28  : has_valid_seed_(false), seed_generator_(seed_generator), gen_()
29  {
30  }
32  {
33  if(!has_valid_seed_)
34  {
35  initialize();
36  }
37  //getting here means random was called form inside a synced context.
38  uint32_t retv = gen_.get_next_random();
39 
40  LOG_RND << "randomness::rng::next_random_impl returned " << retv;
41  return retv;
42  }
43 
45  {
46  std::string new_seed = seed_generator_();
47  gen_.seed_random(new_seed, 0);
48  has_valid_seed_ = true;
49  }
50 
52  {
53 
54  }
55 
57  {
58  return true;
59  }
60 }
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
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
virtual uint32_t next_random_impl()
synced_rng(std::function< std::string()> seed_generator)
virtual bool is_networked() const
Is this random source networked? If it is it's very important we do actually use this random source t...
std::function< std::string()> seed_generator_
Standard logging facilities (interface).
#define LOG_RND
static lg::log_domain log_random("random")