QT 获取某一范围随机数

所谓的随机数只是一种绝对理想状态的随机数,计算机生成的随机数即是随机的又是有规律的:

    srand(1);
    for(int i =0;i<10;++i)
    {
        int randNum = rand%10;
        count<<randNum;
    }


这个生成的随机数是 [0,6)之间的一个数,每次输出都是一样的序列,那么如何让每次输出的序列不一样呢?

利用系统时间:

    int seed = QDateTime::currentDateTime().time().second();
    srand(seed);
    for(int i =0;i<10;++i)
    {
        <pre name="code" class="cpp"><span style="white-space:pre">	</span>int randNum = (rand()%10)+80;

count<<randNum; }



秒是实时变得,所以seed也会变,导致每次输出的序列不一样。

<think>我们被要求使用Qt框架在C++中生成指定范围内随机数。在Qt中,我们可以使用`QRandomGenerator`类来生成高质量的随机数。以下是具体步骤: 1. 包含必要的头文件: ```cpp #include <QRandomGenerator> ``` 2. 使用`QRandomGenerator::global()`获取全局的随机数生成器对象,然后调用其成员函数生成指定范围随机数。 例如,生成个在[low, high]范围内的整数(包括low和high): ```cpp int randomNumber = QRandomGenerator::global()->bounded(low, high+1); ``` 注意:`bounded(int low, int high)`函数生成的整数范围是[low, high),即包括low但不包括high。因此,为了得到[low, high]的闭区间,我们需要将上限设为high+1。 另种方法是先指定上限(生成[0, high-low]的随机数),然后加上low: ```cpp int randomNumber = QRandomGenerator::global()->bounded(high - low + 1) + low; ``` 这里,`bounded(int n)`生成[0, n)之间的整数,即0到n-1。所以`bounded(high-low+1)`生成0到high-low(包括)的整数,然后加上low,得到[low, high]的整数。 3. 生成浮点数: 如果要生成[low, high)范围内的浮点数(包括low,不包括high): ```cpp double randomDouble = QRandomGenerator::global()->bounded(low, high); ``` 如果需要个闭区间[low, high]的浮点数,通常我们使用[low, high+ε)来近似,但更常见的是使用半开区间。如果需要闭区间,可以考虑生成整数再映射,但浮点数通常使用半开区间。 4. 示例代码: 假设我们要生成个[1,100]之间的整数: ```cpp int num = QRandomGenerator::global()->bounded(1, 101); // 因为bounded(1,101)是[1,101)即[1,100] // 或者 int num = QRandomGenerator::global()->bounded(100) + 1; // [0,99] + 1 -> [1,100] ``` 生成[0.0, 1.0)的浮点数: ```cpp double d = QRandomGenerator::global()->generateDouble(); // 这是[0,1)的浮点数 ``` 生成[5.0, 10.0)的浮点数: ```cpp double d = QRandomGenerator::global()->bounded(5.0, 10.0); ``` 5. 注意:`QRandomGenerator`是Qt5.10引入的。如果使用较早版本的Qt,可以使用`qrand()`,但该函数生成的随机数质量较低,且需要配合`srand`初始化种子。例如: ```cpp #include <QDateTime> #include <cstdlib> // 初始化种子 qsrand(QDateTime::currentMSecsSinceEpoch() / 1000); // 生成[low, high]的整数 int randomNumber = qrand() % (high - low + 1) + low; ``` 但是,`qrand()`已经被标记为过时(obsolete),推荐使用`QRandomGenerator`。 总结:在Qt5.10及以上版本,使用`QRandomGenerator`生成随机数,它提供了多种便捷的方法来生成整数和浮点数,并且可以指定范围。 §§相关问题§§ 1. 如何在Qt中生成个随机字符串? 2. 如何确保在多个线程中使用随机数生成器是安全的? 3. 除了`QRandomGenerator`,Qt还提供了哪些与随机数相关的类?</think>在Qt C++中生成指定范围内随机数,可以使用`QRandomGenerator`类(Qt 5.10+推荐)或传统的`qrand()`函数。以下是两种方法的实现: ### 方法1:使用QRandomGenerator(推荐,线程安全) ```cpp #include <QRandomGenerator> // 生成[min, max]范围内的整数 int randomInt = QRandomGenerator::global()->bounded(min, max + 1); // 生成[min, max]范围内的浮点数 double randomDouble = QRandomGenerator::global()->bounded(min, max); ``` ### 方法2:使用qrand()(传统方法) ```cpp #include <QDateTime> #include <cstdlib> // 初始化随机种子(只需执行次) qsrand(QDateTime::currentMSecsSinceEpoch()); // 生成[min, max]范围内的整数 int randomInt = min + qrand() % ((max + 1) - min); ``` ### 完整示例代码 ```cpp #include <QDebug> #include <QRandomGenerator> #include <QDateTime> #include <cstdlib> int main() { const int min = 10; const int max = 50; // 方法1:使用QRandomGenerator int rand1 = QRandomGenerator::global()->bounded(min, max + 1); qDebug() << "QRandomGenerator 结果:" << rand1; // 方法2:使用qrand() qsrand(QDateTime::currentMSecsSinceEpoch()); // 用时间初始化种子 int rand2 = min + (qrand() % (max - min + 1)); qDebug() << "qrand() 结果:" << rand2; return 0; } ``` ### 关键说明: 1. **范围包含性**: - `bounded(min, max)` 生成 `[min, max)` 半开区间 - 整数需用 `max + 1` 包含上限值 - `qrand() % N` 生成 `[0, N-1]` 2. **种子初始化**: - `QRandomGenerator` 自动使用高质量随机源 - `qrand()` 需手动调用 `qsrand()` 初始化 - 推荐使用当前时间戳:`QDateTime::currentMSecsSinceEpoch()` 3. **线程安全**: - `QRandomGenerator::global()` 是线程安全的[^1] - `qrand()` 非线程安全,需加锁保护 > 提示:Qt 5.10+ 强烈推荐使用 `QRandomGenerator`,它提供更高质量的随机数和更好的性能[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值