Skip to content

Commit

Permalink
Extend rng unit test.
Browse files Browse the repository at this point in the history
I wanted to know if our rng::range(<unsigned>) implementation is ok,
especially the tail end. There has been an increase in
rng->range(sequence-size); calls to select from multiple choices, so
wanted to double check.
  • Loading branch information
scamille committed Jun 26, 2018
1 parent df36e9c commit 1309441
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ sc_http$(MODULE_EXT): interfaces$(PATHSEP)sc_http.cpp util$(PATHSEP)sc_io.cpp sc
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -std=c++0x -DUNIT_TEST $(OPTS) $(LINK_FLAGS) $^ $(LINK_LIBS) -o $@

rng$(MODULE_EXT): util$(PATHSEP)rng.cpp
rng$(MODULE_EXT): util$(PATHSEP)rng.cpp util$(PATHSEP)fmt$(PATHSEP)format.cpp
-@echo [$@] Linking
$(CXX) $(CPP_FLAGS) -DUNIT_TEST $(OPTS) $(LINK_FLAGS) $^ $(LINK_LIBS) -o $@

Expand Down
35 changes: 35 additions & 0 deletions engine/util/rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ double stdnormal_inv( double p )

#include <iostream>
#include <iomanip>
#include "util/fmt/format.h"

namespace rng {
static int64_t milliseconds()
Expand Down Expand Up @@ -1061,6 +1062,32 @@ static void monte_carlo( rng_t* rng, uint64_t n )
", numbers/sec = " << static_cast<uint64_t>( n * 1000.0 / elapsed_cpu ) << "\n\n";
}

static void test_uniform_int( rng_t* rng, uint64_t n, unsigned num_buckets )
{
int64_t start_time = milliseconds();

std::vector<unsigned> histogram(num_buckets);

for ( uint64_t i = 0; i < n; ++i )
{
auto result = rng -> range(histogram.size());
histogram[ result ] += 1;
}

int64_t elapsed_cpu = milliseconds() - start_time;

double expected_bucket_size = static_cast<double>(n) / histogram.size();

fmt::print("{} call to rng::{}::range(size_t({}u))\n", n, rng -> name(), histogram.size());
for (unsigned i = 0; i < histogram.size(); ++i)
{
double pct = static_cast<double>(histogram[ i ]) / n;
double diff = static_cast<double>(histogram[ i ]) / expected_bucket_size - 1.0;
fmt::print(" bucket {:2n}: {:5.2f}% ({}) difference to expected: {:9.6f}%\n", i, pct, histogram[ i ], diff);
}
fmt::print("time = {} ms\n\n", elapsed_cpu);
}

} // namespace rng

int main( int /*argc*/, char** /*argv*/ )
Expand Down Expand Up @@ -1111,6 +1138,14 @@ int main( int /*argc*/, char** /*argv*/ )
test_seed( rng_xs128, 100000 );
test_seed( rng_xs1024, 100000 );

unsigned num_buckets = 10;
uint64_t k = 10000;
test_uniform_int( rng_mt_cxx11, k, num_buckets );
test_uniform_int( rng_murmurhash, k, num_buckets );
test_uniform_int( rng_sfmt, k, num_buckets );
test_uniform_int( rng_tinymt, k, num_buckets );
test_uniform_int( rng_xs128, k, num_buckets );
test_uniform_int( rng_xs1024, k, num_buckets );

std::cout << "random device: min=" << rd.min() << " max=" << rd.max() << "\n\n";

Expand Down

0 comments on commit 1309441

Please sign in to comment.