我应该使用从std :: random_device播种的随机引擎还是每次使用std :: random_device [英] Should I use a random engine seeded from std::random_device or use std::random_device every time

查看:102
本文介绍了我应该使用从std :: random_device播种的随机引擎还是每次使用std :: random_device的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个随机性源的类.

I have a class that contains two sources of randomness.

std::random_device rd;
std::mt19937 random_engine;

我通过调用std::random_devicestd::mt19937设置了种子.如果我想生成一个数字并且我不在乎重复性,我应该打电话给rd()还是random_engine()?

I seed the std::mt19937 with a call to std::random_device. If I want to generate a number and I don't care about repeatability, should I call rd() or random_engine()?

在我的特定情况下,我确定两者都可以正常工作,因为这将在某些网络代码中被调用,这些代码的性能并不高,结果也不是特别敏感.但是,我对何时使用硬件熵以及何时使用伪随机数的一些经验法则"感兴趣.

In my particular case, I'm sure both would work just fine, because this is going to be called in some networking code where performance is not at a premium, and the results are not especially sensitive. However, I am interested in some "rules of thumb" on when to use hardware entropy and when to use pseudo-random numbers.

目前,我仅使用std::random_device来生成std::mt19937引擎的种子,而对于程序需要的任何随机数生成,我都使用std::mt19937引擎.

Currently, I am only using std::random_device to seed my std::mt19937 engine, and any random number generation I need for my program, I use the std::mt19937 engine.

这是我使用此特定示例的确切解释:

edit: Here's an explanation for exactly what I am using this particular example for:

这是一个游戏程序.这个特殊的游戏允许用户在开始对对手的回合之前自定义他们的团队".进行战斗的一部分包括将团队发送到服务器.我的程序有几个团队,并使用随机数来确定要加载哪个团队.每个新的战斗都会调用std::random_device来播种伪随机数生成器.我记录了战斗的初始状态,其中包括我随机选择的团队和初始种子.

This is for a game playing program. This particular game allows the user to customize their 'team' prior to beginning a round against an opponent. Part of setting up a battle involves sending a team to the server. My program has several teams and uses the random number to determine which team to load. Each new battle makes a call to std::random_device to seed the pseudo-random number generator. I log the initial state of the battle, which includes this team that I'm randomly selecting and the initial seed.

我要在这个问题中询问的特定随机数是用于随机选择团队(在这种情况下,不让对手提前知道我正在使用的团队是有益的,但不是关键任务),但是我真正想要的是经验法则.如果我不需要数字的可重复性,总是使用std::random_device很好吗?还是存在用完比收集起来快的熵的真实风险?

The particular random number I'm asking about in this question is for the random team selection (where it is beneficial to not have the opponent know ahead of time what team I'm using, but not mission-critical), but what I'm really looking for is a rule of thumb. Is it fine to always use std::random_device if I don't need repeatability of my numbers, or is there a real risk of using up entropy faster than it can be collected?

推荐答案

据我所知,标准做法是将随机数生成器植入计算机未计算出的数字(但来自某些计算机).外部的,不可预测的来源). rd()函数应该是这种情况.从那时起,您为所需的每个伪随机数调用伪随机数生成器(PRNG).

The standard practice, as far as I am aware, is to seed the random number generator with a number that is not calculated by the computer (but comes from some external, unpredictable source). That should be the case with your rd() function. From then on, you call the pseudo-random number generator(PRNG) for each and every pseudo-random number that you need.

如果您担心数字不够随机,则应该选择其他PRNG.熵是一种稀缺和宝贵的资源,应将其视为一种资源.尽管您现在可能不需要那么多的随机数,但将来可能会需要;或其他应用程序可能需要它们.您希望该熵在应用程序要求时可用.

If you are worried about the numbers not being random enough, then you should pick a different PRNG. Entropy is a scarce and precious resource and should be treated as such. Although, you may not be needing that many random numbers right now, you may in the future; or other applications could need them. You want that entropy to be available whenever an application asks for it.

对于您的应用,听起来像是mersenne捻线机可以很好地满足您的需求.没有人玩你的游戏会觉得加载的团队不是随机的.

It sounds like, for your application, that the mersenne twister will suit your needs just fine. No one who plays your game will ever feel like the teams that are loaded aren't random.

这篇关于我应该使用从std :: random_device播种的随机引擎还是每次使用std :: random_device的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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