公共成员函数
<random>
(1) | void seed(); |
---|
(2) | void seed (result_type val); |
---|
(3) | template <class Sseq>void seed (Sseq& q); |
---|
Seed base engine (为基础引擎设置种子)
通过调用其 base,重新初始化 base 引擎的状态seedmember function with the same argument (if any). (同名参数(如果存在)成员函数,重新初始化其状态。)
参数
- val
- 一个播种值。此值传递给 base 引擎的构造函数。
result_type是一个成员类型,定义为由 base 生成的元素类型的别名。
- 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 29
|
// discard_block_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();
// ranlux24 is a standard instantitation of discard_block_engine:
std::ranlux24 generator (seed1);
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: Cooper
Your seed produced: 14845138
A time seed produced: 12807810
|