badrand: a looks_random functions for a simple sanity check
This commit is contained in:
parent
2a63fd9d1c
commit
76037c1c18
2 changed files with 29 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
#include "badrand.h"
|
#include "badrand.h"
|
||||||
|
#include "stdio.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
@ -49,4 +50,30 @@ void sbadrand(uint64_t s) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Simple but dumb sanity check for randomness */
|
||||||
|
int looks_random(char *buf, size_t len) {
|
||||||
|
int counts[256] = {0};
|
||||||
|
int run = 1, max_run = 1;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; i++) {
|
||||||
|
counts[(unsigned int)buf[i]]++;
|
||||||
|
if (i > 0 && buf[i] == buf[i - 1]) {
|
||||||
|
if (++run > max_run)
|
||||||
|
max_run = run;
|
||||||
|
} else {
|
||||||
|
run = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
if (counts[i] > (int)(len * 1 / 10))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (max_run > 16)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
#undef PRAND_BUILD_SEED
|
#undef PRAND_BUILD_SEED
|
||||||
|
|
|
||||||
|
|
@ -45,4 +45,6 @@ uint64_t badrand_range(uint64_t min, uint64_t max);
|
||||||
*/
|
*/
|
||||||
void badrand_buf(char *buf, size_t len);
|
void badrand_buf(char *buf, size_t len);
|
||||||
|
|
||||||
|
int looks_random(char *buf, size_t len);
|
||||||
|
|
||||||
#endif // BADRAND_H
|
#endif // BADRAND_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue