public member function
<random>

std::linear_congruential_engine::seed

(1)
void seed (result_type val = default_seed);
(2)
template <class Sseq>void seed (Sseq& q);
Seed engine
Re-initializes the internal state value:
  • For version (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).
  • For version (2), the function calls q.generate on an array of four elements (plus an additional element for each time the number of bits to represent m surpasses 32). Then it discards the first three elements obtained, and uses the remainder to construct a single value of type result_type, which is used to initialize the engine as if version (1) was called with it.

Parameters

val
A seeding value.
result_type is a member type, defined as an alias of the first class template parameter (UIntType).
default_seed is a member constant, defined as 1u.
q
A seed sequence object, such as an object of type seed_seq.
Sseq shall be a seed sequence class, with a generate member function.

Return value

None

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// linear_congruential_engine::seed example
#include <iostream>
#include <chrono>
#include <random>

int main ()
{
  typedef std::chrono::high_resolution_clock myclock;
  myclock::time_point beginning = myclock::now();

  // obtain a seed from a user string:
  std::string str;
  std::cout << "Please, enter a seed: ";
  std::getline(std::cin,str);
  std::seed_seq seed1 (str.begin(),str.end());

  // obtain a seed from the timer
  myclock::duration d = myclock::now() - beginning;
  unsigned seed2 = d.count();

  std::minstd_rand0 generator (seed1); // minstd_rand0 is a standard linear_congruential_engine
  std::cout << "Your seed produced: " << generator() << std::endl;

  generator.seed (seed2);
  std::cout << "A time seed produced: " << generator() << std::endl;

  return 0;
}

Possible output:
Please, enter a seed: Lehmer
Your seed produced: 606418532
A time seed produced: 1671690313


Complexity

Constant.

See also