1 /* 2 This file is part of solidity. 3 4 solidity is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 solidity is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with solidity. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 // SPDX-License-Identifier: GPL-3.0 18 19 #pragma once 20 21 #include <boost/random/mersenne_twister.hpp> 22 23 #include <cstddef> 24 #include <cstdint> 25 26 namespace solidity::phaser 27 { 28 29 /** 30 * A class that provides functions for generating random numbers good enough for simulation purposes. 31 * 32 * The functions share a common instance of the generator which can be reset with a known seed 33 * to deterministically generate a given sequence of numbers. Initially the generator is seeded with 34 * a value from @a generateSeed() which is different on each run. 35 * 36 * The numbers are not cryptographically secure so do not use this for anything that requires 37 * them to be truly unpredictable. 38 */ 39 class SimulationRNG 40 { 41 public: 42 static bool bernoulliTrial(double _successProbability); 43 static size_t uniformInt(size_t _min, size_t _max); 44 static size_t binomialInt(size_t _numTrials, double _successProbability); 45 46 /// Resets generator to a known state given by the @a seed. Given the same seed, a fixed 47 /// sequence of calls to the members generating random values is guaranteed to produce the 48 /// same results. reset(uint32_t seed)49 static void reset(uint32_t seed) { s_generator = boost::random::mt19937(seed); } 50 51 /// Generates a seed that's different on each run of the program. 52 /// Does **not** use the generator and is not affected by @a reset(). 53 static uint32_t generateSeed(); 54 55 private: 56 thread_local static boost::random::mt19937 s_generator; 57 }; 58 59 } 60