Thursday, May 15, 2025

default_random_engine

Overview
This is a random number engine class that generates pseudo-random numbers.

Details
Its implementation is platform specific.  Here it's assumed that linear_congruential_engine  is used.

member types
Following properties are defined.
NameDefinition
uint_least32_t result_typeThe type of the numbers generated

Constructor
NameDescription
  1. default_random_engine
    ( result_type val = default_seed )
  2. template <class Sseq>
    default_random_engine (Sseq& q )
Constructs a linear_congruential_engine object, and initializes its internal state value:
  1. The state value is set to val%modulus (unless both val and increment are multiples of modulus, in which case the state value is set to default_seed).
  2. The function calls q.generate on a temporary array to construct a single value of type result_type, which is used to initialize the engine as if version (1) was called with it.
Example
    //1
    default_random_engine d(21092018u);

    //2
    string s = "Khrisha Rao";
    seed_seq seed (s.begin(),s.end());
    default_random_engine d2(seed);

Methods
NameDescription
result_type default_random_engine ::min()

Returns the minimum value potentially generated by the random-number engine.

Example
    default_random_engine();

    //prints 1
    cout <<  default_random_engine::min() << endl;
 result_type default_random_engine ::max()
Returns the maximum value potentially generated by the random-number engine.

Example
    default_random_engine();

    //prints 2147483646
    cout <<  default_random_engine::max() << endl;
  1. void seed
    (result_type val = default_seed )
  2. template <class Sseq>
    void seed(Sseq& q )
Reinitializes its internal state value:
  1. The state value is set to val%modulus (unless both val and increment are multiples of modulus, in which case the state value is set to default_seed).
  2. The function calls q.generate on a temporary array to construct a single value of type result_type, which is used to initialize the engine as if version (1) was called with it.
Example
    //1
    default_random_engine d;
    d.seed(21092018u);

    //2
    string s = "Khrisha Rao";
    seed_seq seed (s.begin(),s.end());
    d.seed(seed);
result_type operator()()
Returns a new random number. The function advances the internal state by one, which modifies the state value.

Example
    default_random_engine d;
    //prints 16807
    cout << d() << endl;
void discard
(unsigned long long z)
Advances the internal state by z notches, as if operator() was called z times, but without generating any numbers in the process.
The effects on the state sequence are the same as if the transition algorithm was applied as many times as notches advanced.

Example
    default_random_engine d;
    d.discard(300);
    //prints 1495170053
    cout << d() << endl;


External Methods
NameDescription
ostream& operator<< 
(ostream& os, const  default_random_engine& d )

Saves internal state of d to os.

Example
    default_random_engine d(400);
    stringstream ss;
    ss << d << endl;
    //prints 400
    cout << d << endl;
istream& operator>>
(istream&  is, default_random_engine& d )
Restores internal state of d from is. 

Example
    default_random_engine d(400);
    stringstream ss;
    ss << d << endl;
    //prints 400
    cout << d << endl;

    //prints 400
    ss >> d;
    cout << d << endl;
bool operator==
(const default_random_engine&  d,  const  default_random_engine& d2)
Returns true if d and d2 are the same.
bool operator!=
(const  default_random_engine& d, const  default_random_engine& d2)
Returns true if d and d2 are not the same.

No comments:

Post a Comment