39 #ifndef EMP_CE_RANDOM_H 40 #define EMP_CE_RANDOM_H 49 #include "../tools/math.h" 59 int ma[56] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
79 for (
int i = 0; i < 56; ++i) ma[i] = 0;
81 int mj = (_RAND_MSEED -
seed) % _RAND_MBIG;
85 for (
int i = 1; i < 55; ++i) {
86 int ii = (21 * i) % 55;
93 for (
int k = 0; k < 4; ++k) {
94 for (
int j = 1; j < 55; ++j) {
95 ma[j] -= ma[1 + (j + 30) % 55];
109 constexpr uint32_t
Get() {
110 if (++inext == 56) inext = 0;
111 if (++inextp == 56) inextp = 0;
126 constexpr
Random(
const int _seed=-1) : seed(0), original_seed(0), inext(0), inextp(0), expRV(0) {
127 for (
int i = 0; i < 56; ++i) ma[i] = 0;
151 original_seed = _seed;
153 if (seed < 0) seed *= -1;
183 constexpr
double GetDouble(
const double min,
const double max) {
return GetDouble() * (max - min) + min; }
191 constexpr uint32_t
GetUInt(
const uint32_t max) {
return static_cast<int>(
GetDouble() *
static_cast<double>(max)); }
200 constexpr uint32_t
GetUInt(
const uint32_t min,
const uint32_t max) {
return GetUInt(max - min) + min; }
209 constexpr int32_t
GetInt(
const int max) {
return static_cast<int>(
GetUInt(max)); }
210 constexpr int32_t
GetInt(
const int min,
const int max) {
return static_cast<int>(
GetUInt(max - min)) + min; }
216 constexpr
bool P(
const double _p) {
return (
Get() < (_p * _RAND_MBIG));}
231 expRV -= (expRV2-1)*(expRV2-1)/2;
232 if (expRV > 0)
break;
236 if (
P(.5))
return expRV2;
255 if (a <= 0)
return UINT_MAX;
283 for (uint32_t i = 0; i < n; ++i)
if (
P(p)) k++;
298 if (n * p * (1 - p) >= _BINOMIAL_TO_NORMAL) {
299 return static_cast<uint32_t
>(
GetRandNormal(n * p, n * p * (1 - p)) + 0.5);
302 if (n >= _BINOMIAL_TO_POISSON) {
304 if (k < UINT_MAX)
return k;
328 template <
typename ForwardIterator,
typename OutputIterator,
typename RNG>
329 void sample_with_replacement(ForwardIterator first, ForwardIterator last, OutputIterator ofirst, OutputIterator olast, RNG rng) {
330 std::size_t range = std::distance(first, last);
331 while(ofirst != olast) {
332 *ofirst = *(first+rng(range));
constexpr int GetSeed() const
Definition: ce_random.h:136
int argument_type
Definition: ce_random.h:316
static const uint32_t _RAND_MSEED
Definition: ce_random.h:71
Random & _rng
Definition: ce_random.h:322
constexpr uint32_t GetUInt(const uint32_t min, const uint32_t max)
Definition: ce_random.h:200
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
Definition: BitVector.h:785
constexpr uint32_t Get()
Definition: ce_random.h:109
int result_type
Definition: ce_random.h:317
static constexpr double Ln(double x)
Compile-time natural log calculator.
Definition: math.h:128
This is an adaptor to make Random behave like a proper STL random number generator.
Definition: ce_random.h:315
int operator()(int n)
Definition: ce_random.h:320
constexpr double GetRandNormal()
Definition: ce_random.h:226
constexpr uint32_t GetUInt(const uint32_t max)
Definition: ce_random.h:191
int seed
Current random number seed.
Definition: ce_random.h:55
constexpr void init()
Definition: ce_random.h:76
static const uint32_t _BINOMIAL_TO_POISSON
Definition: ce_random.h:67
static const uint32_t _BINOMIAL_TO_NORMAL
Definition: ce_random.h:66
RandomStdAdaptor(Random &rng)
Definition: ce_random.h:319
constexpr double GetDouble()
Definition: ce_random.h:166
constexpr int32_t GetInt(const int max)
Definition: ce_random.h:209
int ma[56]
Internal state of RNG.
Definition: ce_random.h:59
constexpr Random(const int _seed=-1)
Definition: ce_random.h:126
static const uint32_t _RAND_MBIG
Definition: ce_random.h:70
constexpr void ResetSeed(const int _seed)
Definition: ce_random.h:150
constexpr uint32_t GetRandPoisson(const double n, double p)
Definition: ce_random.h:268
constexpr uint32_t GetRandPoisson(const double mean)
Definition: ce_random.h:251
constexpr double GetDouble(const double min, const double max)
Definition: ce_random.h:183
If we are in emscripten, make sure to include the header.
Definition: array.h:37
constexpr uint32_t GetRandBinomial(const double n, const double p)
Definition: ce_random.h:295
int inext
First position in use in internal state.
Definition: ce_random.h:57
static constexpr double Exp(double exp)
A fast method of calculating e^x.
Definition: math.h:181
double expRV
Definition: ce_random.h:62
constexpr double GetDouble(const double max)
Definition: ce_random.h:174
constexpr bool P(const double _p)
Definition: ce_random.h:216
int original_seed
Orignal random number seed when object was first created.
Definition: ce_random.h:56
constexpr uint32_t GetFullRandBinomial(const double n, const double p)
Definition: ce_random.h:280
void sample_with_replacement(ForwardIterator first, ForwardIterator last, OutputIterator ofirst, OutputIterator olast, RNG rng)
Draw a sample (with replacement) from an input range, copying to the output range.
Definition: ce_random.h:329
constexpr int32_t GetInt(const int min, const int max)
Definition: ce_random.h:210
constexpr int GetOriginalSeed() const
Definition: ce_random.h:141
constexpr double GetRandNormal(const double mean, const double std)
Definition: ce_random.h:244
int inextp
Second position in use in internal state.
Definition: ce_random.h:58