【Qt之·类QRandomGenerator】

系列文章目录



前言


一、概述

1.

2.

二、实例演示

示例1:

#include <QRandomGenerator>
#include <QRandomGenerator64>
#include <QDebug>

int randomInt = QRandomGenerator::global()->bounded(100);  // 生成一个0到99之间的随机数                            
qDebug() << randomInt;                                                                                  
double randomDouble = QRandomGenerator::global()->generateDouble();  // 生成一个0.0到1.0之间的浮点数               
qDebug() << randomDouble;                                                                               
quint64 random64 = QRandomGenerator64::global()->generate();  // 生成一个64位随机数                             
qDebug() << random64;                                                                                   

运行结果:
在这里插入图片描述

示例2:

qsrand(static_cast<uint>(QTime::currentTime().msec()));  // 使用当前时间的毫秒数来设置种子                                      
int randomInt = qrand() % 100;                           // 生成一个0到99之间的随机数                                       
qDebug() << "Random Integer using qrand:" << randomInt;                                                          

运行结果:
在这里插入图片描述


总结

相关推荐

  1. Qt·QProcess】

    2024-07-19 17:46:04       38 阅读
  2. Qt字节数组QByteArray

    2024-07-19 17:46:04       61 阅读
  3. QtQChar的字符判断

    2024-07-19 17:46:04       37 阅读
  4. QtQChar的数据转换

    2024-07-19 17:46:04       48 阅读
  5. QRandomGenerator生成随机数

    2024-07-19 17:46:04       34 阅读
  6. Qt·Qt插件开发·导出插件的步骤】

    2024-07-19 17:46:04       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 17:46:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 17:46:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 17:46:04       58 阅读
  4. Python语言-面向对象

    2024-07-19 17:46:04       69 阅读

热门阅读

  1. 从零开始!Jupyter Notebook 安装教程

    2024-07-19 17:46:04       21 阅读
  2. iptables

    iptables

    2024-07-19 17:46:04      19 阅读
  3. MATLAB6:M文件和控制流

    2024-07-19 17:46:04       19 阅读
  4. early-stopping pytorch refs

    2024-07-19 17:46:04       19 阅读
  5. C++案例三:猜数字游戏

    2024-07-19 17:46:04       17 阅读
  6. 构建高可用应用的设计模式与实践

    2024-07-19 17:46:04       18 阅读
  7. MySQL简介

    2024-07-19 17:46:04       13 阅读