播种default_random_engine? [英] seeding default_random_engine?

查看:687
本文介绍了播种default_random_engine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Visual Studio 2010不支持< chrono> ,所以我必须种子 default_random_engine 。因此,我决定以 rand 种子如下

  srand((unsigned int)time(NULL)); 
std :: default_random_engine engine(rand());
std :: normal_distribution< double> randn(0.0,0.3);

而不是以下

  unsigned seed = std :: chrono :: system_clock :: now()。time_since_epoch()。count(); 
std :: default_random_engine engine(seed);
std :: normal_distribution< double> randn(0.0,0.3);

我得到我想要获得的两种方法。我的问题是有任何注意事项,我应该注意使用rand()? (注意:我不能使用< chrono>

解决方案

我建议从 std :: random_device 获取种子,

  std :: default_random_engine engine(std :: random_device {}()); 


这将为您提供比 std :: time


I'm using visual studio 2010 which doesn't support <chrono>, so I have to seed default_random_engine. Thus, I've decided to seed it with rand as following

srand((unsigned int)time(NULL));
std::default_random_engine engine(rand());
std::normal_distribution<double> randn(0.0, 0.3);

instead of the following

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine engine(seed);
std::normal_distribution<double> randn(0.0, 0.3);

I'm getting what I'm expecting to acquire for both methods. My question is are there any considerations should I pay attention to by using rand()? (Note: I have no choice to use <chrono>

解决方案

I recommend grabbing a seed from std::random_device:

std::default_random_engine engine(std::random_device{}());

which should provide you with significantly more entropy than std::time.

这篇关于播种default_random_engine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