The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
seed_rng.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #include <boost/version.hpp>
23 
24 /*****
25  Use preprocessor tests to decide whether to try to include and
26  use boost random device, or fallback to system time.
27  *****/
28 
29 #define SEED_RNG_USE_BOOST_RANDOM_DEVICE
30 
31 //Boost does not support random device on windows before v 1.43.0
32 //http://www.boost.org/users/history/version_1_43_0.html
33 #if (defined(_WIN32) && (BOOST_VERSION < 104300))
34 #undef SEED_RNG_USE_BOOST_RANDOM_DEVICE
35 #endif
36 
37 /*****
38  End preprocessor checks
39  *****/
40 
41 #ifdef SEED_RNG_USE_BOOST_RANDOM_DEVICE
42 #include <boost/nondet_random.hpp>
43 #else
44 #include <ctime>
45 #endif
46 
47 #include <sstream>
48 #include <iomanip>
49 
50 namespace seed_rng {
51 
53  #ifdef SEED_RNG_USE_BOOST_RANDOM_DEVICE
54  static boost::random_device rnd_;
55  return rnd_();
56  #else
57  return static_cast<uint32_t> (std::time(0));
58  #endif
59  }
60 
62  uint32_t random_seed_ = next_seed();
63  std::stringstream stream;
64  stream << std::setfill('0') << std::setw(sizeof(uint32_t)*2) << std::hex << random_seed_;
65 
66  return stream.str();
67  }
68 
69 } //ends seed_rng namespace
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
GLuint GLuint stream
Definition: glew.h:5239
std::string next_seed_str()
Definition: seed_rng.cpp:61
uint32_t next_seed()
Definition: seed_rng.cpp:52
GLsizei const GLcharARB ** string
Definition: glew.h:4503