当前位置:网站首页>Random number remarks
Random number remarks
2022-06-24 07:47:00 【Fog dispels eyesight】
Random number remarks
c Standard library
stdlib.h
srand: Set random number seed
rand: Parameter random number Range 0~32767
c++ Standard library
<random>
default_random_engine
example :
void testrandom()
{
// In this case, no random seed is set , The values generated during each run are the same
std::default_random_engine random;
std::cout << random() << std::endl;
// When setting random seeds , Each run does not produce the same value
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
random.seed(seed);
std::cout << random() << std::endl;
}
minstd_rand yes minstd_rand0 Improved version Range 1~2147483646
minstd_rand0 Range 1~2147483646
mt19937 Range 0~4294967295 40 More than
mt19937_64 64 position
…
minstd_rand0 minstd_rand default_random_engine All random generators , Generating random numbers .
Generates a random number in a specified range
uniform_int_distribution Can only be int、unsigned、short、unsigned short、long、unsigned long、long long、unsigned long long One of the
uniform_real_distribution The optional type is float、double、long double
Distribution probability
bernoulli_distribution Is a distribution class , But it's not a template class . Its constructor has only one parameter ,
Indicates that this class returns true Probability , This parameter defaults to 0.5 , Return true and false The probability is equal .
The above usage is the following pattern
void testrandom2()
{
// return [0,9]
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine random(seed);
std::uniform_int_distribution<int> distribution(0,9);
std::cout << distribution(random) << std::endl;
}
More on <random>
boost library :
Example :
boost::random::mt19937 rng; // produces randomness out of thin air
// see pseudo-random number generators
boost::random::uniform_int_distribution<> six(1,6);
// distribution that maps to 1..6
// see random number distributions
int x = six(rng); // simulate rolling a die
#include <random>
using namespace std;
int main()
{
// Random number seed
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed); // Large random number
cout << rand_num() << endl;
return 0;
}
More on boost::random library
边栏推荐
- Hubei College Upgraded to undergraduate - Hushi family planning department
- Win10 build webservice
- Unity 的序列化
- Climbing 10000 NASA pictures about Mars exploration, I found a secret
- Jenkins 太老了 试试它?云原生 CI/CD Tekton
- Combine with (& &) logic or (||), dynamic binding and ternary operation
- PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
- 运行npm run eject报错解决方法
- BOM notes
- 《canvas》之第2章 直线图形
猜你喜欢

The two most frequently asked locks in the interview

保留一位小数和保留两位小数

Combine with (& &) logic or (||), dynamic binding and ternary operation

Free ICP domain name filing interface

RDD的执行原理
![[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence](/img/c5/f548223666d7379a7d4aaed2953587.png)
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence

OpenGauss数据库在 CentOS 上的实践,配置篇

Baidu map, coordinate inversion, picking coordinate position

Maxcompute remote connection, uploading and downloading data files

面试中的最常被问到的两种锁
随机推荐
『C语言』系统日期&时间
Black box and white box models for interpretable AI
What industries and scenarios can the easynvr/easygbs/easycvr platform developed by tsingsee green rhino video be used in?
《canvas》之第2章 直线图形
面试中的最常被问到的两种锁
How to realize multi protocol video capture and output in video surveillance system?
本地备份和还原 SQL Server 数据库
图形技术之管线概念
UE常用控制臺命令
【Django中运行scrapy框架,并将数据存入数据库】
C code writing specification
Tidb operator source code reading (IV) control cycle of components
Domain environment importing Tencent cloud considerations
免费ICP域名备案查接口
Anaconda 中使用 You Get
自动化测试的生命周期是什么?
Phonics
Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
BOM笔记
What should I pay attention to after the live broadcast system source code is set up?