20 #include "../base/assert.h" 52 for (
int i = 0; i < 56; ++i) ma[i] = 0;
54 int32_t mj = (_RAND_MSEED -
seed) % _RAND_MBIG;
58 for (int32_t i = 1; i < 55; ++i) {
59 int32_t ii = (21 * i) % 55;
66 for (int32_t k = 0; k < 4; ++k) {
67 for (int32_t j = 1; j < 55; ++j) {
68 ma[j] -= ma[1 + (j + 30) % 55];
83 if (++inext == 56) inext = 0;
84 if (++inextp == 56) inextp = 0;
99 Random(
const int _seed = -1) : seed(0), original_seed(0), inext(0), inextp(0), expRV(0) {
100 for (
int i = 0; i < 56; ++i) ma[i] = 0;
125 original_seed = _seed;
128 int seed_time = (int) time(NULL);
129 int seed_mem = (int) ((uint64_t)
this);
130 seed = seed_time ^ seed_mem;
135 if (seed < 0) seed *= -1;
169 inline double GetDouble(
const double min,
const double max) {
170 emp_assert((max-min) <= (
double) _RAND_MBIG, min, max);
190 template <
typename T>
193 return static_cast<uint32_t
>(
GetDouble() *
static_cast<double>(max));
203 template <
typename T>
205 if (max <= (T) _RAND_MBIG)
return (uint64_t)
GetUInt(max);
206 const double max2 = ((double) max) / (double) _RAND_MBIG;
209 return static_cast<uint64_t
>(
GetDouble() *
static_cast<double>(max))
210 +
static_cast<uint64_t
>(
GetDouble() *
static_cast<double>(max2) * _RAND_MBIG);
221 template <
typename T1,
typename T2>
222 inline uint32_t
GetUInt(
const T1 min,
const T2 max) {
223 return GetUInt<uint32_t>((uint32_t) max - (uint32_t) min) + (uint32_t) min;
232 template <
typename T>
244 inline int GetInt(
const int max) {
return static_cast<int>(
GetUInt((uint32_t) max)); }
245 inline int GetInt(
const int min,
const int max) {
return GetInt(max - min) + min; }
253 inline bool P(
const double p) {
255 return (
Get() < (p * _RAND_MBIG));
272 expRV -= (expRV2-1)*(expRV2-1)/2;
273 if (expRV > 0)
break;
276 if (
P(.5))
return expRV2;
304 const double a = exp(-mean);
305 if (a <= 0)
return UINT_MAX;
325 for (uint32_t i = 0; i < n; ++i)
if (
P(p)) k++;
342 if (n * p * (1 - p) >= _BINOMIAL_TO_NORMAL) {
343 return static_cast<uint32_t
>(
GetRandNormal(n * p, n * p * (1 - p)) + 0.5);
346 if (n >= _BINOMIAL_TO_POISSON) {
348 if (k < UINT_MAX)
return k;
369 template <
typename ForwardIterator,
typename OutputIterator,
typename RNG>
370 void sample_with_replacement(ForwardIterator first, ForwardIterator last, OutputIterator ofirst, OutputIterator olast, RNG rng) {
371 std::size_t range = std::distance(first, last);
372 while(ofirst != olast) {
373 *ofirst = *(first+rng(range));
int GetOriginalSeed() const
Definition: Random.h:115
int argument_type
Definition: Random.h:358
int GetInt(const Range< int > range)
Definition: Random.h:246
double GetRandNormal(const double mean, const double std)
Definition: Random.h:284
static const uint32_t _RAND_MSEED
Definition: ce_random.h:71
int GetInt(const int max)
Definition: Random.h:244
uint32_t GetUInt(const T max)
Definition: Random.h:191
Random(const int _seed=-1)
Definition: Random.h:99
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
Definition: BitVector.h:785
void init()
Definition: Random.h:49
constexpr uint32_t Get()
Definition: ce_random.h:109
double GetDouble()
Definition: Random.h:149
A range of values from a lower limit to and upper limit, of any provided type.
Definition: Range.h:23
int result_type
Definition: Random.h:359
T GetLower() const
Definition: Range.h:32
This is an adaptor to make Random behave like a proper STL random number generator.
Definition: ce_random.h:315
~Random()
Definition: Random.h:104
uint32_t GetFullRandBinomial(const double n, const double p)
Definition: Random.h:321
int operator()(int n)
Definition: Random.h:362
constexpr double GetRandNormal()
Definition: ce_random.h:226
constexpr uint32_t GetUInt(const uint32_t max)
Definition: ce_random.h:191
A simple way to track value ranges.
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: Random.h:361
constexpr double GetDouble()
Definition: ce_random.h:166
uint32_t GetRandPoisson(const double mean)
Definition: Random.h:301
int GetInt(const int min, const int max)
Definition: Random.h:245
double GetDouble(const double max)
Definition: Random.h:157
int ma[56]
Internal state of RNG.
Definition: ce_random.h:59
double GetRandNormal()
Definition: Random.h:266
static const uint32_t _RAND_MBIG
Definition: ce_random.h:70
constexpr void ResetSeed(const int _seed)
Definition: ce_random.h:150
int GetSeed() const
Definition: Random.h:110
void ResetSeed(const int _seed)
Definition: Random.h:124
uint32_t GetRandBinomial(const double n, const double p)
Definition: Random.h:337
constexpr uint32_t GetRandPoisson(const double mean)
Definition: ce_random.h:251
uint32_t GetRandPoisson(const double n, double p)
Definition: Random.h:289
int32_t Get()
Definition: Random.h:82
uint32_t GetUInt(const Range< T > range)
Definition: Random.h:233
If we are in emscripten, make sure to include the header.
Definition: array.h:37
double GetDouble(const Range< double > range)
Definition: Random.h:180
int inext
First position in use in internal state.
Definition: ce_random.h:57
#define emp_assert(...)
Definition: assert.h:199
double expRV
Definition: ce_random.h:62
double GetDouble(const double min, const double max)
Definition: Random.h:169
uint64_t GetUInt64(const T max)
Definition: Random.h:204
T GetUpper() const
Definition: Range.h:33
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
bool P(const double p)
Definition: Random.h:253
uint32_t GetUInt(const T1 min, const T2 max)
Definition: Random.h:222
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
int inextp
Second position in use in internal state.
Definition: ce_random.h:58