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 356 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on 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.

    jsulmJ 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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
      • Ketan__Patel__0011K Ketan__Patel__0011

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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

        • Login

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