2017-01-26 21:21:51 +08:00
|
|
|
#include "random.h"
|
|
|
|
|
2017-01-29 17:41:48 +08:00
|
|
|
static uint32_t g_nextrandom;
|
2017-01-26 21:21:51 +08:00
|
|
|
|
|
|
|
/* Generates a (non-cryptographically secure) 32-bit random number.
|
|
|
|
*
|
|
|
|
* We don't have an implementation of the "rand" function. Instead we use a
|
|
|
|
* method of seeding with the time it took to call "autoseed" from first run.
|
2019-03-09 15:59:13 +08:00
|
|
|
*
|
2017-01-26 21:21:51 +08:00
|
|
|
* https://github.com/Proxmark/proxmark3/pull/209/commits/f9c1dcd9f6e68a8c07cffed697a9c4c8caed6015
|
2017-01-29 17:41:48 +08:00
|
|
|
*
|
|
|
|
* Iceman, rand needs to be fast.
|
|
|
|
* https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/
|
2017-01-26 21:21:51 +08:00
|
|
|
*/
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
inline void fast_prand()
|
|
|
|
{
|
2019-03-10 02:19:50 +08:00
|
|
|
fast_prandEx(GetTickCount());
|
2017-01-29 17:41:48 +08:00
|
|
|
}
|
2019-03-10 07:00:59 +08:00
|
|
|
inline void fast_prandEx(uint32_t seed)
|
|
|
|
{
|
2019-03-10 02:19:50 +08:00
|
|
|
g_nextrandom = seed;
|
2017-01-29 17:41:48 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
uint32_t prand()
|
|
|
|
{
|
2019-03-10 02:19:50 +08:00
|
|
|
// g_nextrandom *= 6364136223846793005;
|
|
|
|
// g_nextrandom += 1;
|
2017-01-29 17:41:48 +08:00
|
|
|
//return (uint32_t)(g_nextrandom >> 32) % 0xffffffff;
|
2019-03-10 02:19:50 +08:00
|
|
|
g_nextrandom = (214013 * g_nextrandom + 2531011);
|
2019-03-10 07:00:59 +08:00
|
|
|
return (g_nextrandom >> 16) & 0xFFFF;
|
2017-01-26 21:21:51 +08:00
|
|
|
}
|
|
|
|
|