The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
random_new.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by David White <[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 #ifndef RANDOM_NEW_H_INCLUDED
15 #define RANDOM_NEW_H_INCLUDED
16 
17 #include <cstdlib> //needed for RAND_MAX
18 #include <boost/cstdint.hpp>
19 
20 using boost::uint32_t;
21 
22 namespace random_new
23 {
24  /**
25  this class does not give synced random results derived classes might do.
26  */
27  class rng
28  {
29  public:
30  rng();
31  /**
32  * Provides the next random draw. This is raw PRG output.
33  */
35  virtual ~rng();
36  /**
37  * Provides the number of random calls to the rng in this context.
38  * Note that this may be different from the number of random calls to
39  * the underlying rng, and to the random_calls number in save files!
40  */
41  unsigned int get_random_calls();
42 
43  /**
44  * This helper method provides a random int from the underlying generator,
45  * using results of next_random in a manner guaranteed to be cross platform.
46  * The result will be random in range [min,max] inclusive.
47  * @param min The minimum value produced.
48  * @param max The maximum value produced.
49  */
50  int get_random_int(int min, int max)
51  { return min + get_random_int_in_range_zero_to(max - min); }
52  static rng& default_instance();
53  protected:
54  virtual uint32_t next_random_impl() = 0;
55  unsigned int random_calls_;
56 
57  private:
58  /** Does the hard work of get_random_int.
59  * The result will be random in range [0,max] inclusive.
60  * @param max The maximum value produced.
61  */
63  };
64 
65  /**
66  This generator is automatically synced during synced context.
67  Calling this rng during a synced context automatically makes undoing impossible.
68  Outside a synced context this has the same effect as rand()
69  */
70  extern rng* generator;
71 }
72 #endif
virtual uint32_t next_random_impl()=0
rng * generator
This generator is automatically synced during synced context.
Definition: random_new.cpp:52
unsigned int get_random_calls()
Provides the number of random calls to the rng in this context.
Definition: random_new.cpp:71
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
static rng & default_instance()
Definition: random_new.cpp:65
virtual ~rng()
Definition: random_new.cpp:60
int get_random_int(int min, int max)
This helper method provides a random int from the underlying generator, using results of next_random...
Definition: random_new.hpp:50
uint32_t next_random()
Provides the next random draw.
Definition: random_new.cpp:76
unsigned int random_calls_
Definition: random_new.hpp:55
this class does not give synced random results derived classes might do.
Definition: random_new.hpp:27
int get_random_int_in_range_zero_to(int max)
Does the hard work of get_random_int.
Definition: random_new.cpp:97