From b01766c4db46a882e840f308594f83a4321cf06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 12 Feb 2020 11:39:00 +0100 Subject: [PATCH] [yul-phaser] Gather functions from Random into SimulationRNG class --- test/yulPhaser/Random.cpp | 14 +++++++------- tools/yulPhaser/Chromosome.cpp | 2 +- tools/yulPhaser/Population.h | 2 +- tools/yulPhaser/Random.cpp | 8 ++++---- tools/yulPhaser/Random.h | 16 ++++++++++++++-- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/test/yulPhaser/Random.cpp b/test/yulPhaser/Random.cpp index 9125e7e56..b9070303f 100644 --- a/test/yulPhaser/Random.cpp +++ b/test/yulPhaser/Random.cpp @@ -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 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 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 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 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); } diff --git a/tools/yulPhaser/Chromosome.cpp b/tools/yulPhaser/Chromosome.cpp index b332266ba..c75ad7612 100644 --- a/tools/yulPhaser/Chromosome.cpp +++ b/tools/yulPhaser/Chromosome.cpp @@ -66,5 +66,5 @@ string const& Chromosome::randomOptimisationStep() { static vector stepNames = allStepNames(); - return stepNames[uniformRandomInt(0, stepNames.size() - 1)]; + return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)]; } diff --git a/tools/yulPhaser/Population.h b/tools/yulPhaser/Population.h index e8a38dc14..4c8eff9fe 100644 --- a/tools/yulPhaser/Population.h +++ b/tools/yulPhaser/Population.h @@ -61,7 +61,7 @@ public: std::vector 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); diff --git a/tools/yulPhaser/Random.cpp b/tools/yulPhaser/Random.cpp index 645afb395..0e4118712 100644 --- a/tools/yulPhaser/Random.cpp +++ b/tools/yulPhaser/Random.cpp @@ -17,15 +17,15 @@ #include -#include -#include #include +#include #include 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)); diff --git a/tools/yulPhaser/Random.h b/tools/yulPhaser/Random.h index 25091ddef..850cbb01f 100644 --- a/tools/yulPhaser/Random.h +++ b/tools/yulPhaser/Random.h @@ -17,12 +17,24 @@ #pragma once +#include + #include 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); +}; }