C++11 随机数 random
#include <iostream> #include <random> int main() { std::random_device rd; std::default_random_engine engine(rd()); std::uniform_int_distribution<> dis(0, 2); auto dice = std::bind(dis, engine); int res = dice(); /* 0、北京 1、合肥 2、成都 */ cout << res << " " << res % 3 << endl; cout << "0、北京 "<< endl; cout << "1、合肥 "<< endl; cout << "2、成都 "<< endl; return 0; }
engine
engine 是一个带状态的随机数生成器,在预定义的范围 [min, max]
以内生成随机数
engine 本身重载了 ()
运算符,使用起来类似函数
distribution
如果我们想要自定义生成随机数的范围,或者会生成的随机数分布有要求,则需要使用 distribution
资料:
https://blog.csdn.net/qq_34784753/article/details/79600809