Updated SFMT RNG code, removed Qt RNG

This commit is contained in:
Mark Morschhäuser 2014-03-08 17:08:40 +01:00
parent ed5f02bf7a
commit a15eb6f29f
11 changed files with 653 additions and 557 deletions

View file

@ -1,5 +1,4 @@
#include "rng_sfmt.h"
#include "sfmt/SFMT.h"
#include <QDateTime>
#include <stdlib.h>
#include <iostream>
@ -7,19 +6,17 @@
RNG_SFMT::RNG_SFMT(QObject *parent)
: RNG_Abstract(parent)
{
std::cerr << "Using SFMT random number generator." << std::endl;
int seed = QDateTime::currentDateTime().toTime_t();
init_gen_rand(seed);
for (int i = 0; i < 100000; ++i)
gen_rand64();
// initialize the random number generator with a 32bit integer seed (timestamp)
sfmt_init_gen_rand(&sfmt, QDateTime::currentDateTime().toTime_t());
}
unsigned int RNG_SFMT::getNumber(unsigned int min, unsigned int max)
{
// To make the random number generation thread safe, a mutex is created around the generation.
mutex.lock();
uint64_t r = gen_rand64();
uint64_t r = sfmt_genrand_uint64(&sfmt);
mutex.unlock();
return min + (unsigned int) (((double) (max + 1 - min)) * r / (18446744073709551616.0 + 1.0));
// return a random number from the interval [min, max]
return (unsigned int) (r % (max - min + 1));
}