summaryrefslogtreecommitdiff
path: root/extract_decoded_executable/rng.h
diff options
context:
space:
mode:
Diffstat (limited to 'extract_decoded_executable/rng.h')
-rw-r--r--extract_decoded_executable/rng.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/extract_decoded_executable/rng.h b/extract_decoded_executable/rng.h
new file mode 100644
index 0000000..93fd317
--- /dev/null
+++ b/extract_decoded_executable/rng.h
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+struct rngstate{
+ uint64_t state[16];
+ int idx;
+};
+
+static struct rngstate global_rng_state;
+
+void init_rng(){
+ uint64_t seed_val = (uint64_t)SEED_VAL;
+ size_t c_idx = 0;
+ for ( ; c_idx < 16; c_idx++ ){
+ seed_val = seed_val * 0x13371337;
+ seed_val = seed_val ^ (seed_val >> 16);
+ global_rng_state.state[c_idx] = seed_val;
+ }
+ return;
+}
+
+uint64_t get_rng_output(){
+ int c_idx = global_rng_state.idx;
+ const uint64_t next_state = global_rng_state.state[c_idx++];
+ uint64_t use_state = global_rng_state.state[c_idx &= 15];
+ use_state ^= use_state << 31;
+ use_state ^= use_state >> 16;
+ use_state ^= next_state ^ (next_state >> 31);
+ global_rng_state.state[c_idx] = use_state;
+ global_rng_state.idx = c_idx;
+ return use_state * 0x1337;
+}
+
+unsigned int get_random_int( unsigned int min, unsigned int max ){
+ if ( min >= max )
+ return min;
+ uint64_t nmax = 0xffffffffffffffff;
+ uint64_t ncount = (max - min) + 1;
+ uint64_t upper_limit = nmax - (nmax % ncount);
+ uint64_t rand_int = get_rng_output();
+ if ( rand_int > upper_limit ){
+ while ( rand_int > upper_limit )
+ rand_int = get_rng_output();
+ }
+ return (unsigned int)(rand_int % max) + min;
+}