• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

随机数mt19937 , distribution

武飞扬头像
Auroraaaaaaaaaaaaa
帮助3

目录

随机数生成器(mt19937)

示例

随机数生成函数

uniform_int_distribution (L,R)

uniform_real_distribution (L,R)

 示例

常用的最大值最小值

例题


本来以为随机数没什么用,昨天打cf才发现,竟然可以用于构造题暴力随机??,还是学习一下

随机数生成器(mt19937)

随机数的生成有许多种,rand()函数等等,这里记录下效率最高的mt19937,它的随机数周期可以达到 2^19937-1 数量级,速度很快

mt19937 是一个随机数生成类型,定义在<random>头文件里
mt19937 的返回值是unsigned int,区间范围为[ 0 , 4*10^9 ];
mt19937_64 的返回值是unsigned long long , 范围更大
使用时需要传入random_device作为随机数的种子

示例

学新通

  1.  
    #include<bits/stdc .h>
  2.  
    using namespace std;
  3.  
    int main(){
  4.  
     
  5.  
    mt19937 rnd1(random_device{}());
  6.  
    mt19937_64 rnd2(random_device{}());
  7.  
     
  8.  
    for(int i=0;i<5;i ){
  9.  
    cout<<rnd1()<<endl;
  10.  
    cout<<rnd2()<<endl;
  11.  
    cout<<endl;
  12.  
    }
  13.  
    return 0;
  14.  
    }

随机数生成函数

一般使用的为distribution函数,在<random>头文件中,std标准库
调用时传入一个随机数生成器

uniform_int_distribution<T> (L,R)

生成一个 [L,R] 值域范围中的随机整数,保证均匀分布,返回值类型为 T,不填写则默认为 int

uniform_real_distribution<T> (L,R)

生成一个 [L,R) 值域范围中的随机实数,保证均匀分布,返回值类型为 T,不填写则默认为 double

 示例

学新通

  1.  
    #include<bits/stdc .h>
  2.  
    using namespace std;
  3.  
    int main(){
  4.  
     
  5.  
    mt19937 Rnd(random_device{}()); //随机数生成器
  6.  
     
  7.  
    uniform_int_distribution<int> dist1(0,100); //定义dist1为0——100的随机整数
  8.  
    uniform_real_distribution<double> dist2(-10,10); //定义dist2为-10——10的随机浮点数
  9.  
     
  10.  
    for(int i=0;i<5;i ){
  11.  
    cout<<dist1(Rnd)<<endl; //输出随机数
  12.  
    cout<<dist2(Rnd)<<endl;
  13.  
    cout<<endl;
  14.  
    }
  15.  
     
  16.  
    return 0;
  17.  
    }
学新通

常用的最大值最小值

int、longlong、double类型的最大值最小值如下

学新通

  1.  
    #include<bits/stdc .h>
  2.  
    using namespace std;
  3.  
    int main(){
  4.  
     
  5.  
    cout<<INT_MAX<<endl;
  6.  
    cout<<INT_MIN<<endl;
  7.  
    cout<<endl;
  8.  
     
  9.  
    cout<<LLONG_MAX<<endl;
  10.  
    cout<<LLONG_MIN<<endl;
  11.  
    cout<<endl;
  12.  
     
  13.  
    cout<<DBL_MAX<<endl;
  14.  
    cout<<DBL_MIN<<endl;
  15.  
     
  16.  
    return 0;
  17.  
    }
学新通

例题

见 codeforce round 857 div.2 C题,暴力随机化 解决构造问题
Codeforces Round 857 div.2

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhejabhi
系列文章
更多 icon
同类精品
更多 icon
继续加载