当前位置:网站首页>生成随机数random学习之uniform_int_distribution,uniform_real_distribution
生成随机数random学习之uniform_int_distribution,uniform_real_distribution
2022-07-25 23:21:00 【班公湖里洗过脚】
在程序开发过程中,有时我们需要用到随机数,如果自己手写一个随机数容易引用重复,而c++11已经提供了一个生成随机数的库random,并且就可设置随机数的范围和类型,下面我们来学习使用两个最常用的随机数生成函数。
均匀分布 | |
(C++11) | 产生在一个范围上均匀分布的整数值 (类模板) |
(C++11) | 产生在一个范围上均匀分布的实数值 (类模板) |
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine int_engine; //定义随机数引擎
//定义整型随机数的取值范围
uniform_int_distribution<unsigned int> udistribution(100000, 999999);
unsigned int uRandom = udistribution(int_engine);
cout << "uRandom====================== " << uRandom << endl;
uniform_int_distribution<int> distribution(-999999, -100000);
int random = distribution(int_engine);
cout << "random======================= " << random << endl;
//定义浮点型随机数的取值范围
default_random_engine real_engine; //定义随机数引擎
uniform_real_distribution<double> doubleDistribution(10000.1, 99999.9);
double realRandom = doubleDistribution(real_engine);
cout << "realRandom=================== " << realRandom << endl;
random_device rd; // 将用于获得随机数引擎的种子
mt19937 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
uniform_real_distribution<> dis(10000, 99999);
for (int n = 0; n < 10; ++n) {
// 用 dis 变换 gen 生成的随机 unsigned int 为 [1, 2) 中的 double
std::cout << dis(gen) << ' '; // 每次调用 dis(gen) 都生成新的随机 double
}
cout << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

参考:
边栏推荐
- Idea sets get and set templates to solve the naming problem of boolean type fields
- @Autowired注解 required属性
- 762. 二进制表示中质数个计算置位
- Why are there many snapshot tables in the BI system?
- Servlet overview
- 策略模式_
- Family relationship calculator wechat applet source code
- 行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
- VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
- Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
猜你喜欢

类和对象(3)

Tencent map API request source is not authorized, this request source domain name

Drive board network cable directly connected to computer shared network configuration

Network Security Learning notes-1 file upload

MVVM model

POI特效 市场调研

The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet

自定义mvc原理

Unity 使用宏

Several commonly used traversal methods
随机推荐
TS basic data type
理解的英文(言语理解)
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
Flight control implementation of four rotor aircraft "suggestions collection"
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
多模态——Deep Multi-Modal Sets
Apple CMS V10 template /mxone Pro adaptive film and television website template
npm+模块加载机制
Strategy mode_
Take away applet with main version of traffic / repair to add main access function of traffic
Recommended system - an embedded learning framework for numerical features in CTR prediction
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
2022牛客多校第二场
Firewall command simple operation
Family relationship calculator wechat applet source code
Summary of built-in instructions and custom instructions
WordPress function encyclopedia, you can do the theme after learning it
Moment.js
PyTorch的数据输入格式要求及转换
Solution of phpstudy service environment 80 port occupied by process system under Windows