提交 2bb32bd4 编写于 作者: L lbernstone 提交者: Me No Dev

Unbiased random (#2468)

* An example to read high frequency analog data using i2s_adc

* Changed random() to use Lemire's method for bounding
上级 89d6b895
......@@ -37,10 +37,23 @@ void randomSeed(unsigned long seed)
long random(long howbig)
{
if(howbig == 0) {
return 0;
uint32_t x = esp_random();
uint64_t m = uint64_t(x) * uint64_t(howbig);
uint32_t l = uint32_t(m);
if (l < howbig) {
uint32_t t = -howbig;
if (t >= howbig) {
t -= howbig;
if (t >= howbig)
t %= howbig;
}
while (l < t) {
x = esp_random();
m = uint64_t(x) * uint64_t(howbig);
l = uint32_t(m);
}
}
return esp_random() % howbig;
return m >> 32;
}
long random(long howsmall, long howbig)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册