The Battle for Wesnoth  1.15.2+dev
seed_rng.cpp
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 /* This file selects a seed source -- "nondeterministic" random number
16  generator in boost documentation. It should be a wrapper for
17  boost::random_device on platforms where this is available, otherwise
18  it should most likely be the system time.
19 */
20 
21 #include "seed_rng.hpp"
22 
23 /*****
24  Use preprocessor tests to decide whether to try to include and
25  use boost random device, or fallback to system time.
26  *****/
27 
28 #define SEED_RNG_USE_BOOST_RANDOM_DEVICE
29 
30 /*****
31  End preprocessor checks
32  *****/
33 
34 #ifdef SEED_RNG_USE_BOOST_RANDOM_DEVICE
35 #include <boost/nondet_random.hpp>
36 #else
37 #include <ctime>
38 #endif
39 
40 #include <sstream>
41 #include <iomanip>
42 
43 namespace seed_rng {
44 
45  uint32_t next_seed() {
46  #ifdef SEED_RNG_USE_BOOST_RANDOM_DEVICE
47  static boost::random_device rnd_;
48  return rnd_();
49  #else
50  return static_cast<uint32_t> (std::time(0));
51  #endif
52  }
53 
54  std::string next_seed_str() {
55  uint32_t random_seed_ = next_seed();
56  std::stringstream stream;
57  stream << std::setfill('0') << std::setw(sizeof(uint32_t)*2) << std::hex << random_seed_;
58 
59  return stream.str();
60  }
61 
62 } //ends seed_rng namespace
std::string next_seed_str()
Definition: seed_rng.cpp:54
uint32_t next_seed()
Definition: seed_rng.cpp:45