简体   繁体   English

std::uniform_int_distribution 不够随机

[英]std::uniform_int_distribution isn't random enough

I want to randomly select an integer among n numbers, where n is small (say 5).我想在 n 个数字中随机选择一个整数,其中 n 很小(比如 5)。 To do this, I am using std::uniform_int_distribution .为此,我使用std::uniform_int_distribution

The exact details of the distribution are not that important, but I want the results to be random and to use all the integers in the range.分布的确切细节并不那么重要,但我希望结果是随机的并使用范围内的所有整数。 The randomized selection will be done many more times that the length (size) of the range.随机选择将比范围的长度(大小)多很多倍。 I want to the choice to be done quickly and randomly .我想快速随机地完成选择。

But, no matter how many times I run it, I receive the same output.但是,无论我运行多少次,我都会收到相同的输出。 In an attempt to get different output, I let some minutes to pass and then tried again: the result was still the same (probably this code is not affected by time).为了获得不同的输出,我让几分钟过去了,然后再试一次:结果仍然相同(可能这段代码不受时间影响)。

Is it expected that each time I use uniform_int_distribution that I will get the same output?是否期望每次使用uniform_int_distribution都会得到相同的输出?

As uniform_int_distribution in particular is not what I need (I just need random numbers), I tried with RandomUniform from my pseudosite .由于特别是uniform_int_distribution不是我需要的(我只需要随机数),我尝试使用我的站点 RandomUniform 。 While I got different results than std::uniform_int_distribution produced, when I ran it again it produced the same numbers each time.虽然我得到的结果与std::uniform_int_distribution产生的结果不同,但当我再次运行它时,它每次都产生相同的数字。 Of course, I used the same main.当然,我使用了相同的主程序。

I didn't post RandomUniform, in order to not make the post any bigger.我没有发布 RandomUniform,为了不让帖子变得更大。

Try to seed the random engine.尝试播种随机引擎。 Time is a good choice.时间是一个不错的选择。 Read this to get familiar to another ways.阅读本文以熟悉其他方式。

std::default_random_engine generator( (unsigned int)time(0) );

or you can use std::random_device which tries to produce non-deterministic random numbers或者您可以使用std::random_device尝试生成非确定性随机数

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

std::random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers. std::random_device 是一个均匀分布的整数随机数生成器,它产生非确定性随机数。

Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (eg a hardware device) is not available to the implementation.请注意,如果非确定性源(例如硬件设备)对实现不可用,则 std::random_device 可以根据伪随机数引擎实现。

It uses a hardware random generator device or generates a pseudo random number.它使用硬件随机发生器设备或生成伪随机数。 Useful to seed.对种子有用。

您没有为default_random_engine种子,因此它使用的是默认的常量种子。

As others have mentioned, your main problem is that you're not seeding your engine.正如其他人所提到的,您的主要问题是您没有为引擎播种。 I thought I'd address something else since you note that speed and quality is important to you.我想我会说点别的,因为你注意到速度和质量对你很重要。

Unlike other engines, default_random_engine makes no guarantees.与其他引擎不同, default_random_engine不做任何保证。 Do not use it if you have any reasonable requirement.如果您有任何合理的要求,请不要使用它。 It's only there if you don't know what you're doing and the application of it doesn't really matter.只有当您不知道自己在做什么并且它的应用并不重要时,它才会存在。 From the standard:从标准:

Remark: The choice of engine type named by this typedef is implementation-defined.备注:由这个 typedef 命名的引擎类型的选择是实现定义的。 [ Note: The implementation may select this type on the basis of performance, size, quality, or any combination of such factors, so as to provide at least acceptable engine behavior for relatively casual, inexpert, and/or lightweight use. [注意:实现可以根据性能、大小、质量或这些因素的任意组合来选择这种类型,以便为相对随意、不熟练和/或轻量级使用提供至少可接受的引擎行为。 Because different implementations may select different underlying engine types, code that uses this typedef need not generate identical sequences across implementations.因为不同的实现可能会选择不同的底层引擎类型,所以使用这个 typedef 的代码不需要跨实现生成相同的序列。 — end note ] — 尾注 ]

A better choice might be mt19937 , which has high quality and is very fast.更好的选择可能是mt19937 ,它具有高质量且速度非常快。

As said, I should seed the generator.如前所述,我应该为生成器播种。 Notice that the reference does not provide something obvious for a seed.请注意, 引用并没有为种子提供明显的信息。

The working code is here .工作代码在这里

Notice, that as mentioned in the answers, mt19937 should be used, for better speed and quality.请注意,如答案中所述,应使用mt19937 ,以提高速度和质量。

Example in first answer here . 此处第一个答案中的示例。

Here is another example , found on the internet, that compiles.这是在互联网上找到的另一个编译示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不是`std::uniform_int_distribution<uint8_t> ` 和 `std::uniform_int_distribution<int8_t> ` 允许吗? - Why Aren't `std::uniform_int_distribution<uint8_t>` and `std::uniform_int_distribution<int8_t>` Allowed? 如何使用 std::uniform_int_distribution<t> ::参数()?</t> - How to use std::uniform_int_distribution<T>::param()? std::uniform_int_distribution 的分段错误 - Segmentation fault with std::uniform_int_distribution 为什么是 <random> 每次使用std :: uniform_int_distribution时,库都会产生相同的结果 - Why is <random> library producing the same results every time when using std::uniform_int_distribution 使用 c++11 std::uniform_int_distribution 生成随机素数 - Generate a random prime using c++11 std::uniform_int_distribution 的std :: uniform_int_distribution <int> g ++和msvc的范围 - std::uniform_int_distribution<int> range in g++ and msvc 使用std :: uniform_int_distribution并在以后定义其范围 - Use std::uniform_int_distribution and define its range later 无法转换uniform_int_distribution <int> 诠释 - Can't convert uniform_int_distribution<int> to int 来自std :: uniform_int_distribution的重复值 - Repeated values from std::uniform_int_distribution 为什么是 std::uniform_int_distribution<inttype> ::operator() 不是常量?</inttype> - Why is std::uniform_int_distribution<IntType>::operator() not const?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM