Commit 8b5f476f authored by Over_score's avatar Over_score

Merge branch '411-pcg32_boundedrand_r-enhancement' into 'master'

Fast random integer generation in an interval

Closes #411

See merge request !505
parents a07b23b7 4b354762
......@@ -20,17 +20,40 @@
#include "wp43s.h"
//////////////////////////////////////////////////////////
// Fast Random Integer Generation in an Interval
// DANIEL LEMIRE, Université du Québec (TELUQ), Canada
// https://arxiv.org/abs/1805.10941
uint32_t randomFrom0ToS(uint32_t s) { // random integer in [0 , s)
uint32_t x = pcg32_random() ;
uint64_t m = (uint64_t)x * (uint64_t)s;
uint32_t l = (uint32_t) m;
if(l < s) {
uint32_t t = -s % s;
while(l < t) {
x = pcg32_random();
m = (uint64_t) x *(uint64_t)s;
l = (uint32_t)m;
}
}
return m >> 32;
}
/////////////////////////////////////////////////////////////////////////////
// Method for pseudo random number generation: http://www.pcg-random.org/
void fnRandom(uint16_t unusedParamButMandatory) {
real39_t x1, x2;
saveStack();
uInt32ToReal(pcg32_boundedrand(100000000), &x1);
uInt32ToReal(pcg32_boundedrand(100000000), &x2);
//uInt32ToReal(pcg32_boundedrand(100000000), &x1);
//uInt32ToReal(pcg32_boundedrand(100000000), &x2);
uInt32ToReal(randomFrom0ToS(100000000), &x1);
uInt32ToReal(randomFrom0ToS(100000000), &x2);
realFMA(const_1e8, &x1, &x2, &x1, &ctxtReal39);
x1.exponent -= 16;
......
......@@ -20,5 +20,6 @@
#include "pcg_basic.h"
void fnRandom(uint16_t unusedParamButMandatory);
uint32_t randomFrom0ToS(uint32_t s);
void fnRandom (uint16_t unusedParamButMandatory);
void fnSeed (uint16_t unusedParamButMandatory);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment