mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Updated SFMT RNG code, removed Qt RNG
This commit is contained in:
parent
ed5f02bf7a
commit
a15eb6f29f
11 changed files with 653 additions and 557 deletions
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue