2020-01-17 06:42:43 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-01-17 06:42:43 +00:00
|
|
|
|
2020-02-15 02:03:22 +00:00
|
|
|
#include <tools/yulPhaser/SimulationRNG.h>
|
2020-01-17 06:42:43 +00:00
|
|
|
|
2020-06-03 11:52:37 +00:00
|
|
|
// NOTE: The code would work with std::random but the results for a given seed would not be reproducible
|
|
|
|
// across different STL implementations. Boost does not guarantee this either but at least it has only one
|
|
|
|
// implementation. Reproducibility is not a hard requirement for yul-phaser but it's nice to have.
|
2020-02-05 23:48:16 +00:00
|
|
|
#include <boost/random/bernoulli_distribution.hpp>
|
2020-01-17 06:42:43 +00:00
|
|
|
#include <boost/random/binomial_distribution.hpp>
|
2020-02-12 10:39:00 +00:00
|
|
|
#include <boost/random/uniform_int_distribution.hpp>
|
2020-01-17 06:42:43 +00:00
|
|
|
|
|
|
|
#include <ctime>
|
2020-06-03 11:52:37 +00:00
|
|
|
#include <limits>
|
2020-01-17 06:42:43 +00:00
|
|
|
|
2020-06-03 11:52:37 +00:00
|
|
|
using namespace std;
|
2020-01-17 06:42:43 +00:00
|
|
|
using namespace solidity;
|
2020-02-12 10:39:00 +00:00
|
|
|
using namespace solidity::phaser;
|
2020-01-17 06:42:43 +00:00
|
|
|
|
2020-02-14 23:23:32 +00:00
|
|
|
thread_local boost::random::mt19937 SimulationRNG::s_generator(SimulationRNG::generateSeed());
|
|
|
|
|
2020-02-05 23:48:16 +00:00
|
|
|
bool SimulationRNG::bernoulliTrial(double _successProbability)
|
|
|
|
{
|
2020-06-03 11:52:37 +00:00
|
|
|
boost::random::bernoulli_distribution<double> distribution(_successProbability);
|
2020-02-05 23:48:16 +00:00
|
|
|
|
2020-06-03 11:52:37 +00:00
|
|
|
return distribution(s_generator);
|
2020-02-05 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:52:37 +00:00
|
|
|
size_t SimulationRNG::uniformInt(size_t _min, size_t _max)
|
2020-01-17 06:42:43 +00:00
|
|
|
{
|
2020-06-03 11:52:37 +00:00
|
|
|
boost::random::uniform_int_distribution<size_t> distribution(_min, _max);
|
2020-02-14 23:23:32 +00:00
|
|
|
return distribution(s_generator);
|
2020-01-17 06:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:52:37 +00:00
|
|
|
size_t SimulationRNG::binomialInt(size_t _numTrials, double _successProbability)
|
2020-01-17 06:42:43 +00:00
|
|
|
{
|
2020-06-03 11:52:37 +00:00
|
|
|
// NOTE: binomial_distribution<size_t> would not work because it internally tries to use abs()
|
|
|
|
// and fails to compile due to ambiguous conversion.
|
|
|
|
assert(_numTrials <= static_cast<size_t>(numeric_limits<long>::max()));
|
|
|
|
|
|
|
|
boost::random::binomial_distribution<long> distribution(static_cast<long>(_numTrials), _successProbability);
|
|
|
|
return static_cast<size_t>(distribution(s_generator));
|
2020-02-14 23:23:32 +00:00
|
|
|
}
|
2020-01-17 06:42:43 +00:00
|
|
|
|
2020-02-14 23:23:32 +00:00
|
|
|
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.
|
2020-04-01 01:42:05 +00:00
|
|
|
return time(nullptr);
|
2020-01-17 06:42:43 +00:00
|
|
|
}
|