随机数生成:最佳做法 [英] Random number generation: best practices

查看:181
本文介绍了随机数生成:最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在 std :: mt19937 调用 std :: random_device 。如果我想生成一个数字,我不关心重复性,我应该调用 rd() random_engine()



在我的特殊情况下,我相信两者都会正常工作,因为这将在一些网络代码中调用,溢价,结果不是特别敏感。但是,我对什么时候使用硬件熵和何时使用伪随机数有一些经验法则感兴趣。



目前,我只使用 std :: random_device 以种子我的 std :: mt19937 引擎和任何随机数生成我需要我的程序, std :: mt19937 引擎。



编辑:这里有一个解释, :



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



我在这个问题中询问的特定随机数问题是随机的团队选择(其中有利的是没有对手提前知道什么团队我使用,但不是任务关键),但我真正寻找的是一个经验法则。是否可以使用 std :: random_device 如果我不需要我的数字的重复性,或有真正的风险,使用熵比它可以收集?


解决方案

据我所知,标准做法是将随机数生成器与一个不是由计算机计算(但来自一些外部,不可预测的来源)。这应该是你的rd()函数的情况。从那时起,你为每个你需要的伪随机数调用伪随机数生成器(PRNG)。



如果你担心数字不是随机的,那么你应该选择一个不同的PRNG。熵是一种稀缺和宝贵的资源,应该这样对待。虽然,你可能不需要那么多的随机数,现在,你可能在未来;或其他应用程序可能需要它们。你希望每当应用程序要求时,都可以使用熵。



对于你的应用程序来说,mersenne twister可以满足你的需求。没有人玩过你的游戏会感觉到加载的球队不是随机的。


I have a class that contains two sources of randomness.

std::random_device rd;
std::mt19937 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.

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:

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.

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?

解决方案

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.

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.

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.

这篇关于随机数生成:最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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