公有成員函式 (public member function)
<random>
(1) | void seed (result_type val = default_seed); |
---|
(2) | template <class Sseq>void seed (Sseq& q); |
---|
播種引擎
重新初始化內部狀態值
- 對於版本(1),狀態值設定為val % modulus(除非 val 和increment都是modulus的倍數,在這種情況下,狀態值設定為default_seed).
- 對於版本(2),該函式在一個包含四個元素(外加一個額外元素,用於表示 m 所需的位數超過 32)的陣列上呼叫 `q.generate`。然後它丟棄獲得的前三個元素,並使用剩餘的元素構造一個型別為result_type的值,該值用於初始化引擎,就如同呼叫版本(1) 時使用它一樣。
引數
- val
- 一個種子值。
result_type是一個成員型別,定義為第一個類模板引數的別名 (UIntType).
default_seed是一個成員常量,定義為1u.
- q
- 一個種子序列物件,例如seed_seq型別的物件。
Sseq應為具有generate成員函式的種子序列類。
示例
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;
}
|
可能的輸出
Please, enter a seed: Lehmer
Your seed produced: 606418532
A time seed produced: 1671690313
|