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