MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / getRandomBits

Function getRandomBits

libraries/randomHelpers/randomHelpers.cpp:199–224  ·  view source on GitHub ↗

n = 1..31 TODO: performance gain too low for n > 16

Source from the content-addressed store, hash-verified

197// n = 1..31
198// TODO: performance gain too low for n > 16
199uint32_t getRandomBits(uint8_t n)
200{
201 uint32_t rv = 0;
202
203 // for large values of n the more straightforward approach is faster (UNO).
204 if (n > 32) n = 32;
205 if (n >= 20) return getRandom32() >> (32 - n);
206
207 if (n >= __randomIdx)
208 {
209 if (__randomIdx > 0)
210 {
211 n -= __randomIdx;
212 rv = __randomBuffer << n;
213 }
214 __randomBuffer = getRandom32();
215 __randomIdx = 32;
216 }
217 if (n > 0) // more bits needed?
218 {
219 rv |= __randomBuffer & ((1UL << n) - 1);
220 __randomBuffer >>= n;
221 __randomIdx -= n;
222 }
223 return rv;
224}
225
226
227// -- END OF FILE --

Callers

nothing calls this directly

Calls 1

getRandom32Function · 0.85

Tested by

no test coverage detected