mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] SimulationRNG: Add bernoulliTrial()
This commit is contained in:
parent
a8fa332a9c
commit
f29d3655f7
@ -31,6 +31,57 @@ namespace solidity::phaser::test
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(RandomTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(bernoulliTrial_should_produce_samples_with_right_expected_value_and_variance)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
constexpr size_t numSamples = 10000;
|
||||
constexpr double successProbability = 0.4;
|
||||
constexpr double relativeTolerance = 0.05;
|
||||
|
||||
// For bernoulli distribution with success probability p: EX = p, VarX = p(1 - p)
|
||||
constexpr double expectedValue = successProbability;
|
||||
constexpr double variance = successProbability * (1 - successProbability);
|
||||
|
||||
vector<uint32_t> samples;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples.push_back(static_cast<uint32_t>(SimulationRNG::bernoulliTrial(successProbability)));
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(bernoulliTrial_can_be_reset)
|
||||
{
|
||||
constexpr size_t numSamples = 10;
|
||||
constexpr double successProbability = 0.4;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples1;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples1.push_back(static_cast<uint32_t>(SimulationRNG::bernoulliTrial(successProbability)));
|
||||
|
||||
vector<uint32_t> samples2;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples2.push_back(static_cast<uint32_t>(SimulationRNG::bernoulliTrial(successProbability)));
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<uint32_t> samples3;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples3.push_back(static_cast<uint32_t>(SimulationRNG::bernoulliTrial(successProbability)));
|
||||
|
||||
SimulationRNG::reset(2);
|
||||
vector<uint32_t> samples4;
|
||||
for (uint32_t i = 0; i < numSamples; ++i)
|
||||
samples4.push_back(static_cast<uint32_t>(SimulationRNG::bernoulliTrial(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_CASE(uniformInt_returns_different_values_when_called_multiple_times)
|
||||
{
|
||||
SimulationRNG::reset(1);
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <boost/random/bernoulli_distribution.hpp>
|
||||
#include <boost/random/binomial_distribution.hpp>
|
||||
#include <boost/random/uniform_int_distribution.hpp>
|
||||
|
||||
@ -27,6 +28,13 @@ using namespace solidity::phaser;
|
||||
|
||||
thread_local boost::random::mt19937 SimulationRNG::s_generator(SimulationRNG::generateSeed());
|
||||
|
||||
bool SimulationRNG::bernoulliTrial(double _successProbability)
|
||||
{
|
||||
boost::random::bernoulli_distribution<> distribution(_successProbability);
|
||||
|
||||
return static_cast<bool>(distribution(s_generator));
|
||||
}
|
||||
|
||||
uint32_t SimulationRNG::uniformInt(uint32_t _min, uint32_t _max)
|
||||
{
|
||||
boost::random::uniform_int_distribution<> distribution(_min, _max);
|
||||
|
@ -37,6 +37,7 @@ namespace solidity::phaser
|
||||
class SimulationRNG
|
||||
{
|
||||
public:
|
||||
static bool bernoulliTrial(double _successProbability);
|
||||
static uint32_t uniformInt(uint32_t _min, uint32_t _max);
|
||||
static uint32_t binomialInt(uint32_t _numTrials, double _successProbability);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user