Add Random::randomUInt() and Random::randomUIntRange().

This commit is contained in:
Felix Geyer 2011-07-08 14:40:42 +02:00
parent 4dbdc4d847
commit 8335db4db5
2 changed files with 29 additions and 1 deletions

View File

@ -21,7 +21,7 @@
void Random::randomize(QByteArray& ba)
{
gcry_randomize(ba.data(), ba.size(), GCRY_STRONG_RANDOM);
randomize(ba.data(), ba.size());
}
QByteArray Random::randomArray(int len)
@ -34,6 +34,23 @@ QByteArray Random::randomArray(int len)
return ba;
}
quint32 Random::randomUInt(quint32 limit)
{
quint32 rand;
randomize(&rand, 4);
return (rand % limit);
}
quint32 Random::randomUIntRange(quint32 min, quint32 max)
{
return min + randomUInt(max - min);
}
void Random::randomize(void* data, int len)
{
gcry_randomize(data, len, GCRY_STRONG_RANDOM);
}
Random::Random()
{
}

View File

@ -26,7 +26,18 @@ public:
static void randomize(QByteArray& ba);
static QByteArray randomArray(int len);
/**
* Generate a random quint32 in the range [0, @p limit)
*/
static quint32 randomUInt(quint32 limit);
/**
* Generate a random quint32 in the range [@p min, @p max)
*/
static quint32 randomUIntRange(quint32 min, quint32 max);
private:
static void randomize(void* data, int len);
Random();
};