Qt/c++ random string generation

18,687

You could write a function like this:

QString GetRandomString() const
{
   const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
   const int randomStringLength = 12; // assuming you want random strings of 12 characters

   QString randomString;
   for(int i=0; i<randomStringLength; ++i)
   {
       int index = qrand() % possibleCharacters.length();
       QChar nextChar = possibleCharacters.at(index);
       randomString.append(nextChar);
   }
   return randomString;
}
Share:
18,687

Related videos on Youtube

user2444217
Author by

user2444217

Updated on September 15, 2022

Comments

  • user2444217
    user2444217 over 1 year

    I am creating an application where I need to generate multiple random strings, almost like a unique ID that is made up of ASCII characters of a certain length that have a mix of uppercase/lowercase/numerical characters.

    Are there any Qt libraries for achieving this? If not, what are some of the preferred ways of generating multiple random strings in pure c++?