首页 > 编程语言 >C++11生成随机数

C++11生成随机数

时间:2023-05-05 23:14:42浏览次数:36  
标签:11 const int random C++ result 随机数 param type

一、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

标签:11,const,int,random,C++,result,随机数,param,type
From: https://www.cnblogs.com/lxycoding/p/17375449.html

相关文章

  • C++中的多线程编程和同步机制
    C++中的多线程编程和同步机制使得程序员可以利用计算机的多核心来提高程序的运行效率和性能。本文将介绍多线程编程和同步机制的基本概念和使用方法。多线程编程基础在C++中,使用<thread>库来创建和管理线程。线程可以通过函数、成员函数或者Lambda表达式来实现。以下是一个使......
  • C++容器(vector、deque、list、map)
    (1)vector:将元素置于一个动态数组中,可以随机存储元素(也就是用索引直接存取)。数组尾部添加或删除元素非常迅速。但在中部或头部就比较费时。*代码演示:*取:at在下标越界时会抛出异常,我们能捕获异常进行处理;而[]下标越界会让程序直接终止;构造函数:cbegin,cend,crbegin,cren......
  • c++打卡练习(21)
    分钱:五块钱,换成一块、五毛、一毛,有几种换法?流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){ intyuan[6]={0,10,20,30,40,50}; intjiao[11]={0,5,10,15,20,25,30,35,40,45,50}; inti,j,k,m=1; for(i=0;i<=50;i++){ for(j=0;j<11;j++){ for(k=0;k......
  • [CodeForces-545A]题解(C++)
    PartIPreface原题目(Luogu)原题目(CodeForces)PartIISketch给定一个正整数\(n\),表示汽车数量。给定一个\(n\timesn\)阶矩阵\(A\),第\(i\)行\(j\)列上的数字表示\(i\)车与\(j\)车的对撞情况。\(\begin{aligned}\begin{cases}A_{i,j}=-1&i,j\text{车没......
  • C++
    动物世界1、实现Mammal类的方法2、由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型)3、Dog类中增加以下方法:constructors:Dog()、Dog(intage)、Dog(intage,intweight)、Dog(intage,COLORcolor)、Dog(intage,intweight,COLORcolor)、~Dog()accessors......
  • [CodeForces-545A]题解(C++)
    PartIPreface原题目(Luogu)原题目(CodeForces)PartIISketch给定一个正整数\(n\),表示汽车数量。给定一个\(n\timesn\)阶矩阵\(A\),第\(i\)行\(j\)列上的数字表示\(i\)车与\(j\)车的对撞情况。\(\begin{aligned}\begin{cases}A_{i,j}=-1&i,j\text{车没......
  • 信奥赛题1105:数组逆序重存放
    新奥赛一本通,题11051105:数组逆序重存放时间限制:1000ms         内存限制:65536KB提交数:70600                通过数:47540【题目描述】将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。【输入】两行:第......
  • c++-2023-05-05
    1、什么是标识符?变量、常量。为什么给标识符命名时要求开头不能使用数字?假如定义成int1=1,将造成混乱。2、为什么要有数据类型?为了方便分配内存。3、在vs的c++编译器中,如果定义单精度变量时其初始值后没有加f,系统会默认为double类型。4、c++中字符串的定义stringstr="hello......
  • C++ - VS2019配置pthread线程库
    说明在VS里用MS编译器不能直接调用pthread库,需要先自行下载该库:http://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip解压后用的到的只有Pre-built.2文件夹下的文件。 配置如下图分别配置三大项:包含目录-->...pthreads-w32-2-9-1-release\Pre-built.......
  • UVA11107 Life Forms
    怎么没有正常的后缀数组二分的题解啊给定\(n\)个字符串,求出最长的,在多于\(\left\lfloor\frac{n}{2}\right\rfloor\)个字符串中出现的子串,并按照字典序从小到大输出。\(n\leq100\),\(|S|\leq1000\),根据套路可以将所有字符串连成一个,不同的字符串用特殊符号隔开,然后建出新......