RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Public Types | Public Member Functions | Protected Attributes | List of all members
RandomLib::RandomSeed Class Referenceabstract

A base class for random generators. More...

#include <RandomLib/RandomSeed.hpp>

Inheritance diagram for RandomLib::RandomSeed:
RandomLib::RandomEngine< Algorithm, Mixer >

Public Types

typedef Random_u32 u32
 
typedef Random_u64 u64
 
typedef RandomType
< 32, unsigned long > 
seed_t
 
typedef seed_t::type seed_type
 

Public Member Functions

virtual ~RandomSeed ()=0 throw ()
 
Resetting the seed
template<typename IntType >
void Reseed (const std::vector< IntType > &v)
 
template<typename InputIterator >
void Reseed (InputIterator a, InputIterator b)
 
void Reseed (seed_type n)
 
void Reseed ()
 
void Reseed (const std::string &s)
 
Examining the seed
const std::vector< seed_type > & Seed () const throw ()
 
std::string SeedString () const
 
Resetting the random seed
virtual void Reset ()=0 throw ()
 

Static Public Member Functions

Static functions for seed management
static seed_type SeedWord ()
 
static std::vector< seed_typeSeedVector ()
 
template<typename IntType >
static std::string VectorToString (const std::vector< IntType > &v)
 
static std::vector< seed_typeStringToVector (const std::string &s)
 

Protected Attributes

std::vector< seed_type_seed
 

Detailed Description

A base class for random generators.

This provides facilities for managing the seed and for converting the seed into random generator state.

The seed is taken to be a vector of unsigned longs of arbitrary length. (Only the low 32 bit of each element of the vector are used.) The class provides several methods for setting the seed, static functions for producing "random" and "unique" seeds, and facilities for converting the seed to a string so that it can be printed easily.

The seeding algorithms are those used by MT19937 with some modifications to make all states accessible and to minimize the likelihood of different seeds giving the same state.

Finally some low-level routines are provided to facilitate the creation of I/O methods for the random generator.

A random generator class can be written based on this class. The generator class would use the base class methods for setting the seed and for converting the seed into state. It would provide the machinery for advancing the state and for producing random data. It is also responsible for the routine to save and restore the generator state (including the seed).

Written by Charles Karney charl.nosp@m.es@k.nosp@m.arney.nosp@m..com and licensed under the MIT/X11 License. The seeding algorithms are adapted from those of MT19937. For more information, see http://randomlib.sourceforge.net/

Definition at line 62 of file RandomSeed.hpp.

Member Typedef Documentation

Definition at line 64 of file RandomSeed.hpp.

Definition at line 65 of file RandomSeed.hpp.

typedef RandomType<32, unsigned long> RandomLib::RandomSeed::seed_t

A type large enough to hold the seed words. This is needs to hold 32 bits and is an unsigned long for portability.

Definition at line 72 of file RandomSeed.hpp.

Definition at line 73 of file RandomSeed.hpp.

Constructor & Destructor Documentation

RandomLib::RandomSeed::~RandomSeed ( )
throw (
)
inlinepure virtual

Definition at line 243 of file RandomSeed.hpp.

Member Function Documentation

template<typename IntType >
void RandomLib::RandomSeed::Reseed ( const std::vector< IntType > &  v)
inline

Set the seed to a vector v. Only the low 32 bits of each element are used.

Template Parameters
IntTypethe integral type of the elements of the vector.
Parameters
[in]vthe vector of elements.

Definition at line 86 of file RandomSeed.hpp.

Referenced by RandomLib::RandomEngine< Algorithm, Mixer >::SelfTest().

template<typename InputIterator >
void RandomLib::RandomSeed::Reseed ( InputIterator  a,
InputIterator  b 
)
inline

Set the seed to [a, b) from a pair of iterators. The iterator must produce results which can be converted into seed_type. Only the low 32 bits of each element are used.

Template Parameters
InputIteratorthe type of the iterator.
Parameters
[in]athe beginning iterator.
[in]bthe ending iterator.

Definition at line 99 of file RandomSeed.hpp.

void RandomLib::RandomSeed::Reseed ( seed_type  n)
inline

Set the seed to [n]. Only the low 32 bits of n are used.

Parameters
[in]nthe new seed to use.

Definition at line 113 of file RandomSeed.hpp.

