diff --git a/README.md b/README.md index cd18db6..b9a1959 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,17 @@ FastRandom is a small library made to generate random enough numbers and UUIDs. The randomity is not that guarranted, but at least, the output have really 100% of chance of being trash. ```c++ -#include +#include -archivist::UUID weak{fast_uuid()}; // generate a hasty UUID -archivist::UUID medium{balanced_uuid()}; // generate a probably strong UUID -archivist::UUID good{strong_uuid()}; // generate a really random UUID +archivist::uuid_fast weak; // generate a hasty UUID +archivist::uuid_balanced medium; // generate a probably strong UUID +archivist::uuid_strong good; // generate a really random UUID ``` ```c++ -#include +#include -uint64_t number1 = archivist::PRNG(); // Generate a random number -uint64_t number2 = archivist::PRNG(4682); // Generate a random number and mixes some entropy (thread wide) -archivist::PRNG_feed(some_entropy); // Give some entropy system wide +uint64_t number1 = archivist::prng(); // Generate a random number +uint64_t number2 = archivist::prng(4682); // Generate a random number and mixes some entropy (thread wide) +archivist::prng_feed(some_entropy); // Give some entropy system wide ``` diff --git a/include/FastRandom/Base_PRNG.h b/include/FastRandom/Base_PRNG.h deleted file mode 100644 index 536c986..0000000 --- a/include/FastRandom/Base_PRNG.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Licenced under MIT Licence - * - * (c) Ludovic 'Archivist' Lagouardette 2018 - * - */ - - #pragma once -#include -#include -#include - -namespace archivist{ - -namespace internal{ - extern thread_local uint64_t prngState; - extern thread_local uint64_t prngState_c; - extern thread_local uint64_t prngState_a; - extern std::atomic _entropy_contributor; -} - inline uint64_t PRNG(uint64_t gift=42) - { - internal::prngState_c+= internal::_entropy_contributor.load(std::memory_order_relaxed); - internal::prngState_a= (internal::prngState_a << 17)+internal::prngState_c; - internal::prngState^=gift; - internal::prngState=internal::prngState*internal::prngState_a+internal::prngState_c; - return internal::prngState; - } - - inline void PRNG_feed(uint64_t feed) - { - internal::_entropy_contributor.fetch_xor(feed,std::memory_order_relaxed); - } - - inline void PRNG_feed_alt(uint64_t feed) - { - internal::_entropy_contributor.fetch_xor(feed,std::memory_order_relaxed); - } -} \ No newline at end of file diff --git a/include/FastRandom/UUID.h b/include/FastRandom/UUID.h deleted file mode 100644 index db68343..0000000 --- a/include/FastRandom/UUID.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Licenced under MIT Licence - * - * (c) Ludovic 'Archivist' Lagouardette 2018 - * - */ - -#include "FastRandom/Base_PRNG.h" -#include -#include -#include - -namespace archivist{ - - class fast_uuid{}; - class balanced_uuid{}; - class strong_uuid{}; - - struct UUID{ - static thread_local std::random_device src; - static thread_local uint8_t balance; - uint64_t state[2]; - UUID(fast_uuid) : UUID(){} - UUID() - { - state[0]=PRNG(9998); - state[1]=PRNG(17); - } - UUID(strong_uuid) - { - uint32_t internal=src(); - uint32_t cross=src() | std::chrono::duration_cast< std::chrono::microseconds >( - std::chrono::system_clock::now().time_since_epoch() - ).count(); - - state[0]=PRNG(internal); - PRNG_feed(cross); - state[1]=PRNG(cross); - } - UUID(balanced_uuid) - { - balance++; - if(balance%128) - { - *this=UUID(); - } - else - { - *this=UUID(strong_uuid()); - } - } - bool operator==(const UUID& rhs) const - { - return (state[0]==rhs.state[0])&&(state[1]==rhs.state[1]); - } - bool operator<(const UUID& rhs) const - { - return (state[0] +#include +#include + +namespace archivist +{ + + namespace internal + { + extern thread_local uint64_t prngState; + extern thread_local uint64_t prngState_c; + extern thread_local uint64_t prngState_a; + extern std::atomic _entropy_contributor; + } + + inline uint64_t prng(uint64_t gift = 42) + { + internal::prngState_c += internal::_entropy_contributor.load(std::memory_order_relaxed); + internal::prngState_a = (internal::prngState_a << 17) + internal::prngState_c; + internal::prngState ^= gift; + internal::prngState = internal::prngState * internal::prngState_a + internal::prngState_c; + return internal::prngState; + } + + inline void prng_feed(uint64_t feed) + { + internal::_entropy_contributor.fetch_xor(feed, std::memory_order_relaxed); + } + + inline void prng_feed_alt(uint64_t feed) + { + internal::_entropy_contributor.fetch_xor(feed, std::memory_order_relaxed); + } +} diff --git a/include/FastRandom/uuid.h b/include/FastRandom/uuid.h new file mode 100644 index 0000000..f87dd7d --- /dev/null +++ b/include/FastRandom/uuid.h @@ -0,0 +1,92 @@ +#ifndef archivist_uuid_h +#define archivist_uuid_h + +/** + * Licenced under MIT Licence + * + * (c) Ludovic 'Archivist' Lagouardette 2018 + * + */ + +#include "FastRandom/base_prng.h" +#include +#include +#include +#include + +namespace archivist +{ + + enum class uuid_modes + { + balanced, fast, strong + }; + + struct basic_uuid + { + static thread_local std::random_device src; + static thread_local uint8_t balance; + uint64_t state[2]{}; + + inline void init_default() + { + state[0] = prng(9998); + state[1] = prng(17); + } + + inline void init_strong() + { + uint32_t internal = src(); + uint32_t cross = src() | std::chrono::duration_cast< std::chrono::microseconds >( + std::chrono::system_clock::now().time_since_epoch()).count(); + + state[0] = prng(internal); + prng_feed(cross); + state[1] = prng(cross); + } + + bool operator==(const basic_uuid& rhs) const + { + return (state[0] == rhs.state[0]) && (state[1] == rhs.state[1]); + } + + bool operator<(const basic_uuid& rhs) const + { + return (state[0] < rhs.state[0]) || ((state[1] < rhs.state[1]) && (state[0] == rhs.state[0])); + } + }; + + using uuid = archivist::basic_uuid; + + struct uuid_balanced : public basic_uuid + { + uuid_balanced() + { + balance++; + if(balance % 128) + init_default(); + + else + init_strong(); + } + }; + + struct uuid_fast : public basic_uuid + { + uuid_fast() + { + init_default(); + } + }; + + struct uuid_strong : public basic_uuid + { + uuid_strong() + { + init_strong(); + } + }; + +} + +#endif//archivist_uuid_h diff --git a/src/FastRandom/Base_PRNG.cpp b/src/FastRandom/Base_PRNG.cpp deleted file mode 100644 index 288b450..0000000 --- a/src/FastRandom/Base_PRNG.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Licenced under MIT Licence - * - * (c) Ludovic 'Archivist' Lagouardette 2018 - * - */ - - #include "FastRandom/Base_PRNG.h" -#include -namespace archivist{ -thread_local uint64_t internal::prngState=0x7A7A7A7A5B5B5B5B; -thread_local uint64_t internal::prngState_a=0x00; -thread_local uint64_t internal::prngState_c=0x7A7A6565655B5B; -std::atomic internal::_entropy_contributor; - -class dummy168783486732{ -public: - dummy168783486732() - { - std::random_device rnd; - internal::prngState_a=rnd()*0x0000000100000000+rnd(); - PRNG_feed(rnd()); - } -}; -thread_local dummy168783486732 dummy168783486732_inst; -} \ No newline at end of file diff --git a/src/FastRandom/UUID.cpp b/src/FastRandom/UUID.cpp deleted file mode 100644 index 7ade21d..0000000 --- a/src/FastRandom/UUID.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/** - * Licenced under MIT Licence - * - * (c) Ludovic 'Archivist' Lagouardette 2018 - * - */ - - #include -#include "catch.hpp" -#include -#include - -thread_local std::random_device archivist::UUID::src; -thread_local uint8_t archivist::UUID::balance; - -using namespace archivist; - - -TEST_CASE("Test of fast UUIDs validity") -{ - const size_t loop=5000000; - std::set rec; - for(size_t lo=0;lo( - end-begin - ).count()/loop/10 - << "ns" - << std::endl; -} - -TEST_CASE("Test of strong UUIDs") -{ - const size_t loop=50000; - auto begin = std::chrono::system_clock::now(); - for(size_t lo=0;lo( - end-begin - ).count()/loop/10 - << "ns" - << std::endl; -} - -TEST_CASE("Test of balanced UUIDs") -{ - const size_t loop=50000; - auto begin = std::chrono::system_clock::now(); - for(size_t lo=0;lo( - end-begin - ).count()/loop/10 - << "ns" - << std::endl; -} \ No newline at end of file diff --git a/src/tests.cpp b/src/tests.cpp index 327af1f..4d98cdb 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1,9 +1,9 @@ /** * Licenced under MIT Licence - * + * * (c) Ludovic 'Archivist' Lagouardette 2018 * */ - #define CATCH_CONFIG_MAIN -#include "catch.hpp" \ No newline at end of file +#define CATCH_CONFIG_MAIN +#include "catch.hpp"