当前位置: 首页 > news >正文

C++ STL 随机数用法介绍

目录

一:C语言中的随机数

二:C++中的随机数

    1. 生成随机数的例子

    2. 随机数引擎

    3. 随机数引擎适配器

    4. C++中预定义的随机数引擎,引擎适配器

    5. 随机数分布


一:C语言中的随机数

<stdlib.h>//初始化随机种子
srand(static_cast<unsigned int>(time(nullptr)));//生成随机数
cout << rand() << endl;//生成一个区间的内随机数(min, max)
return static_cast<int>(rand() % (max + 1UL - min)) + min;//生成100内随机数
int num = rand() % 100;//生成1-100内随机数
int num = rand() % 100 + 1;

二:C++中的随机数

    1. 生成随机数的例子
include <random>
#include <map>
#include <iostream>int main()
{static const int NUM = 1000000;//第一步 产生随机种子std::random_device seed;//第二步:使用随机数引擎生成器std::mt19937 gen(seed());//第三步:为随机数选择一种分布,此处选择均匀分布std::uniform_int_distribution<> uniformDist(0, 20); // min= 0; max= 20for (int i = 1; i <= NUM; ++i) {std::cout << uniformDist(gen) << " ";}
}
    2. 随机数引擎
引擎说明
random_device真随机数(非基于软件生成器),通常用来产生随机种子
linear_congruential_engine线性同余法生成器
mersenne_twister_engine梅森素数法生成器
subtract_with_carry_engine带进位减法随机数生成器
    3. 随机数引擎适配器
template<class Engine, size_t p, size_t r> class discard_block_engine {...}
template<class Engine, size_t w, class UIntT> class independent_bits_engine {...}
template<class Engine, size_t k> class shuffle_order_engine {...}
    4. C++中预定义的随机数引擎,引擎适配器

  5. 随机数分布
template<class IntType = int> class uniform_int_distribution
uniform_int_distribution(IntType a = 0, IntType b = numeric_limits<IntType>::max());template<class RealType = double> class uniform_real_distribution
uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);class bernoulli_distribution
bernoulli_distribution(double p = 0.5);template<class IntType = int> class binomial_distribution
binomial_distribution(IntType t = 1, double p = 0.5);template<class IntType = int> class geometric_distribution
geometric_distribution(double p = 0.5);template<class IntType = int> class negative_binomial_distribution
negative_binomial_distribution(IntType k = 1, double p = 0.5)template<class IntType = int> class poisson_distribution
poisson_distribution(double mean = 1.0);template<class RealType = double> class exponential_distribution
exponential_distribution(RealType lambda = 1.0);template<class RealType = double> class gamma_distribution
gamma_distribution(RealType alpha = 1.0, RealType beta = 1.0);template<class RealType = double> class weibull_distribution
weibull_distribution(RealType a = 1.0, RealType b = 1.0);template<class RealType = double> class extreme_value_distribution
extreme_value_distribution(RealType a = 0.0, RealType b = 1.0);template<class RealType = double> class normal_distribution
normal_distribution(RealType mean = 0.0, RealType stddev = 1.0);template<class RealType = double> class lognormal_distribution
lognormal_distribution(RealType m = 0.0, RealType s = 1.0);template<class RealType = double> class chi_squared_distribution
chi_squared_distribution(RealType n = 1);template<class RealType = double> class cauchy_distribution
cauchy_distribution(RealType a = 0.0, RealType b = 1.0);template<class RealType = double> class fisher_f_distribution
fisher_f_distribution(RealType m = 1, RealType n = 1);template<class RealType = double> class student_t_distribution
student_t_distribution(RealType n = 1);template<class IntType = int> class discrete_distribution
discrete_distribution(initializer_list<double> wl);template<class RealType = double> class piecewise_constant_distribution
template<class UnaryOperation>piecewise_constant_distribution(initializer_list<RealType> bl,
UnaryOperation fw);template<class RealType = double> class piecewise_linear_distribution
template<class UnaryOperation>piecewise_linear_distribution(initializer_list<RealType> bl,
UnaryOperation fw);

http://www.lryc.cn/news/396680.html

相关文章:

  • 容器之docker compose
  • MIT机器人运动控制原理浅析-人形机器人
  • 开源 WAF 解析:选择最适合你的防护利器
  • AirPods Pro新功能前瞻:iOS 18的五大创新亮点
  • JavaScript中的可选链操作符
  • huggingface笔记:gpt2
  • 一次业务的批量数据任务的处理优化
  • 新能源汽车充电站远程监控系统S275钡铼技术无线RTU
  • 海外视频媒体发布/发稿:如何在国外媒体以视频的形式宣发
  • HTML 【实用教程】(2024最新版)
  • How to Describe Figures in a Research Article
  • 昇思MindSpore学习入门-CELL与参数一
  • 【k8s中安装rabbitmq】k8s中安装rabbitmq并搭建镜像集群-hostpath版
  • (5) 深入探索Python-Pandas库的核心数据结构:Series详解
  • JAVA之开发神器——IntelliJ IDEA的下载与安装
  • 通过Umijs从0到1搭建一个React项目
  • Redis 数据过期及淘汰策略
  • vue vite+three在线编辑模型导入导出
  • 去水印小程序源码修复版-前端后端内置接口+第三方接口
  • 机器学习:预测评估8类指标
  • 【深度学习基础】MAC pycharm 专业版安装与激活
  • 排序相关算法--1.插入排序+冒泡排序回顾
  • 变阻器的故障排除方法有哪些?
  • 软考《信息系统运行管理员》-3.1信息系统设施运维的管理体系
  • Nginx重定向
  • 私有化地图离线部署方案之高程检索服务
  • PostgreSQL 中如何实现数据的增量更新和全量更新的平衡?
  • 数据结构--二叉树相关习题5(判断二叉树是否是完全二叉树 )
  • Python 轻松生成多种条形码、二维码 (Code 128、EAN-13、QR code等)
  • Python: 分块读取文本文件