当前位置:网站首页>随机数备注
随机数备注
2022-06-24 06:44:00 【雾散睛明】
随机数备注
c标准库
stdlib.h
srand:设置随机数种子
rand:参数随机数 范围 0~32767
c++标准库
<random>
default_random_engine
实例:
void testrandom()
{
//该情况不设置随机种子,每次运行时产生的数值一样
std::default_random_engine random;
std::cout << random() << std::endl;
//设置随机种子时,每次运行不会产生相同的数值
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
random.seed(seed);
std::cout << random() << std::endl;
}
minstd_rand 是minstd_rand0的改进版 范围1~2147483646
minstd_rand0 范围1~2147483646
mt19937 范围0~4294967295 40多亿
mt19937_64 64位
…
minstd_rand0 minstd_rand default_random_engine 都是随机生成器,生成随机数的。
产生指定范围的随机数
uniform_int_distribution 只能是 int、unsigned、short、unsigned short、long、unsigned long、long long、unsigned long long 中的一种
uniform_real_distribution 可选类型为 float、double、long double
分布概率
bernoulli_distribution 是一个分布类,但它不是模板类。它的构造函数只有一个参数,
表示该类返回 true 的概率,该参数默认为 0.5 ,即返回 true 和 false 的概率相等。
以上的用法都是如下的模式
void testrandom2()
{
//返回[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;
}
更多详见 <random>
boost库:
示例:
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()
{
// 随机数种子
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed); // 大随机数
cout << rand_num() << endl;
return 0;
}
更多详见boost::random库
边栏推荐
猜你喜欢
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code

与(&&)逻辑或(||),动态绑定结合三目运算

get_started_3dsctf_2016
![[WUSTCTF2020]alison_ likes_ jojo](/img/a9/dcc6f524772cd0b8781289cbaef63f.png)
[WUSTCTF2020]alison_ likes_ jojo
![Selector (>, ~, +, [])](/img/7e/2becfcf7a7b2e743772deee5916caf.png)
Selector (>, ~, +, [])
![[GUET-CTF2019]zips](/img/79/22ff5d4a3cdc3fa9e0957ccc9bad4b.png)
[GUET-CTF2019]zips
![[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

bjdctf_ 2020_ babystack

Dichotomous special training
![[image fusion] image fusion based on NSST and PCNN with matlab code](/img/b4/61a5adde0d0bfc5a339ef8ab948d43.png)
[image fusion] image fusion based on NSST and PCNN with matlab code
随机推荐
How to open the soft keyboard in the computer, and how to open the soft keyboard in win10
Blue Bridge Cup seven segment code (dfs/ shape pressing + parallel search)
buuctf misc [UTCTF2020]docx
[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
第三方软件测试公司如何选择?2022国内软件测试机构排名
[image fusion] image fusion based on pseudo Wigner distribution (PWD) with matlab code
jarvisoj_level2
New ways to play web security [6] preventing repeated use of graphic verification codes
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
How to realize multi protocol video capture and output in video surveillance system?
A summary of the posture of bouncing and forwarding around the firewall
只显示两行,超出部分省略号显示
Super fast reading in OI
bjdctf_2020_babystack
[wustctf2020] climb
Win11 points how to divide disks? How to divide disks in win11 system?
[signal recognition] signal modulation classification based on deep learning CNN with matlab code
C code writing specification
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer
图形技术之坐标转换