Create a random string or number in Qt4

前端 未结 6 561
独厮守ぢ
独厮守ぢ 2020-12-16 10:49

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

相关标签:
6条回答
  • 2020-12-16 11:01

    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;
    }
    
    0 讨论(0)
  • 2020-12-16 11:11

    Use QUuid

    #include <QUuid>
    QString randomStr = QUuid::createUuid();
    
    0 讨论(0)
  • 2020-12-16 11:20
    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.

    0 讨论(0)
  • 2020-12-16 11:21

    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].

    0 讨论(0)
  • 2020-12-16 11:23

    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.

    0 讨论(0)
  • 2020-12-16 11:26

    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"
    
    0 讨论(0)
提交回复
热议问题