core_random, default_is_invalid
This commit is contained in:
@@ -43,4 +43,5 @@
|
|||||||
#include "core_string.c"
|
#include "core_string.c"
|
||||||
#include "core_log.c"
|
#include "core_log.c"
|
||||||
#include "core_lexer.c"
|
#include "core_lexer.c"
|
||||||
#include "core_type_info.c"
|
#include "core_type_info.c"
|
||||||
|
#include "core_random.c"
|
||||||
@@ -66,6 +66,7 @@ fn void default_log_proc(log_event_t ev);
|
|||||||
#endif
|
#endif
|
||||||
#define not_implemented assert(!"not implemented!")
|
#define not_implemented assert(!"not implemented!")
|
||||||
#define invalid_codepath assert(!"invalid code path!")
|
#define invalid_codepath assert(!"invalid code path!")
|
||||||
|
#define default_is_invalid default: assert(!"invalid default case")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
35
src/core/core_random.c
Normal file
35
src/core/core_random.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
typedef struct random_seed_t random_seed_t;
|
||||||
|
struct random_seed_t {
|
||||||
|
u64 a;
|
||||||
|
};
|
||||||
|
|
||||||
|
// xorshift64
|
||||||
|
u64 get_random_u64(random_seed_t *seed) {
|
||||||
|
u64 x = seed->a;
|
||||||
|
x ^= x << 13;
|
||||||
|
x ^= x >> 7;
|
||||||
|
x ^= x << 17;
|
||||||
|
return seed->a = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 get_random_in_range_u64(random_seed_t *seed, u64 start, u64 one_past_end) {
|
||||||
|
u64 r = get_random_u64(seed);
|
||||||
|
u64 diff = one_past_end - start;
|
||||||
|
u64 rr = r % diff;
|
||||||
|
u64 rrr = rr + start;
|
||||||
|
return rrr;
|
||||||
|
}
|
||||||
|
|
||||||
|
i64 get_random_in_range_i64(random_seed_t *seed, i64 start, i64 one_past_end) {
|
||||||
|
u64 r = get_random_u64(seed);
|
||||||
|
i64 diff = one_past_end - start;
|
||||||
|
u64 rr = r % diff;
|
||||||
|
i64 rrr = (i64)rr + start;
|
||||||
|
return rrr;
|
||||||
|
}
|
||||||
|
|
||||||
|
f64 get_random_f64(random_seed_t *seed) {
|
||||||
|
f64 random = (f64)get_random_in_range_u64(seed, 0, UINT32_MAX);
|
||||||
|
f64 result = random / (f64)(UINT32_MAX - 1u);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user