Included SFMT RNG

This commit is contained in:
Max-Wilhelm Bruker 2010-09-15 21:48:52 +02:00
parent d7b3764bba
commit 7e9879c0ea
7 changed files with 85 additions and 7 deletions

View file

@ -69,7 +69,7 @@ HEADERS += src/counter.h \
../common/protocol_items.h \
../common/protocol_datastructures.h \
../common/rng_abstract.h \
../common/rng_qt.h \
../common/rng_sfmt.h \
../common/server.h \
../common/server_arrow.h \
../common/server_card.h \
@ -142,7 +142,8 @@ SOURCES += src/counter.cpp \
../common/protocol_items.cpp \
../common/protocol_datastructures.cpp \
../common/rng_abstract.cpp \
../common/rng_qt.cpp \
../common/rng_sfmt.cpp \
../common/sfmt/SFMT.c \
../common/server.cpp \
../common/server_card.cpp \
../common/server_cardzone.cpp \

View file

@ -1,4 +1,33 @@
#include "rng_abstract.h"
#include "rng_qt.h"
#include "rng_sfmt.h"
#include <QDebug>
RNG_Abstract *rng = new RNG_Qt;
RNG_Abstract *rng = new RNG_SFMT;
QVector<int> RNG_Abstract::makeNumbersVector(int n, int min, int max)
{
const int bins = max - min + 1;
QVector<int> result(bins);
for (int i = 0; i < n; ++i) {
int number = getNumber(min, max);
if ((number < min) || (number > max))
qDebug() << "getNumber(" << min << "," << max << ") returned " << number;
else
result[number - min]++;
}
return result;
}
double RNG_Abstract::testRandom(const QVector<int> &numbers) const
{
int n = 0;
for (int i = 0; i < numbers.size(); ++i)
n += numbers[i];
double expected = (double) n / (double) numbers.size();
double chisq = 0;
for (int i = 0; i < numbers.size(); ++i)
chisq += ((double) numbers[i] - expected) * ((double) numbers[i] - expected) / expected;
return chisq;
}

View file

@ -2,12 +2,15 @@
#define RNG_ABSTRACT_H
#include <QObject>
#include <QVector>
class RNG_Abstract : public QObject {
Q_OBJECT
public:
RNG_Abstract(QObject *parent = 0) : QObject(parent) { }
virtual unsigned int getNumber(unsigned int min, unsigned int max) = 0;
QVector<int> makeNumbersVector(int n, int min, int max);
double testRandom(const QVector<int> &numbers) const;
};
extern RNG_Abstract *rng;

View file

@ -6,7 +6,6 @@ RNG_Qt::RNG_Qt(QObject *parent)
: RNG_Abstract(parent)
{
int seed = QDateTime::currentDateTime().toTime_t();
qDebug(QString("qsrand(%1)").arg(seed).toLatin1());
qsrand(seed);
}

View file

@ -20,7 +20,7 @@ HEADERS += src/servatrice.h \
../common/protocol_items.h \
../common/protocol_datastructures.h \
../common/rng_abstract.h \
../common/rng_qt.h \
../common/rng_sfmt.h \
../common/server.h \
../common/server_arrow.h \
../common/server_card.h \
@ -41,7 +41,8 @@ SOURCES += src/main.cpp \
../common/protocol_items.cpp \
../common/protocol_datastructures.cpp \
../common/rng_abstract.cpp \
../common/rng_qt.cpp \
../common/rng_sfmt.cpp \
../common/sfmt/SFMT.c \
../common/server.cpp \
../common/server_card.cpp \
../common/server_cardzone.cpp \

View file

@ -20,7 +20,9 @@
#include <QCoreApplication>
#include <QTextCodec>
#include <iostream>
#include "servatrice.h"
#include "rng_abstract.h"
void myMessageOutput(QtMsgType /*type*/, const char *msg)
{
@ -31,6 +33,41 @@ void myMessageOutput(QtMsgType /*type*/, const char *msg)
fflush(f);
}
void testRNG()
{
const int n = 500000;
std::cerr << "Testing random number generator (n = " << n << " * bins)..." << std::endl;
const int min = 1;
const int minMax = 2;
const int maxMax = 10;
QVector<QVector<int> > numbers(maxMax - minMax + 1);
QVector<double> chisq(maxMax - minMax + 1);
for (int max = minMax; max <= maxMax; ++max) {
numbers[max - minMax] = rng->makeNumbersVector(n * (max - min + 1), min, max);
chisq[max - minMax] = rng->testRandom(numbers[max - minMax]);
}
qDebug() << numbers;
for (int i = 0; i <= maxMax - min; ++i) {
std::cerr << (min + i);
for (int j = 0; j < numbers.size(); ++j) {
if (i < numbers[j].size())
std::cerr << "\t" << numbers[j][i];
else
std::cerr << "\t";
}
std::cerr << std::endl;
}
std::cerr << std::endl << "Chi^2 =";
for (int j = 0; j < chisq.size(); ++j)
std::cerr << "\t" << QString::number(chisq[j], 'f', 3).toStdString();
std::cerr << std::endl << "k =";
for (int j = 0; j < chisq.size(); ++j)
std::cerr << "\t" << (j - min + minMax);
std::cerr << std::endl << std::endl;
}
int main(int argc, char *argv[])
{
qInstallMsgHandler(myMessageOutput);
@ -40,8 +77,16 @@ int main(int argc, char *argv[])
app.setApplicationName("Servatrice");
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
std::cerr << "Servatrice " << Servatrice::versionString.toStdString() << " starting." << std::endl;
std::cerr << "-------------------------" << std::endl;
testRNG();
Servatrice server;
std::cerr << "-------------------------" << std::endl;
std::cerr << "Server initialized." << std::endl;
return app.exec();
}

View file

@ -139,4 +139,4 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS
return UnknownUser;
}
const QString Servatrice::versionString = "Servatrice 0.20100603";
const QString Servatrice::versionString = "Servatrice 0.20100915";