[yul-phaser] Gather functions from Random into SimulationRNG class

This commit is contained in:
Kamil Śliwak 2020-02-12 11:39:00 +01:00
parent 4ee9174454
commit b01766c4db
5 changed files with 27 additions and 15 deletions

View File

@ -29,7 +29,7 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(RandomTest)
BOOST_AUTO_TEST_CASE(uniformRandomInt_returns_different_values_when_called_multiple_times)
BOOST_AUTO_TEST_CASE(uniformInt_returns_different_values_when_called_multiple_times)
{
constexpr uint32_t numSamples = 1000;
constexpr uint32_t numOutcomes = 100;
@ -38,8 +38,8 @@ BOOST_AUTO_TEST_CASE(uniformRandomInt_returns_different_values_when_called_multi
vector<uint32_t> samples2;
for (uint32_t i = 0; i < numSamples; ++i)
{
samples1.push_back(uniformRandomInt(0, numOutcomes - 1));
samples2.push_back(uniformRandomInt(0, numOutcomes - 1));
samples1.push_back(SimulationRNG::uniformInt(0, numOutcomes - 1));
samples2.push_back(SimulationRNG::uniformInt(0, numOutcomes - 1));
}
vector<uint32_t> counts1(numOutcomes, 0);
@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(uniformRandomInt_returns_different_values_when_called_multi
BOOST_TEST(counts1 != counts2);
}
BOOST_AUTO_TEST_CASE(binomialRandomInt_returns_different_values_when_called_multiple_times)
BOOST_AUTO_TEST_CASE(binomialInt_returns_different_values_when_called_multiple_times)
{
constexpr uint32_t numSamples = 1000;
constexpr uint32_t numTrials = 100;
@ -73,8 +73,8 @@ BOOST_AUTO_TEST_CASE(binomialRandomInt_returns_different_values_when_called_mult
vector<uint32_t> samples2;
for (uint32_t i = 0; i < numSamples; ++i)
{
samples1.push_back(binomialRandomInt(numTrials, successProbability));
samples2.push_back(binomialRandomInt(numTrials, successProbability));
samples1.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
samples2.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
}
vector<uint32_t> counts1(numTrials, 0);
@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(binomialRandomInt_returns_different_values_when_called_mult
++counts2[samples2[i]];
}
// See remark for uniformRandomInt() above. Same applies here.
// See remark for uniformInt() above. Same applies here.
BOOST_TEST(counts1 != counts2);
}

View File

@ -66,5 +66,5 @@ string const& Chromosome::randomOptimisationStep()
{
static vector<string> stepNames = allStepNames();
return stepNames[uniformRandomInt(0, stepNames.size() - 1)];
return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)];
}

View File

@ -61,7 +61,7 @@ public:
std::vector<Individual> const& individuals() const { return m_individuals; }
static size_t randomChromosomeLength() { return binomialRandomInt(MaxChromosomeLength, 0.5); }
static size_t randomChromosomeLength() { return SimulationRNG::binomialInt(MaxChromosomeLength, 0.5); }
static size_t measureFitness(Chromosome const& _chromosome, Program const& _program);
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);

View File

@ -17,15 +17,15 @@
#include <tools/yulPhaser/Random.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/binomial_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <ctime>
using namespace solidity;
using namespace solidity::phaser;
uint32_t phaser::uniformRandomInt(uint32_t _min, uint32_t _max)
uint32_t SimulationRNG::uniformInt(uint32_t _min, uint32_t _max)
{
// TODO: Seed must be configurable
static boost::random::mt19937 generator(time(0));
@ -34,7 +34,7 @@ uint32_t phaser::uniformRandomInt(uint32_t _min, uint32_t _max)
return distribution(generator);
}
uint32_t phaser::binomialRandomInt(uint32_t _numTrials, double _successProbability)
uint32_t SimulationRNG::binomialInt(uint32_t _numTrials, double _successProbability)
{
// TODO: Seed must be configurable
static boost::random::mt19937 generator(time(0));

View File

@ -17,12 +17,24 @@
#pragma once
#include <boost/random/mersenne_twister.hpp>
#include <cstdint>
namespace solidity::phaser
{
uint32_t uniformRandomInt(uint32_t _min, uint32_t _max);
uint32_t binomialRandomInt(uint32_t _numTrials, double _successProbability);
/**
* A class that provides functions for generating random numbers good enough for simulation purposes.
*
* The numbers are not cryptographically secure so do not use this for anything that requires
* them to be truly unpredictable.
*/
class SimulationRNG
{
public:
static uint32_t uniformInt(uint32_t _min, uint32_t _max);
static uint32_t binomialInt(uint32_t _numTrials, double _successProbability);
};
}