From ec16f5412b4fa1d0cdc76acb799d3cbf78e8cefd Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 23 Jul 2023 13:10:11 -0400 Subject: [PATCH] new randomness functions --- include/blt/std/random.h | 45 ++++++++++++++++++++++++++++++++++++---- src/tests/main.cpp | 20 ++++++++++++++++++ src/tests/queue_tests.h | 2 +- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/include/blt/std/random.h b/include/blt/std/random.h index 86ca8fd..f8bd993 100755 --- a/include/blt/std/random.h +++ b/include/blt/std/random.h @@ -9,7 +9,41 @@ #include -namespace blt { +namespace blt::random { + + static inline uint32_t PCG_Hash(uint32_t input) { + uint32_t state = input * 747796405u + 2891336453u; + uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u; + return (word >> 22u) ^ + word; + } + + static inline float randomFloat(uint32_t& seed){ + seed = PCG_Hash(seed); + return (float)seed / (float)std::numeric_limits::max(); + } + + /** + * @return random float without changing seed + */ + static inline float randomFloat_c(uint32_t seed){ + return randomFloat(seed); + } + + /** + * @param seed seed for random + * @param min inclusive min + * @param max exclusive max + * @return random int between min (inclusive) and max (exclusive) + */ + static inline int randomInt(uint32_t& seed, int min = 0, int max = 1){ + return (int)((randomFloat(seed) * (float)(max - min)) + (float)min); + } + + static inline int randomInt_c(uint32_t seed, int min = 0, int max = 1){ + return randomInt(seed, min, max); + } + /** * Creates a container class for generating random number distributions * @tparam T numeric type @@ -28,17 +62,19 @@ namespace blt { * @param max max value possible to generate. (default: 1) * @param seed seed to use in generating random values. (default: 0) */ - explicit random(T min = (T)0, T max = (T)1, long seed = 0): gen(std::mt19937(seed)){ + explicit random(T min = (T) 0, T max = (T) 1, long seed = 0): gen(std::mt19937(seed)) { distribution = new dist(min, max); } + /** * Note the min/max are inclusive and defaults to a **uniform** distribution. * @return random number between the defined min/max or the default of [0,1]. */ - T get(){ + T get() { return (*distribution)(gen); } - ~random(){ + + ~random() { delete distribution; } }; @@ -52,6 +88,7 @@ namespace blt { } }; + } #endif //BLT_RANDOM_H diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 5b45c52..bc4d95e 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -57,5 +57,25 @@ int main() { BLT_ERROR("Hello Error!"); BLT_FATAL("Hello Fatal!"); + int vals[100]; + + for (int & val : vals) + val = 0; + + uint32_t seed = 1023; + for (int i = 0; i < 1203000; i++){ + seed = seed * i; + vals[blt::random::randomInt(seed, 0, 100)]++; + } + + std::fstream csv("./randoms2.csv", std::ios::out); + + csv << "index,count\n"; + for (int i = 0; i < 100; i++){ + csv << i << "," << vals[i] << "\n"; + } + + csv.close(); + return 0; } \ No newline at end of file diff --git a/src/tests/queue_tests.h b/src/tests/queue_tests.h index 052b670..758c4e8 100755 --- a/src/tests/queue_tests.h +++ b/src/tests/queue_tests.h @@ -129,7 +129,7 @@ static inline void random_access() { } static inline void test_queues() { - blt::random rand{1, 100}; + blt::random::random rand{1, 100}; for (int& value : values){ value = rand.get();