Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Qt中重新设定qrand()的种子?_Qt_Seed - Fatal编程技术网

如何在Qt中重新设定qrand()的种子?

如何在Qt中重新设定qrand()的种子?,qt,seed,Qt,Seed,我在Qt中使用此函数生成随机字符串: GenerateRandomString() { const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); const int randomStringLength = 5; // assuming you want random strings of 5 characters QString randomStrin

我在Qt中使用此函数生成随机字符串:

GenerateRandomString()
{
const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
const int randomStringLength = 5; // assuming you want random strings of 5 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;
}
GenerateRandomString()
{
常量QString可能的字符(“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789”);
const int randomStringLength=5;//假设需要5个字符的随机字符串
QString随机字符串;

对于(int i=0;i我找到了一个解决方案…我将其添加到构造函数中,以便每次对程序进行不同的种子设定。它符合我的目的

QDateTime cd = QDateTime::currentDateTime();
qsrand(cd.toTime_t());

我找到了一个解决方案…我把它添加到构造函数中,这样每次程序的种子都会有所不同。它符合我的目的

QDateTime cd = QDateTime::currentDateTime();
qsrand(cd.toTime_t());

作为@Yep答案的替代方案,由于
QDateTime::toTime_t()
Qt5.8
以来就被弃用,以下内容同样适用:

qsrand(QDateTime::currentMSecsSinceEpoch()%UINT_MAX);

作为@Yep答案的替代方案,由于
QDateTime::toTime_t()
Qt5.8
以来就被弃用,以下内容同样适用:

qsrand(QDateTime::currentMSecsSinceEpoch()%UINT_MAX);
请注意,自Qt5.8起,toTime_t()已被弃用。有关备选方案,请参见我的答案:)请注意,自Qt5.8起,toTime_t()已被弃用。有关备选方案,请参见我的答案:)