在Qt4中创建一个随机字符串或数字

发布于 2024-09-10 10:58:57 字数 38 浏览 1 评论 0原文

是否有任何函数或类似的东西可以让我创建完全随机的字符串或数字?

Is there any function or something like that by which I can create totally random strings or numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

日暮斜阳 2024-09-17 10:58:57

您可以使用 qrand 创建随机数。如果需要字符串,可以将 int 转换为字符串。您还可以检查 QUuid 类,它生成通用唯一标识符。这些并不是“完全随机”的,但它们是独一无二的。

You can create random numbers using qrand. If you need strings, you can convert the int to string. You could also check the QUuid class, which generates Universally Unique Identifiers. Those are not 'totally random', but they are unique.

终难愈 2024-09-17 10:58:57
int number;
int randomValue = qrand() % number;

返回一个随机数 randomValue,其中 0 <= randomValue < 数字

qrand() 声明于QtGlobal 被许多其他 Qt 文件 #included。

int value;
QString aString = QString::number(value);

将整数转换为 QString。

int number;
int randomValue = qrand() % number;

returns a random number randomValue with 0 <= randomValue < number.

qrand() is declared in QtGlobal which is #included by many other Qt files.

int value;
QString aString = QString::number(value);

converts an integer to QString.

°如果伤别离去 2024-09-17 10:58:57

适用于 Qt6

double value= QRandomGenerator::global()->bounded(0, 10);

生成从 0 到 10 的双精度数

Works in Qt6

double value= QRandomGenerator::global()->bounded(0, 10);

Generate a double from 0 to 10

执手闯天涯 2024-09-17 10:58:57

以下示例生成从 A 到 Z 的大写字母且长度 = len 的字母字符串。

QString randString(int len)
{
    QString str;
    str.resize(len);
    for (int s = 0; s < len ; ++s)
        str[s] = QChar('A' + char(qrand() % ('Z' - 'A')));

    return str;
}

The following example generates alphabetic strings with capital letters from A to Z and length = len.

QString randString(int len)
{
    QString str;
    str.resize(len);
    for (int s = 0; s < len ; ++s)
        str[s] = QChar('A' + char(qrand() % ('Z' - 'A')));

    return str;
}
中二柚 2024-09-17 10:58:57

这不是生成给定范围内的随机数的很好方法。 (事实上​​,这对于大多数生成器来说非常非常糟糕)

您假设生成器的低位是均匀分布的。大多数发电机的情况并非如此。在大多数生成器中,随机性发生在高阶位中。

通过使用除法后的余数,您实际上抛弃了随机性。

您应该使用乘法和除法进行缩放。不使用模运算符。
例如,

my_numbe r= start_required + ( generator_output *  range_required)/generator_maximum;

如果generator_output位于[0,generator_maximum]中,
my_number 将位于 [start_required , start_required + range_required] 中。

This is not a very good method to generate random numbers within a given range. (In fact it's very very bad for most generators )

You are assuming that the low-order bits from the generator are uniformly distributed. This is not the case with most generators. In most generators the randomness occurs in the high order bits.

By using the remainder after divisions you are in effect throwing out the randomness.

You should scale using multiplication and division. Not using the modulo operator.
eg

my_numbe r= start_required + ( generator_output *  range_required)/generator_maximum;

If generator_output is in [0, generator_maximum],
my_number will be in [start_required , start_required + range_required].

莫相离 2024-09-17 10:58:57

使用QUuid

#include <QUuid>
QString randomStr = QUuid::createUuid();

Use QUuid

#include <QUuid>
QString randomStr = QUuid::createUuid();
爱情眠于流年 2024-09-17 10:58:57

这里是使用qrand()。下面的解决方案使用 QUuid,正如上面已经建议的那样,生成随机和唯一的 ID(它们都是十六进制数字):

#include <QApplication>
#include <QDebug>
#include <QRegularExpression>
#include <QUuid>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // random hex string generator
    for (int i = 0; i < 10; i++)
    {
        QString str = QUuid::createUuid().toString();
        str.remove(QRegularExpression("{|}|-")); // if you want only hex numbers
        qDebug() << str;
    }

    return a.exec();
}

输出

"479a494a852747fe90efe0dc0137d059"
"2cd7e3b404b54fad9154e46c527c368a"
"84e43735eacd4b8f8d733bf642476097"
"d7e824f920874f9d8b4264212f3bd385"
"40b1c6fa89254705801caefdab5edd96"
"b7067852cf9d45ca89dd7af6ffdcdd23"
"9a2e5e6b65c54bea8fb9e7e8e1676a1a"
"981fa826073947e68adc46ddf47e311c"
"129b0ec42aed47d78be4bfe279996990"
"818035b0e83f401d8a56f34122ba7990"

Here is the good answer using qrand(). The solution below uses QUuid, as already was suggested above, to generate random and unique ids (they are all hex numbers):

#include <QApplication>
#include <QDebug>
#include <QRegularExpression>
#include <QUuid>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // random hex string generator
    for (int i = 0; i < 10; i++)
    {
        QString str = QUuid::createUuid().toString();
        str.remove(QRegularExpression("{|}|-")); // if you want only hex numbers
        qDebug() << str;
    }

    return a.exec();
}

Output

"479a494a852747fe90efe0dc0137d059"
"2cd7e3b404b54fad9154e46c527c368a"
"84e43735eacd4b8f8d733bf642476097"
"d7e824f920874f9d8b4264212f3bd385"
"40b1c6fa89254705801caefdab5edd96"
"b7067852cf9d45ca89dd7af6ffdcdd23"
"9a2e5e6b65c54bea8fb9e7e8e1676a1a"
"981fa826073947e68adc46ddf47e311c"
"129b0ec42aed47d78be4bfe279996990"
"818035b0e83f401d8a56f34122ba7990"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文