Badrand pseudo random number generator, emphasis on pseudo
This commit is contained in:
parent
edc082adcb
commit
4c96aac02c
4 changed files with 95 additions and 1 deletions
35
kern/libkern/badrand.c
Normal file
35
kern/libkern/badrand.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "badrand.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define PRAND_BUILD_SEED \
|
||||
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * (uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
|
||||
(uint64_t)(__TIME__[6]) * (uint64_t)(__TIME__[7]))
|
||||
|
||||
static uint64_t seed = PRAND_BUILD_SEED * 6364136223846793005ULL + 1;
|
||||
|
||||
uint64_t badrand() {
|
||||
seed = seed * 6364136223846793005ULL + 1;
|
||||
return seed;
|
||||
}
|
||||
|
||||
uint64_t badrand_range(uint64_t min, uint64_t max) {
|
||||
uint64_t range = max - min + 1;
|
||||
uint64_t x;
|
||||
uint64_t limit = UINT64_MAX - (UINT64_MAX % range);
|
||||
|
||||
do {
|
||||
x = badrand();
|
||||
} while (x > limit);
|
||||
|
||||
return min + (x % range);
|
||||
}
|
||||
|
||||
void sbadprand(uint64_t s) {
|
||||
if (s) {
|
||||
seed ^= (s * 0x9e3779b97f4a7c15ULL) + (seed << 6) + (seed >> 2);
|
||||
} else {
|
||||
seed ^= PRAND_BUILD_SEED;
|
||||
}
|
||||
}
|
||||
|
||||
#undef PRAND_BUILD_SEED
|
||||
Loading…
Add table
Add a link
Reference in a new issue