Random随机数生成
下面代码演示使用c++11的random库进行随机数的生成
#include <iostream>
#include <random>
using namespace std;
int main()
{
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> dist6(1, 6);
std::cout << dist6(rng) << std::endl;
std::uniform_real_distribution<double> distribution(-1, 1);
std::cout << distribution(rng) << std::endl;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
初始化随机种子
std::mt19937 rng;
rng.seed(std::random_device()());
- 1
- 2
创建一个均匀分布,这个均匀分布可以等概率(随机)生成[1, 6]区间的整形数字;
std::uniform_int_distribution<std::mt19937::result_type> dist6(1, 6);
- 1
也可以直接写成
std::uniform_int_distribution<int> dist6(1, 6);
- 1
创建一个均匀分布,随机(等概率)生成在(-1, 1)之间的小数;
std::uniform_real_distribution<double> distribution(-1, 1);
- 1
更多信息看参考链接