this class does not give synced random results derived classes might do. More...
#include <random.hpp>
Public Types | |
using | result_type = uint32_t |
Public Member Functions | |
rng () | |
uint32_t | next_random () |
Provides the next random draw. More... | |
virtual | ~rng () |
unsigned int | get_random_calls () const |
Provides the number of random calls to the rng in this context. More... | |
int | get_random_int (int min, int max) |
bool | get_random_bool (double probability) |
This helper method returns true with the probability supplied as a parameter. More... | |
double | get_random_double () |
This helper method returns a floating-point number in the range [0,1[. More... | |
template<typename T > | |
T::difference_type | get_random_element (T first, T last) |
This helper method selects a random element from a container of floating-point numbers. More... | |
uint32_t | operator() () |
virtual bool | is_networked () const |
Is this random source networked? If it is it's very important we do actually use this random source to stay in-sync. More... | |
Static Public Member Functions | |
static rng & | default_instance () |
Protected Member Functions | |
virtual uint32_t | next_random_impl ()=0 |
Protected Attributes | |
unsigned int | random_calls_ |
Private Types | |
using | base = std::numeric_limits< uint32_t > |
Private Member Functions | |
int | get_random_int_in_range_zero_to (int max) |
Does the hard work of get_random_int. More... | |
this class does not give synced random results derived classes might do.
Definition at line 27 of file random.hpp.
|
private |
Definition at line 29 of file random.hpp.
using randomness::rng::result_type = uint32_t |
Definition at line 78 of file random.hpp.
randomness::rng::rng | ( | ) |
Definition at line 62 of file random.cpp.
|
virtual |
Definition at line 68 of file random.cpp.
|
static |
Definition at line 73 of file random.cpp.
Referenced by unit_animation_component::choose_animation(), choose_track(), ai::default_recruitment::recruitment::get_best_leader_from_ratio_scores(), leave_synced_context::leave_synced_context(), unit_animation::matches(), editor::editor_action_shuffle_area::perform_without_undo(), pick_one(), display::reinit_flags_for_team(), play_controller::select_music(), gui2::tip_of_the_day::shuffle(), and soundsource::positional_source::update().
bool randomness::rng::get_random_bool | ( | double | probability | ) |
This helper method returns true with the probability supplied as a parameter.
probability | The probability of returning true, from 0 to 1. |
Definition at line 136 of file random.cpp.
References get_random_double().
unsigned int randomness::rng::get_random_calls | ( | ) | const |
Provides the number of random calls to the rng in this context.
Note that this may be different from the number of random calls to the underlying rng, and to the random_calls number in save files!
Definition at line 79 of file random.cpp.
References random_calls_.
double randomness::rng::get_random_double | ( | ) |
This helper method returns a floating-point number in the range [0,1[.
Definition at line 111 of file random.cpp.
References next_random().
Referenced by ai::default_recruitment::recruitment::do_randomness(), get_random_bool(), and get_random_element().
T::difference_type randomness::rng::get_random_element | ( | T | first, |
T | last | ||
) |
This helper method selects a random element from a container of floating-point numbers.
Every number has a probability to be selected equal to the number itself (e.g. a number of 0.1 is selected with a probability of 0.1). The sum of numbers should be one.
first | Iterator to the beginning of the container |
last | Iterator to the end of the container |
Definition at line 111 of file random.hpp.
References get_random_double().
|
inline |
This helper method provides a random int from the underlying generator,
using results of next_random in a manner guaranteed to be cross platform. The result will be random in range [min,max] inclusive.
min | The minimum value produced. |
max | The maximum value produced. |
Definition at line 51 of file random.hpp.
References get_random_int_in_range_zero_to().
Referenced by unit_animation_component::choose_animation(), choose_track(), wfl::operator_expression::dice_roll(), generate_gender(), unit::generate_traits(), ai::default_recruitment::recruitment::get_random_pattern_type_if_exists(), ai::helper_advance_unit(), pick_one(), display::reinit_flags_for_team(), play_controller::select_music(), saved_game::set_random_seed(), and soundsource::positional_source::update().
|
private |
Does the hard work of get_random_int.
This code is based on the boost implementation of uniform_smallint.
The result will be random in range [0,max] inclusive.
max | The maximum value produced. |
http://www.boost.org/doc/libs/1_55_0/boost/random/uniform_smallint.hpp Using that code would be ideal, except that boost, and C++11, do not guarantee that it will work the same way on all platforms, or that the results may not be different in future versions of the library. The simplified version I have written should work the same on all platforms, which is the most important thing for us. The existence of "modulo bias" seems less important when we have moved to std::mt19937, since it guarantees that there are no "bad bits" and has a very large range.
If a standard cross platform version becomes available then this should be replaced.
Definition at line 105 of file random.cpp.
References next_random().
Referenced by get_random_int().
|
inlinevirtual |
Is this random source networked? If it is it's very important we do actually use this random source to stay in-sync.
Reimplemented in randomness::synced_rng.
Definition at line 89 of file random.hpp.
uint32_t randomness::rng::next_random | ( | ) |
Provides the next random draw.
This is raw PRG output.
Definition at line 84 of file random.cpp.
References next_random_impl(), and random_calls_.
Referenced by get_random_double(), get_random_int_in_range_zero_to(), context_free_grammar_generator::init_seed(), lua_mathx::intf_random(), markov_generate_name(), operator()(), and tod_manager::resolve_random().
|
protectedpure virtual |
Implemented in randomness::synced_rng, randomness::rng_proxy, and randomness::rng_deterministic.
Referenced by next_random().
|
inline |
Definition at line 81 of file random.hpp.
References next_random().
|
protected |
Definition at line 93 of file random.hpp.
Referenced by get_random_calls(), and next_random().