std :: uniform_int_distribution不够随机 [英] std::uniform_int_distribution isn't random enough

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

问题描述

我想随机选择n个数字之间的整数,其中n很小(比如5)。为此,我使用 std :: uniform_int_distribution

I want to randomly select an integer among n numbers, where n is small (say 5). To do this, I am using 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).

每次我使用 uniform_int_distribution ,我会得到相同的输出吗?

Is it expected that each time I use uniform_int_distribution that I will get the same output?

$ c> uniform_int_distribution 尤其不是我需要的(我只需要随机数),我尝试了RandomUniform从我的假复合物。虽然我得到不同的结果比 std :: 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. While I got different results than std::uniform_int_distribution produced, when I ran it again it produced the same numbers each time. Of course, I used the same main.

我没有发布RandomUniform,为了不使帖子任何更大。 p>

I didn't post RandomUniform, in order to not make the post any bigger.

推荐答案

尝试种子随机引擎。时间是一个不错的选择。您可以阅读以阅读其他种子申请方式。

Try to seed the random engine. Time is a good choice. You can read this to read another ways to seed.

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

 

std :: random_device 尝试生成非确定性随机数

or you can use std::random_device which tries to produce non-deterministic random numbers

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




std :: random_device是均匀分布的整数随机数$ b生成非确定性随机数。

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 (e.g. a hardware device) is not available to the implementation.

它绑定到一个硬件随机数(例如
硬件设备)发生器装置或产生伪随机数。对种子有用。

It bind to a hardware random generator device or generate a pseudo random numbers. Useful to seed.

这篇关于std :: uniform_int_distribution不够随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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