C++11生成随机数

发布时间 2023-05-05 22:58:33作者: lixycc

一、random_device 类

class random_device {
public:
   typedef unsigned int result_type;

   // constructor 构造函数
   explicit random_device(const std::string& token = "");

   // properties
   static result_type min();
   static result_type max();
   double entropy() const;

   // generate 返回一个随机数
   result_type operator()();

   // no-copy functions
   random_device(const random_device&) = delete;
   void operator=(const random_device&) = delete;
};

 通常,random_device 用于设定使用引擎或引擎适配器创建的其他生成器的种子,即用来实例化一个随机数种子对象。
(1)头文件:<random>
(2)示例

#include <random>
#include <iostream>
using namespace std;

int main()
{
    random_device rd; // 构造一个随机数种子

    cout << "entropy == " << rd.entropy() << endl;
    cout << "min == " << rd.min() << endl;
    cout << "max == " << rd.max() << endl;

    cout << "a random value == " << rd() << endl;
    cout << "a random value == " << rd() << endl;
    cout << "a random value == " << rd() << endl;
}
// output
entropy == 32
min == 0
max == 4294967295
a random value == 2378414971
a random value == 3633694716
a random value == 213725214

二、std::mt19937 类

 std::mt19937(since C++ 11) 类是一个非常高效的伪随机数生成器,并在随机头文件中定义。它使用著名的流行算法 Mersenne twister 算法生成 32 位伪随机数。
(1)头文件:<random>
(2)示例

// 构造一个随机数生成器gen
// 方式1
random_device rd;
mt19937 gen(rd());
// 方式2
mt19937 gen(random_device{}());

三、std::uniform_int_distribution 类

template<class IntType = int>
   class uniform_int_distribution {
public:
   // types
   typedef IntType result_type;
   struct param_type;

   // constructors and reset functions
   explicit uniform_int_distribution(
      result_type a = 0, result_type b = numeric_limits<result_type>::max());
   explicit uniform_int_distribution(const param_type& parm);
   void reset();

   // generating functions
   template <class URNG>
      result_type operator()(URNG& gen);
   template <class URNG>
      result_type operator()(URNG& gen, const param_type& parm);

   // property functions
   result_type a() const;
   result_type b() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
};

 在包含起始值和结束值的输出范围中生成均匀的(每个值的概率都均等)整数分布。
(1)头文件:<random>
(2)示例

#include <iostream>
#include <random>
using namespace std;

int main(int argc, char const* argv[])
{
    random_device rd; // 使用随机数引擎获得随机种子
    std::mt19937 gen(rd()); // 以 rd() 为种子的标准 mersenne_twister_engine
    uniform_int_distribution<int> distribute(1, 6);
    for (int i = 0; i < 10; ++i) {
        cout << distribute(gen) << " ";
    }
    cout << endl;
}
// output
4 5 3 6 1 4 6 4 6 6

四、std::uniform_real_distribution 类

template<class RealType = double>
   class uniform_real_distribution {
public:
   // types
   typedef RealType result_type;
   struct param_type;

   // constructors and reset functions
   explicit uniform_real_distribution(
      result_type a = 0.0, result_type b = 1.0);
   explicit uniform_real_distribution(const param_type& parm);
   void reset();

   // generating functions
   template <class URNG>
      result_type operator()(URNG& gen);
   template <class URNG>
      result_type operator()(URNG& gen, const param_type& parm);

   // property functions
   result_type a() const;
   result_type b() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
};

 在包含起始值不包含结束值的输出范围中生成均匀的(每个值的概率都均等)浮点分布。
(1)头文件:<random>
(2)示例

#include <random>
#include <iostream>
 
int main()
{
    std::random_device rd;  // 将用于获得随机数引擎的种子
    std::mt19937 gen(rd()); // 以 rd() 为种子的标准 mersenne_twister_engine
    std::uniform_real_distribution<> dis(1, 2);
    for (int n = 0; n < 10; ++n) {
        // 用 dis 变换 gen 生成的随机 unsigned int 为 [1, 2) 中的 double
        std::cout << dis(gen) << ' '; // 每次调用 dis(gen) 都生成新的随机 double
    }
    std::cout << '\n';
}
// output
1.80829 1.15391 1.18483 1.38969 1.36094 1.0648 1.97798 1.27984 1.68261 1.57326