-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRandomPlus.cpp
27 lines (23 loc) · 921 Bytes
/
QRandomPlus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <QTime>
#include <QRandomGenerator>
#include "QRandomPlus.h"
QRandomGenerator qRandomGenerator;
QChar hexs[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
void initializeRandom()
{
// because we also share timestamp do we have same randomness on different computers even if seed with same time ?
qRandomGenerator = QRandomGenerator(QTime::currentTime().msec()); // doesn't seem necessary in Among Us decentralized for instance
}
QString randomHex(quint16 length)
{
QString res = "";
for(quint16 i = 0; i < length / 4/* / 64*/; i++)
{
//quint64 randomNb = qRandomGenerator.generate64();
quint32 randomNb = qRandomGenerator.generate() % 16;
//res += QString(QByteArray::fromHex(QString::number(randomNb).toUtf8()));
QChar hex = hexs[randomNb];
res.append(hex);
}
return res;
}