Create a random string or number in Qt4












21















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










share|improve this question



























    21















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










    share|improve this question

























      21












      21








      21


      3






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










      share|improve this question














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







      qt qt4 random






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 14 '10 at 9:49









      defiantdefiant

      93582347




      93582347
























          6 Answers
          6






          active

          oldest

          votes


















          18














          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.






          share|improve this answer


























          • The links in your answer are now broken!

            – zar
            Jun 3 '15 at 18:40



















          12














          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.






          share|improve this answer

































            2














            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;
            }





            share|improve this answer































              2














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






              share|improve this answer

































                0














                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"





                share|improve this answer































                  0














                  Use QUuid



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





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3244999%2fcreate-a-random-string-or-number-in-qt4%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    18














                    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.






                    share|improve this answer


























                    • The links in your answer are now broken!

                      – zar
                      Jun 3 '15 at 18:40
















                    18














                    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.






                    share|improve this answer


























                    • The links in your answer are now broken!

                      – zar
                      Jun 3 '15 at 18:40














                    18












                    18








                    18







                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 7 '15 at 7:43









                    kqt

                    6817




                    6817










                    answered Jul 14 '10 at 10:10







                    user362638




















                    • The links in your answer are now broken!

                      – zar
                      Jun 3 '15 at 18:40



















                    • The links in your answer are now broken!

                      – zar
                      Jun 3 '15 at 18:40

















                    The links in your answer are now broken!

                    – zar
                    Jun 3 '15 at 18:40





                    The links in your answer are now broken!

                    – zar
                    Jun 3 '15 at 18:40













                    12














                    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.






                    share|improve this answer






























                      12














                      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.






                      share|improve this answer




























                        12












                        12








                        12







                        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.






                        share|improve this answer















                        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.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 26 '14 at 12:46









                        ssc

                        5,67584066




                        5,67584066










                        answered Oct 24 '12 at 17:46









                        Barış AkkurtBarış Akkurt

                        1,57611633




                        1,57611633























                            2














                            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;
                            }





                            share|improve this answer




























                              2














                              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;
                              }





                              share|improve this answer


























                                2












                                2








                                2







                                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;
                                }





                                share|improve this answer













                                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;
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Sep 17 '14 at 12:05









                                Uga BugaUga Buga

                                1,14711330




                                1,14711330























                                    2














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






                                    share|improve this answer






























                                      2














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






                                      share|improve this answer




























                                        2












                                        2








                                        2







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






                                        share|improve this answer















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







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Sep 19 '14 at 11:43









                                        AbcAeffchen

                                        8,467123552




                                        8,467123552










                                        answered Sep 19 '14 at 10:35









                                        PaulPaul

                                        513




                                        513























                                            0














                                            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"





                                            share|improve this answer




























                                              0














                                              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"





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                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"





                                                share|improve this answer













                                                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"






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 14 '18 at 4:43









                                                BoburBobur

                                                325416




                                                325416























                                                    0














                                                    Use QUuid



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





                                                    share|improve this answer




























                                                      0














                                                      Use QUuid



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





                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        Use QUuid



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





                                                        share|improve this answer













                                                        Use QUuid



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






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 21 '18 at 15:42









                                                        Bibin VenugopalBibin Venugopal

                                                        90210




                                                        90210






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3244999%2fcreate-a-random-string-or-number-in-qt4%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            Bressuire

                                                            Vorschmack

                                                            Quarantine