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;
}
Related videos on Youtube
Author by
user2444217
Updated on September 15, 2022Comments
-
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++?
-
Shafik Yaghmour over 10 yearspossible duplicate of Create a random string or number in Qt4 also see How do I create a random alpha-numeric string in C++?
-