C++ 中的随机标头系列1

简介: 此标头引入了随机数生成功能。该库允许使用生成器和分布的组合生成随机数。

开启掘金成长之旅!这是我参与「掘金日新计划 · 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 
复制代码

  • operator(): 它生成随机数。

  • min: 它给出成员 operator() 返回的最小值。

  • max: 它给出成员 operator() 返回的最大值。
// 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”是字大小:状态序列中每个字的位数。


  • operator(): 它生成随机数。

  • min: 它返回成员operator()返回的最小值,对于mersenne_twister_engine,该值始终为零。

  • max: 它返回成员operator()返回的最大值,对于mersenne_twister_engine为 2w-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 个整数元素的状态序列,加上一个进位值。


  • operator() :它生成随机数。

  • max:它返回成员operator()返回的最大值,对于 subtract_with_carry_engine 为 (2^w)-1,其中 'w' 是单词大小。

  • min:它返回成员operator()返回的最小值,对于 subtract_with_carry_engine,该值始终为零。
// 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.随机数生成器:它是一种产生非确定性随机数的随机数生成器。


  • random_device:它是真正的随机数生成器。

  • operator() :它返回一个新的随机数。

  • min:它返回成员operator()返回的最小值,对于random_device,该值始终为零。

  • max:它返回成员operator() 返回的最大值。
// 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. 伪随机数引擎( 实例化):这些是生成器引擎和适配器的特定实例化:

38.png


1. default_random_engine:这是一个生成伪随机数的随机数引擎类。


  • min:它返回 operator() 给出的最小值。

  • max:它返回operator() 给出的最大值。

  • operator() :它返回一个新的随机数。

  • 该函数将内部状态更改 1,根据给定的算法修改状态值:
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: 生成伪随机数;它类似于线性全余生成器


  • operator(): 它返回一个新的随机数。该函数将内部状态更改 1,根据以下算法修改状态值:
x = (a.x + c) mod m
where x= current state value
a ,c and m=class template parameter
复制代码
  • min: 它返回成员operator()给出的最小值。
  • max: 它返回成员operator()给出的最大值,对于linear_congruential_engine为 (modulus-1)。
// 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 位数字的伪随机生成器。


  • operator(): 它生成一个随机数。该函数使用转换算法将内部状态更改一个,该算法会在所选元素上产生扭曲。

  • max: 它返回operator()给出的最大值。

  • min: 它返回 operator() 给出的最小值。
// 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 生成器的基础引擎。


  • operator(): 它返回一个新的随机数。
    该函数通过调用其转换算法来更改内部状态,该算法对元素应用减法随进位操作。

  • max: 它返回operator()给出的最大值。

  • min: 它返回 operator() 给出的最小值。
// 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。


  • operator(): 它返回一个新的随机数。

  • max: 它返回operator()给出的最大值。

  • min: 它返回 operator() 给出的最小值。
// 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)的随机数。


  • operator(): 它返回一个新的随机数。
  • 引擎的转换算法根据需要多次调用基础引擎的 operator() 成员,以获得足够的有效位来构造随机值。

  • max: 它返回operator()给出的最大值。

  • min: 它返回 operator() 给出的最小值。


// 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 个生成的数字组成的缓冲区,并在请求时返回缓冲区内随机选择的数字,并将其替换为从其基本引擎获得的值。


  • operator(): 它返回一个新的随机数。
  • 引擎的转换算法在内部表中选取一个值(由函数返回),并将其替换为从其基础引擎获得的新值。

  • max: 它返回operator()给出的最大值。

  • 最小值: 它返回 operator() 给出的最小值。


// 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



目录
相关文章
|
定位技术 网络安全 数据安全/隐私保护
部署Halo开源建站工具
Halo是一款强大易用的开源建站工具,拥有丰富的主题模板与插件,可以快速帮助您构建理想的网站。本文主要介绍使用Halo应用镜像快速部署Halo开源建站工具。
|
关系型数据库 MySQL 分布式数据库
云原生数据库PolarDB MySQL版评测报告
云原生数据库PolarDB MySQL版评测报告
383 4
|
网络安全 数据安全/隐私保护 Android开发
想知道Android手机怎么远程登录到系统后台吗?看这里
想知道Android手机怎么远程登录到系统后台吗?看这里
想知道Android手机怎么远程登录到系统后台吗?看这里
|
程序员
程序员请放下浮躁的心
以前觉得程序员是一个非常有竞争力的职业,程序员是一群不断学习,不断跟着技术的潮流。有人说21世纪是终身学习的世纪,没错我们处在知识大爆炸的时代,是目前人类最辉煌的时刻。
1219 0
|
MySQL 关系型数据库 API
|
iOS开发 前端开发
纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果
原文:纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果 大家好,我是小强老师。 今天我们看下CSS3最为简单的两个属性。      css3给我们带来了很多视觉的感受和变化,以前的图片做的事情,很多代码都能实现。
1665 0
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16727 61
|
7天前
|
域名解析 人工智能 Linux
|
1天前
|
人工智能 安全 网络安全