Qt产生随机数

目的

利用种子产生不同的随机数。如今有两种产生方式(目前个人所掌握的)

头文件

#include < QTime >
#include < QtGlobal>

qsrand(unsigned seed)
qrand();

结论:种子相同,每一个随机序列内的每一个数都是随机的,但是随机序列之间却不是随机的。是有一定规律输出的。
为了获得不同的随机序列,可以使用当前时间作为种子,来进行模拟随机数。因为时间是不断变化的,即种子的值也是不同的,所以产生的随机序列也是不同的。
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()))
int QTime::secsTo ( const QTime & t ) const 函数返回这个时间到t的秒数(如果t早于这个时间,返回的为负数)。因为QTime只能度量一天之内的时间,而且一天内只有86400秒,所以结果就应该在-86400秒和86400秒之间。实际上QTime(0,0,0).secsTo(QTime::currentTime())返回的值就是从0到QTime::currentTime()的值。

举例: 引用别人的

随机数就是就随机数种子中取出的数。
种子就是个序号,这个序号交给一个数列管理器,通过这个序号,你从管理器中取出一个数列,这个数列就是你通过那个序号得到的随机数。
但这个随技术并不真正随机。因为它是通过某个算法的得到。也就是说你给数列管理器同一个序号将得到同样一个“随机”数列。也就是说种子和随机数列是一一对应的。
{An}=f(x), x 就是种子,F()是算法,{An}是数列,这个数列看上去是随机的,这是因为An的通项很复杂。
例如:从1、2、3、4、5、6、7、8、9、0这十个数中随机取出一个数,取出的数是6的话,那么6就叫随机数。十个数字就叫随机数种子。如果是从1到50之间取数字,取出的数字叫随机数,这1到50那50个数字就叫随机数种子。

引用

注意:如果要在循环中产生不同随机数

QList < int > list;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //不能放在循环里,否则容易出现很多相同随机数
for(int var =0; var < num;++var)
{
ilist.append(qrand()%(100)); 产生[0-99];
}

引用别人的总结·:
在这里插入图片描述
了解伪随机与随机种子

QRandomGenerator

Qt 文档介绍
Since:Qt 5.10

The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator.
QRandomGenerator may be used to generate random values from a high-quality random number generator. Like the C++ random engines, QRandomGenerator can be seeded with user-provided values through the constructor. When seeded, the sequence of numbers generated by this class is deterministic. That is to say, given the same seed data, QRandomGenerator will generate the same sequence of numbers. But given different seeds, the results should be considerably different.
QRandomGenerator::securelySeeded() can be used to create a QRandomGenerator that is securely seeded with QRandomGenerator::system(), meaning that the sequence of numbers it generates cannot be easily predicted. Additionally, QRandomGenerator::global() returns a global instance of QRandomGenerator that Qt will ensure to be securely seeded. This object is thread-safe, may be shared for most uses, and is always seeded from QRandomGenerator::system()
QRandomGenerator::system() may be used to access the system's cryptographically-safe random generator. On Unix systems, it's equivalent to reading from /dev/urandom or the getrandom() or getentropy() system calls.
The class can generate 32-bit or 64-bit quantities, or fill an array of those. The most common way of generating new values is to call the generate(), generate64() or fillRange() functions. One would use it as:
Additionally, it provides a floating-point function generateDouble() that returns a number in the range [0, 1) (that is, inclusive of zero and exclusive of 1). There's also a set of convenience functions that facilitate obtaining a random number in a bounded, integral range. 

QRandomGenerator类允许从高质量的随机数生成器获取随机值。
QRandomGenerator可用于从高质量随机数生成器生成随机值。与c++随机引擎一样,QRandomGenerator可以通过构造函数使用用户提供的值进行播种。当播种时,这个类生成的数字序列是确定的。也就是说,给定相同的种子数据,QRandomGenerator将生成相同的数字序列。但是考虑到不同的种子,结果会有很大的不同。
可以使用QRandomGenerator:: securely()创建一个QRandomGenerator,该生成器安全地使用QRandomGenerator::system()进行播种,这意味着它生成的数字序列不容易预测。另外,QRandomGenerator::global()返回QRandomGenerator的全局实例,Qt将确保该实例被安全播种。这个对象是线程安全的,可以在大多数情况下共享,并且总是从QRandomGenerator::system()中获得。
system()可用于访问系统的加密安全随机生成器。在Unix系统上,它相当于从/dev/urandom或getrandom()或getentropy()系统调用中读取数据。
该类可以生成32位或64位的数量,也可以填充这些数量的数组。生成新值的最常见方法是调用generate()、generate64()或fillRange()函数。有人会把它当作:
qurandomgenerator::global()->generate();
此外,它还提供了一个浮点函数generateDouble(),该函数返回范围为0,1的数字。
QRandomGenerator是根据c++标准库中对随机数引擎的要求建模的,可以在标准库引擎可以使用的几乎所有上下文中使用。这些要求的例外情况如下:
除了std::seed_seq本身之外,QRandomGenerator不支持从其他类似于种子序列的类中进行播种;
QRandomGenerator与std::ostream或std::istream不可比较(但可复制)或可流。
QRandomGenerator还兼容统一分布类std::uniform_int_distribution和std:uniform_real_distribution,以及自由函数std::generate_canonical。例如,下面的代码可以用来生成范围为[1,2.5]的浮点数:

      std::uniform_real_distribution dist(1, 2.5);
      return dist(*QRandomGenerator::global());
  1. bound(边界范围)
double bounded(double highest)
quint32 bounded(quint32 highest)
int bounded(int highest)
quint32 bounded(quint32 lowest, quint32 highest)
int bounded(int lowest, int highest)
  1. fiilRange(填充数组)
void fillRange(UInt *buffer, qsizetype count)
void fillRange(UInt (&)[N] buffer = ...)
  1. generate(产生随机数)
quint64 generate64()
quint32 generate()
void generate(ForwardIterator begin, ForwardIterator end)
double generateDouble()
  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道阻且长,行则降至

无聊,打赏求刺激而已

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值