使用和实用的std :: shuffle? [英] Use and utility of std::shuffle?

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

问题描述

如果你看看随机shuffle在C ++ 11中的规格,有3个函数。我的问题是什么是典型的用途和优点:

  template<类RandomIt,类URNG> 
void shuffle(RandomIt first,RandomIt last,URNG&& g);

  template< class RandomIt> 
void random_shuffle(RandomIt first,RandomIt last);

我的意思是,无论URNG是什么(均匀分布),结果都是一样的(从统计角度看)。我看到的唯一一点是, std :: shuffle 是安全的,而这个重载 std :: random_shuffle 不是。你能确认吗?



编辑:我认为URNG应该是一个统一的分布,但似乎不编译。因此,有人可以提供一个使用 std :: shuffle

解决方案

如评论中所述, std :: shuffle 在标准中采用了随机数字生成器(或引擎说话),而不是随机数分布




  • 随机伪随机 - 真随机数生成器使用某种外部熵源。伪随机生成器(PRNG)是严格确定的。

  • 性能 - 某些发电机比其他发电机更快。

  • strong>内存使用 - 某些PRNG需要更多内存来存储其状态。

  • 期间长度 - 所有PRNG都有一个有限期它们从开始重复相同的序列。

  • 随机性质量 - 有众多测试用于测量是否有微妙的(或不那么微妙的!)模式在pseurorandom流。例如,请参阅 Diehard测试

  • 是否该流是 加密安全 。 AFAIK,标准PRNG都不是。



有关该标准提供的不同发电机的概述,请参阅 http://en.cppreference.com/w/cpp/numeric/random 。 p>

If you look to the specifications of random shuffle in C++11, there are 3 functions. My question is what is the typical use and advantage of :

template< class RandomIt, class URNG >
void shuffle( RandomIt first, RandomIt last, URNG&& g );

compared to:

template< class RandomIt >
void random_shuffle( RandomIt first, RandomIt last );

I mean, it seems that whatever URNG is (a uniform distribution), the result will be the same (from a statistical point of view). The only point I see, is that std::shuffle is thead-safe, whereas this overload ofstd::random_shuffle is not. Could you confirm that ?

EDIT: I thought that URNG should be a uniform distribution but that does not seem to compile. So can someone provide a little example of use of std::shuffle?

解决方案

As mentioned in the comments, std::shuffle takes a random number generator (or engine in standard speak), not a random number distribution. Different random number generators have different characteristics even if they have a theoretically uniform distribution.

  • Random or pseudo-random - True random number generators use some sort of an external entropy source. Pseudo-random generators (PRNGs) are strictly deterministic.
  • Performance - some generators are faster than others.
  • Memory usage - some PRNGs need more memory to store their state than others.
  • Period length - all PRNGs have a finite period after which they start repeating the same sequence from the beginning. Some have much longer periods than others.
  • Randomness quality - there are numerous tests for measuring whether there are subtle (or not-so-subtle!) patterns in a pseurorandom stream. See, for example, the Diehard tests.
  • Whether the stream is cryptographically secure or not. AFAIK, none of the standard PRNGs are.

For an overview of the different generators offered by the standard, please refer to http://en.cppreference.com/w/cpp/numeric/random.

这篇关于使用和实用的std :: shuffle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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