2016-08-03 03:35:42 -04:00
|
|
|
|
|
|
|
// FastRandomTest.cpp
|
|
|
|
|
|
|
|
// Tests the randomness of cFastRandom
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "FastRandom.h"
|
|
|
|
|
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-03 03:35:42 -04:00
|
|
|
static void TestInts(void)
|
|
|
|
{
|
|
|
|
cFastRandom rnd;
|
|
|
|
int sum = 0;
|
|
|
|
const int BUCKETS = 8;
|
2017-06-13 15:35:30 -04:00
|
|
|
int Counts[BUCKETS] = {0};
|
2016-08-03 03:35:42 -04:00
|
|
|
const int ITER = 10000;
|
|
|
|
for (int i = 0; i < ITER; i++)
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
int v = rnd.RandInt(1000);
|
2016-08-03 03:35:42 -04:00
|
|
|
assert_test(v >= 0);
|
2017-06-13 15:35:30 -04:00
|
|
|
assert_test(v <= 1000);
|
2016-08-03 03:35:42 -04:00
|
|
|
Counts[v % BUCKETS]++;
|
|
|
|
sum += v;
|
|
|
|
}
|
|
|
|
double avg = static_cast<double>(sum) / ITER;
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("avg: %f", avg);
|
2016-08-03 03:35:42 -04:00
|
|
|
for (int i = 0; i < BUCKETS; i++)
|
|
|
|
{
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG(" bucket %d: %d", i, Counts[i]);
|
2016-08-03 03:35:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-03 03:35:42 -04:00
|
|
|
static void TestFloats(void)
|
|
|
|
{
|
|
|
|
cFastRandom rnd;
|
|
|
|
float sum = 0;
|
|
|
|
const int BUCKETS = 8;
|
2017-06-13 15:35:30 -04:00
|
|
|
int Counts[BUCKETS] = {0};
|
2016-08-03 03:35:42 -04:00
|
|
|
const int ITER = 10000;
|
|
|
|
for (int i = 0; i < ITER; i++)
|
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
float v = rnd.RandReal(1000.0f);
|
2016-08-03 03:35:42 -04:00
|
|
|
assert_test(v >= 0);
|
|
|
|
assert_test(v <= 1000);
|
|
|
|
Counts[static_cast<int>(v) % BUCKETS]++;
|
|
|
|
sum += v;
|
|
|
|
}
|
|
|
|
sum = sum / ITER;
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("avg: %f", sum);
|
2016-08-03 03:35:42 -04:00
|
|
|
for (int i = 0; i < BUCKETS; i++)
|
|
|
|
{
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG(" bucket %d: %d", i, Counts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Checks whether re-creating the cFastRandom class produces the same initial number over and over (#2935) */
|
|
|
|
static void TestReCreation(void)
|
|
|
|
{
|
|
|
|
const int ITER = 10000;
|
|
|
|
int lastVal = 0;
|
|
|
|
int numSame = 0;
|
|
|
|
int maxNumSame = 0;
|
|
|
|
for (int i = 0; i < ITER; ++i)
|
|
|
|
{
|
|
|
|
cFastRandom rnd;
|
2017-06-13 15:35:30 -04:00
|
|
|
int val = rnd.RandInt(9);
|
2017-03-28 10:37:25 -04:00
|
|
|
if (val == lastVal)
|
|
|
|
{
|
|
|
|
numSame += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (numSame > maxNumSame)
|
|
|
|
{
|
|
|
|
maxNumSame = numSame;
|
|
|
|
}
|
|
|
|
numSame = 0;
|
|
|
|
lastVal = val;
|
|
|
|
}
|
2016-08-03 03:35:42 -04:00
|
|
|
}
|
2017-03-28 10:37:25 -04:00
|
|
|
if (numSame > maxNumSame)
|
|
|
|
{
|
|
|
|
maxNumSame = numSame;
|
|
|
|
}
|
|
|
|
LOG("Out of %d creations, there was a chain of at most %d same numbers generated.", ITER, maxNumSame);
|
2016-08-03 03:35:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-03 03:35:42 -04:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("FastRandom Test started");
|
2016-08-03 03:35:42 -04:00
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("Testing ints");
|
2016-08-03 03:35:42 -04:00
|
|
|
TestInts();
|
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("Testing floats");
|
2016-08-03 03:35:42 -04:00
|
|
|
TestFloats();
|
|
|
|
|
2017-03-28 10:37:25 -04:00
|
|
|
LOG("Testing re-creation");
|
|
|
|
TestReCreation();
|
|
|
|
|
2016-08-03 03:35:42 -04:00
|
|
|
LOG("FastRandom test finished");
|
|
|
|
}
|