void RandomLib::RandomSeed::Reseed ( )
inline

Set the seed to [SeedVector()]. This is the standard way to reseed with a "unique" seed.

Definition at line 124 of file RandomSeed.hpp.

References Reseed().

Referenced by Reseed().

void RandomLib::RandomSeed::Reseed ( const std::string &  s)
inline

Set the seed from the string s using Random::StringToVector.

Parameters
[in]sthe string to be decoded into a seed.

Definition at line 130 of file RandomSeed.hpp.

const std::vector<seed_type>& RandomLib::RandomSeed::Seed ( ) const
throw (
)
inline

Return reference to the seed vector (read-only).

Returns
the seed vector.

Definition at line 147 of file RandomSeed.hpp.

std::string RandomLib::RandomSeed::SeedString ( ) const
inline

Format the current seed suitable for printing.

Returns
the seedd as a string.

Definition at line 153 of file RandomSeed.hpp.

Referenced by RandomLib::RandomEngine< Algorithm, Mixer >::SelfTest().

virtual void RandomLib::RandomSeed::Reset ( )
throw (
)
pure virtual

Resets the sequence to its just-seeded state. This needs to be declared virtual here so that the Reseed functions can call it after saving the seed.

Implemented in RandomLib::RandomEngine< Algorithm, Mixer >.

RandomSeed::seed_type RandomLib::RandomSeed::SeedWord ( )
static

Return a 32 bits of data suitable for seeding the random generator. The result is obtained by combining data from /dev/urandom, gettimeofday, time, and getpid to provide a reasonably "random" word of data. Usually, it is safer to seed the random generator with SeedVector() instead of SeedWord().

Returns
a single "more-or-less random" seed_type to be used as a seed.

Definition at line 161 of file Random.cpp.

References RandomLib::RandomType< bits, UIntType >::cast(), RandomLib::RandomType< bits, UIntType >::CheckSum(), SeedVector(), and STATIC_ASSERT.

std::vector< RandomSeed::seed_type > RandomLib::RandomSeed::SeedVector ( )
static

Return a vector of unsigned longs suitable for seeding the random generator. The vector is almost certainly unique; however, the results of successive calls to Random::SeedVector() will be correlated. If several Random objects are required within a single program execution, call Random::SeedVector once, print it out (!), push_back additional data to identify the instance (e.g., loop index, thread ID, etc.), and use the result to seed the Random object. The number of elements included in the vector may depend on the operating system. Additional elements may be added in future versions of this library.

Returns
a "unique" vector of seed_type to be uses as a seed.

Definition at line 184 of file Random.cpp.

References RandomLib::RandomType< bits, UIntType >::cast().

Referenced by SeedWord().

template<typename IntType >
static std::string RandomLib::RandomSeed::VectorToString ( const std::vector< IntType > &  v)
inlinestatic

Convert a vector into a string suitable for printing or as an argument for Random::Reseed(const std::string& s).

Template Parameters
IntTypethe integral type of the elements of the vector.
Parameters
[in]vthe vector to be converted.
Returns
the resulting string.

Definition at line 205 of file RandomSeed.hpp.

std::vector< RandomSeed::seed_type > RandomLib::RandomSeed::StringToVector ( const std::string &  s)
static

Convert a string into a vector of seed_type suitable for printing or as an argument for Random::Reseed(const std::vector<seed_type>& v). Reads consecutive digits in string. Thus "[1,2,3]" => [1,2,3]; "-0.123e-4" => [0,123,4], etc. strtoul understands C's notation for octal and hexadecimal, for example "012 10 0xa" => [10,10,10]. Reading of a number stops at the first illegal character for the base. Thus "2006-04-08" => 2006,4,0,8. Note that input numbers greater than ULONG_MAX overflow to ULONG_MAX, which probably will result in the number being interpreted as LONG_MASK.

Parameters
[in]sthe string to be converted.
Returns
the resulting vector of seed_type.

Definition at line 227 of file Random.cpp.

References RandomLib::RandomType< bits, UIntType >::cast().

Member Data Documentation

std::vector<seed_type> RandomLib::RandomSeed::_seed
protected

The seed vector

Definition at line 239 of file RandomSeed.hpp.

Referenced by RandomLib::RandomEngine< Algorithm, Mixer >::Load().


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