mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] SimulationRNG: Use a single, shared and seedable generator
This commit is contained in:
parent
342a4e5dee
commit
db140a667a
@ -63,6 +63,39 @@ BOOST_AUTO_TEST_CASE(uniformInt_returns_different_values_when_called_multiple_ti
|
||||
BOOST_TEST(counts1 != counts2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(uniformInt_can_be_reset)
|
||||
{
|
||||
constexpr size_t numSamples = 10;
|
||||
constexpr uint32_t minValue = 50;
|
||||
constexpr uint32_t maxValue = 80;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples1;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples1.push_back(SimulationRNG::uniformInt(minValue, maxValue));
|
||||
|
||||
vector<uint32_t> samples2;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples2.push_back(SimulationRNG::uniformInt(minValue, maxValue));
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples3;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples3.push_back(SimulationRNG::uniformInt(minValue, maxValue));
|
||||
|
||||
SimulationRNG::reset(2);
|
||||
vector<uint32_t> samples4;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples4.push_back(SimulationRNG::uniformInt(minValue, maxValue));
|
||||
|
||||
BOOST_TEST(samples1 != samples2);
|
||||
BOOST_TEST(samples1 == samples3);
|
||||
BOOST_TEST(samples1 != samples4);
|
||||
BOOST_TEST(samples2 != samples3);
|
||||
BOOST_TEST(samples2 != samples4);
|
||||
BOOST_TEST(samples3 != samples4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(binomialInt_returns_different_values_when_called_multiple_times)
|
||||
{
|
||||
constexpr uint32_t numSamples = 1000;
|
||||
@ -89,6 +122,39 @@ BOOST_AUTO_TEST_CASE(binomialInt_returns_different_values_when_called_multiple_t
|
||||
BOOST_TEST(counts1 != counts2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(binomialInt_can_be_reset)
|
||||
{
|
||||
constexpr size_t numSamples = 10;
|
||||
constexpr uint32_t numTrials = 10;
|
||||
constexpr double successProbability = 0.6;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples1;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples1.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
|
||||
|
||||
vector<uint32_t> samples2;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples2.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples3;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples3.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
|
||||
|
||||
SimulationRNG::reset(2);
|
||||
vector<uint32_t> samples4;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples4.push_back(SimulationRNG::binomialInt(numTrials, successProbability));
|
||||
|
||||
BOOST_TEST(samples1 != samples2);
|
||||
BOOST_TEST(samples1 == samples3);
|
||||
BOOST_TEST(samples1 != samples4);
|
||||
BOOST_TEST(samples2 != samples3);
|
||||
BOOST_TEST(samples2 != samples4);
|
||||
BOOST_TEST(samples3 != samples4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
@ -25,20 +25,24 @@
|
||||
using namespace solidity;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
thread_local boost::random::mt19937 SimulationRNG::s_generator(SimulationRNG::generateSeed());
|
||||
|
||||
uint32_t SimulationRNG::uniformInt(uint32_t _min, uint32_t _max)
|
||||
{
|
||||
// TODO: Seed must be configurable
|
||||
static boost::random::mt19937 generator(time(0));
|
||||
boost::random::uniform_int_distribution<> distribution(_min, _max);
|
||||
|
||||
return distribution(generator);
|
||||
return distribution(s_generator);
|
||||
}
|
||||
|
||||
uint32_t SimulationRNG::binomialInt(uint32_t _numTrials, double _successProbability)
|
||||
{
|
||||
// TODO: Seed must be configurable
|
||||
static boost::random::mt19937 generator(time(0));
|
||||
boost::random::binomial_distribution<> distribution(_numTrials, _successProbability);
|
||||
|
||||
return distribution(generator);
|
||||
return distribution(s_generator);
|
||||
}
|
||||
|
||||
uint32_t SimulationRNG::generateSeed()
|
||||
{
|
||||
// This is not a secure way to seed the generator but it's good enough for simulation purposes.
|
||||
// The only thing that matters for us is that the sequence is different on each run and that
|
||||
// it fits the expected distribution. It does not have to be 100% unpredictable.
|
||||
return time(0);
|
||||
}
|
||||
|
@ -27,6 +27,10 @@ namespace solidity::phaser
|
||||
/**
|
||||
* A class that provides functions for generating random numbers good enough for simulation purposes.
|
||||
*
|
||||
* The functions share a common instance of the generator which can be reset with a known seed
|
||||
* to deterministically generate a given sequence of numbers. Initially the generator is seeded with
|
||||
* a value from @a generateSeed() which is different on each run.
|
||||
*
|
||||
* The numbers are not cryptographically secure so do not use this for anything that requires
|
||||
* them to be truly unpredictable.
|
||||
*/
|
||||
@ -35,6 +39,18 @@ class SimulationRNG
|
||||
public:
|
||||
static uint32_t uniformInt(uint32_t _min, uint32_t _max);
|
||||
static uint32_t binomialInt(uint32_t _numTrials, double _successProbability);
|
||||
|
||||
/// Resets generator to a known state given by the @a seed. Given the same seed, a fixed
|
||||
/// sequence of calls to the members generating random values is guaranteed to produce the
|
||||
/// same results.
|
||||
static void reset(uint32_t seed) { s_generator = boost::random::mt19937(seed); }
|
||||
|
||||
/// Generates a seed that's different on each run of the program.
|
||||
/// Does **not** use the generator and is not affected by @a reset().
|
||||
static uint32_t generateSeed();
|
||||
|
||||
private:
|
||||
thread_local static boost::random::mt19937 s_generator;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user