C++ TR1 置随机数种子

1、

#include <stdlib.h>
#include <random>
#include <iostream>
using namespace std;
void main()
{
    std::tr1::mt19937 eng;  // a core engine class:Mersenne Twister generator

    std::cout<<"产生正态分布的10个随机数:"<<endl;
    std::tr1::normal_distribution<double> dist;
    for (int i=0; i<10; i++)
    {
        std::cout<<dist(eng)<<endl;
    }

    std::cout<<"\n产生均匀分布的在1到52之间的五个整数随机数:"<<endl;
    std::tr1::uniform_int<int> unif(1, 52); 
    for (int i=0; i<5; i++)
    {
        std::cout<<unif(eng)<<endl;
    }
    system("pause");
}

 

上述代码每次运行时生成的随机数序列固定不变:

2、

 1 #include <random>
 2 #include <iostream>
 3 #include <time.h>
 4 using namespace std;
 5 void main()
 6 {
 7     std::tr1::mt19937 eng;  // a core engine class:Mersenne Twister generator
 8     eng.seed((unsigned int)time(NULL)); // reseed base engine 设置种子用#include <time.h>, 不能用#include <time>
 9 
10     std::cout<<"产生正态分布的10个随机数:"<<endl;
11     std::tr1::normal_distribution<double> dist;
12     for (int i=0; i<10; i++)
13     {
14         std::cout<<dist(eng)<<endl;
15     }
16 
17     std::cout<<"\n产生均匀分布的在1到52之间的五个整数随机数:"<<endl;
18     std::tr1::uniform_int<int> unif(1, 52); 
19     for (int i=0; i<5; i++)
20     {
21         std::cout<<unif(eng)<<endl;
22     }
23     system("pause");
24 }

 

转载于:https://www.cnblogs.com/whl2012/p/3193222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值