开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第1天,点击查看活动详情
此标头引入了随机数生成功能。该库允许使用生成器和分布的组合生成随机数。
发电机
一、伪随机数引擎: 他们使用一种算法根据初始种子生成随机数。
1. linear_congruential_engine:它是 STL 库中生成随机无符号整数的最简单引擎。它如下:
x = (a.x +c) mod m
Where x= current state value
a = multiplier parameter ; if m is not zero,
this parameter should be lower than m.
c = increment parameter ; if m is not zero,
this parameter should be lower than m.
m = modulus parameter
// C++程序,用于说明在linear_congruential_engine中使用operator()、max和min
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
// 驱动程序
int main (){
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
minstd_rand0 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: 是基于梅森费尔托斯特算法的随机数引擎。它在区间 [0, (2^w)-1] 内生成高质量的无符号整数随机数。 其中“w”是字大小:状态序列中每个字的位数。
// C++程序,用于说明在mersenne_twister_engine中使用operator()、min和max
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
// 驱动程序
int main (){
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: 是一种产生无符号整数的伪随机数生成器引擎。 使用的算法是一个滞后斐波那契生成器,具有 r 个整数元素的状态序列,加上一个进位值。
// C++程序,用于说明减法器with_carry_engine中operator()、min和max的用法
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
// 驱动程序
int main (){
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
subtract_with_carry_engine<unsigned, 24, 10, 24> generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
8606455 is a random number between 0 and 16777215
II.随机数生成器:它是一种产生非确定性随机数的随机数生成器。
// C++程序,用于说明在random_device中使用operator()、min和max
#include <iostream>
#include <random>
using namespace std;
//驱动程序
int main ()
{
random_device example;
cout << "default random_device characteristics:" << endl;
cout << "minimum: " << example.min() << endl;
cout << "maximum: " << example.max() << endl;
cout << "entropy: " << example.entropy() << endl;
cout << "a random number: " << example() << endl;
return 0;
}
输出:
default random_device characteristics:
minimum: 0
maximum: 4294967295
entropy: 0
a random number: 3705944883
III. 伪随机数引擎( 实例化):这些是生成器引擎和适配器的特定实例化:
1. default_random_engine:这是一个生成伪随机数的随机数引擎类。
x= (a.x + c)mod m
Where x= current state value
a and c = respective class template parameters
m = class template parameter
// C++程序,用于说明在default_random_engine中使用operator()、min和max
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
// 驱动程序
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
minstd_rand0 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: 生成伪随机数;它类似于线性全余生成器
x = (a.x + c) mod m
where x= current state value
a ,c and m=class template parameter
// C++程序,用于说明在minstd_land中使用operator()、max和min
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
//驱动代码
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
minstd_rand0 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
489592737 is a random number between 1 and 2147483646
3. mt19937: 它是梅森费尔托斯特19937发电机。它是一个状态大小为 19937 位的 32 位数字的伪随机生成器。
// C++程序演示mt19937中operator()、min和max的使用
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
// 驱动程序
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
1445431990 is a random number between 0 and 4294967295
4.ranlux24_base: 它是Ranlux 24基础发电机。它是一个 24 位数字的减法伪随机生成器,通常用作 ranlux24 生成器的基础引擎。
// C++程序演示了在ranlux24_base中使用operator()、min和max
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
//驱动程序
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
subtract_with_carry_engine<unsigned,24,10,24> generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
7275352 is a random number between 0 and 16777215
类似的格式适用于其他示例。
四、发动机适配器
1. discard_block_engine: 它是一个引擎适配器类模板,它通过仅使用其生成的序列中每个“p”元素块的“r”元素来适应伪随机数生成器引擎类型,丢弃其余元素。
标准发电机ranlux24和ranlux48使用此适配器适应subtract_with_carry_engine。
// C++程序演示了在discard_block_engine中使用operator()、min和max
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
//驱动程序
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
ranlux24 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
8132325 is a random number between 0 and 16777215
2. independent_bits_engine: 它是一个引擎适配器类模板,它适应伪随机数生成器引擎类型以产生具有特定位数(w)的随机数。
// C++程序,用于说明在independent_bits_engine中使用operator()、min和max
#include <iostream>
#include <chrono>
#include <cstdint>
#include <random>
using namespace std;
//驱动程序
int main ()
{
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
independent_bits_engine<mt19937,64,uint_fast64_t> generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: 它是一个引擎适配器类模板,它适应伪随机数生成器引擎类型,以便以不同的顺序传递数字。 该对象在内部保留一个由 k 个生成的数字组成的缓冲区,并在请求时返回缓冲区内随机选择的数字,并将其替换为从其基本引擎获得的值。
// C++程序演示了在shuffle_order_engine中使用 operator()、min和max
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
int main ()
{
// 查找系统时钟(当前时间)和时钟纪元之间的时间
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
ranlux24 generator (seed);
cout << generator() << " is a random number between ";
cout << generator.min() << " and " << generator.max();
return 0;
}
输出:
9213395 is a random number between 0 and 16777215
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有