Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. std::rand() create same string
Forum Updated to NodeBB v4.3 + New Features

std::rand() create same string

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 381 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Ketan__Patel__0011
    wrote on 11 May 2021, 12:15 last edited by
    #1

    Hello Friends and Qt Experts.

    Right now i am working on Randomization
    i want to Generate unique string from my software every time.

    Here is my code.

    QString MainWindow::GenerateRandomString(int StringLength)
    {
        const QString PossibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
        QString RandomGeneratedString;
        for(int i = 0; i < StringLength; i++)
        {
            int index = std::rand() % PossibleCharacters.length();
            QChar NextCharacter = PossibleCharacters.at(index);
            RandomGeneratedString.append(NextCharacter);
        }
        return RandomGeneratedString;
    }
    

    this function working well n good

    but when i restart the my application
    i am getting same string which is previously generated.(Before restart the Application.)

    Result is ::

    /////// First Time Generated Strings ///////
    F98ER24S8U
    R3ZPZXH0DA
    1YOJE8760X
    LSLS7E9TH4
    LPCV7UVH32

    /////// Second Time Generated Strings (After Restart The Application) ///////
    F98ER24S8U
    R3ZPZXH0DA
    1YOJE8760X
    LSLS7E9TH4
    LPCV7UVH32

    Please help me to solve my problem.
    Thanks in advance.

    J 1 Reply Last reply 11 May 2021, 12:26
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 11 May 2021, 12:25 last edited by Chris Kawa 5 Nov 2021, 19:26
      #2

      With std::rand() you need to seed your generator with a different value. Traditionally a common choice is current time. A more modern one is std::random_device. See std::srand() for details.

      As a side note - rand() and % is not a very good way to generate random numbers in a range. It can result in a biased distribution. If you want a number from a given range use a proper dedicated distribution function: std::uniform_int_distribution.

      1 Reply Last reply
      9
      • K Ketan__Patel__0011
        11 May 2021, 12:15

        Hello Friends and Qt Experts.

        Right now i am working on Randomization
        i want to Generate unique string from my software every time.

        Here is my code.

        QString MainWindow::GenerateRandomString(int StringLength)
        {
            const QString PossibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            QString RandomGeneratedString;
            for(int i = 0; i < StringLength; i++)
            {
                int index = std::rand() % PossibleCharacters.length();
                QChar NextCharacter = PossibleCharacters.at(index);
                RandomGeneratedString.append(NextCharacter);
            }
            return RandomGeneratedString;
        }
        

        this function working well n good

        but when i restart the my application
        i am getting same string which is previously generated.(Before restart the Application.)

        Result is ::

        /////// First Time Generated Strings ///////
        F98ER24S8U
        R3ZPZXH0DA
        1YOJE8760X
        LSLS7E9TH4
        LPCV7UVH32

        /////// Second Time Generated Strings (After Restart The Application) ///////
        F98ER24S8U
        R3ZPZXH0DA
        1YOJE8760X
        LSLS7E9TH4
        LPCV7UVH32

        Please help me to solve my problem.
        Thanks in advance.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 11 May 2021, 12:26 last edited by
        #3

        @Ketan__Patel__0011 Sounds like you forgot to initialise the random generator

        std::srand(std::time(nullptr)); // use current time as seed for random generator
        

        See https://en.cppreference.com/w/cpp/numeric/random/rand

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5

        1/3

        11 May 2021, 12:15

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved