Fix random algo to evenly distribute.
This commit is contained in:
@@ -3,32 +3,20 @@
|
|||||||
|
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
|
|
||||||
static uint64_t sqrt64(uint64_t n) {
|
static uint64_t get_split(uint64_t total_len, uint64_t remaining_len) {
|
||||||
uint64_t g = UINT64_C(1) << 31;
|
|
||||||
|
|
||||||
for (uint64_t c = g; c; g |= c) {
|
|
||||||
if (g * g > n) {
|
|
||||||
g ^= c;
|
|
||||||
}
|
|
||||||
c >>= 1;
|
|
||||||
}
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t get_split(uint64_t len) {
|
|
||||||
uint64_t rnd;
|
uint64_t rnd;
|
||||||
rand_fill(&rnd, sizeof(rnd));
|
rand_fill(&rnd, sizeof(rnd));
|
||||||
rnd %= (len * len);
|
rnd %= total_len;
|
||||||
return sqrt64(rnd) + 1;
|
return rnd > remaining_len ? remaining_len : rnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int __attribute__ ((unused)) argc, char __attribute__ ((unused)) *argv[]) {
|
int main(int __attribute__ ((unused)) argc, char __attribute__ ((unused)) *argv[]) {
|
||||||
rand_init();
|
rand_init();
|
||||||
|
|
||||||
for (uint64_t len = 1397; len;) {
|
uint64_t total_len = 1397;
|
||||||
uint64_t consume = get_split(len);
|
for (uint64_t remaining = total_len, consume = 0; remaining; remaining -= consume) {
|
||||||
|
consume = get_split(total_len, remaining);
|
||||||
fprintf(stderr, "consume %ju bytes\n", (uintmax_t) consume);
|
fprintf(stderr, "consume %ju bytes\n", (uintmax_t) consume);
|
||||||
len -= consume;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rand_cleanup();
|
rand_cleanup();
|
||||||
|
|||||||
Reference in New Issue
Block a user