The Battle for Wesnoth  1.19.5+dev
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | List of all members
randomness::rng Class Referenceabstract

this class does not give synced random results derived classes might do. More...

#include <random.hpp>

Inheritance diagram for randomness::rng:

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 rngdefault_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...
 

Detailed Description

this class does not give synced random results derived classes might do.

Definition at line 27 of file random.hpp.

Member Typedef Documentation

◆ base

using randomness::rng::base = std::numeric_limits<uint32_t>
private

Definition at line 29 of file random.hpp.

◆ result_type

using randomness::rng::result_type = uint32_t

Definition at line 78 of file random.hpp.

Constructor & Destructor Documentation

◆ rng()

randomness::rng::rng ( )

Definition at line 62 of file random.cpp.

◆ ~rng()

randomness::rng::~rng ( )
virtual

Definition at line 68 of file random.cpp.

Member Function Documentation

◆ default_instance()

rng & randomness::rng::default_instance ( )
static

◆ get_random_bool()

bool randomness::rng::get_random_bool ( double  probability)

This helper method returns true with the probability supplied as a parameter.

Parameters
probabilityThe probability of returning true, from 0 to 1.

Definition at line 136 of file random.cpp.

References get_random_double().

◆ get_random_calls()

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_.

◆ get_random_double()

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().

◆ get_random_element()

template<typename T >
T::difference_type randomness::rng::get_random_element ( first,
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.

Parameters
firstIterator to the beginning of the container
lastIterator to the end of the container
Returns
The index of the selected number

Definition at line 111 of file random.hpp.

References get_random_double().

◆ get_random_int()

int randomness::rng::get_random_int ( int  min,
int  max 
)
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.

Parameters
minThe minimum value produced.
maxThe 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().

◆ get_random_int_in_range_zero_to()

int randomness::rng::get_random_int_in_range_zero_to ( int  max)
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.

Parameters
maxThe 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().

◆ is_networked()

virtual bool randomness::rng::is_networked ( ) const
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.

◆ next_random()

uint32_t randomness::rng::next_random ( )

◆ next_random_impl()

virtual uint32_t randomness::rng::next_random_impl ( )
protectedpure virtual

◆ operator()()

uint32_t randomness::rng::operator() ( )
inline

Definition at line 81 of file random.hpp.

References next_random().

Member Data Documentation

◆ random_calls_

unsigned int randomness::rng::random_calls_
protected

Definition at line 93 of file random.hpp.

Referenced by get_random_calls(), and next_random().


The documentation for this class was generated from the following files: