From 75bfbbcad3a584bf7c1d77bb373c7e81c5060811 Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sat, 10 Feb 2018 19:07:24 +0100 Subject: [PATCH 01/10] Aestethics --- include/FastRandom/Base_PRNG.h | 39 -------------- include/FastRandom/UUID.h | 62 ----------------------- include/FastRandom/base_prng.h | 42 ++++++++++++++++ include/FastRandom/uuid.h | 92 ++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 101 deletions(-) delete mode 100644 include/FastRandom/Base_PRNG.h delete mode 100644 include/FastRandom/UUID.h create mode 100644 include/FastRandom/base_prng.h create mode 100644 include/FastRandom/uuid.h 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..c0fc730 --- /dev/null +++ b/include/FastRandom/uuid.h @@ -0,0 +1,92 @@ +/** + * 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 {}; + + 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]{}; + + basic_uuid() + { + init_default(); + } + + 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() + { + if(++balance % 128) + init_default(); + + else + init_strong(); + } + }; + + struct uuid_fast : public basic_uuid + { + uuid_fast() : basic_uuid() + { + } + }; + + struct uuid_strong : public basic_uuid + { + uuid_strong() + { + init_strong(); + } + }; + +} From 9b9d774df72999357a21029a6653c5aec2c0eef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B1=D1=80=D0=B0=D0=B3=D0=B8=CC=81=D0=BC=20M?= Date: Sun, 11 Feb 2018 21:14:29 +0100 Subject: [PATCH 02/10] Delete Base_PRNG.h --- include/FastRandom/Base_PRNG.h | 39 ---------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 include/FastRandom/Base_PRNG.h 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 From 90132c40cf6774aa9fbc31517de6369ed370e1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B1=D1=80=D0=B0=D0=B3=D0=B8=CC=81=D0=BC=20M?= Date: Sun, 11 Feb 2018 21:14:42 +0100 Subject: [PATCH 03/10] Delete UUID.h --- include/FastRandom/UUID.h | 62 --------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 include/FastRandom/UUID.h 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] Date: Sat, 10 Feb 2018 19:41:05 +0100 Subject: [PATCH 04/10] Updated README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cd18db6..df34219 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,15 @@ FastRandom is a small library made to generate random enough numbers and UUIDs. ```c++ #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 -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 ``` From 04d2a5793a574a5aff1258e1ad83f031d9030e1b Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sat, 10 Feb 2018 22:01:34 +0100 Subject: [PATCH 05/10] Updated README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index df34219..91a0ca9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 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_fast weak; // generate a hasty UUID archivist::uuid_balanced medium; // generate a probably strong UUID @@ -13,7 +13,7 @@ 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) From 2da74be44a848300d18084cad3abc2d5bc7e90ec Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sat, 10 Feb 2018 22:02:05 +0100 Subject: [PATCH 06/10] Updated README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 91a0ca9..b9a1959 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,15 @@ FastRandom is a small library made to generate random enough numbers and UUIDs. ```c++ #include -archivist::uuid_fast weak; // generate a hasty 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 +archivist::uuid_strong good; // generate a really random UUID ``` ```c++ #include -uint64_t number1 = archivist::prng(); // Generate a random number +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 +archivist::prng_feed(some_entropy); // Give some entropy system wide ``` From 3041df8e97e268798d2d54173aaf7f0316a89d52 Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sun, 18 Feb 2018 18:19:03 +0100 Subject: [PATCH 07/10] Fixed slight sluggishness in uuid_balanced and uuid_strong --- include/FastRandom/uuid.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/include/FastRandom/uuid.h b/include/FastRandom/uuid.h index c0fc730..bce0696 100644 --- a/include/FastRandom/uuid.h +++ b/include/FastRandom/uuid.h @@ -1,3 +1,6 @@ +#ifndef archivist_uuid_h +#define archivist_uuid_h + /** * Licenced under MIT Licence * @@ -7,14 +10,12 @@ #include "FastRandom/base_prng.h" #include +#include #include #include namespace archivist { - class fast_uuid {}; - class balanced_uuid {}; - class strong_uuid {}; enum class uuid_modes { @@ -27,18 +28,14 @@ namespace archivist static thread_local uint8_t balance; uint64_t state[2]{}; - basic_uuid() - { - init_default(); - } - inline void init_default() { state[0] = prng(9998); state[1] = prng(17); + //std::cout << "init_default\n"; } - inline void init_strong() + void init_strong() { uint32_t internal = src(); uint32_t cross = src() | std::chrono::duration_cast< std::chrono::microseconds >( @@ -47,6 +44,7 @@ namespace archivist state[0] = prng(internal); prng_feed(cross); state[1] = prng(cross); + //std::cout << "init_strong\n"; } bool operator==(const basic_uuid& rhs) const @@ -66,7 +64,8 @@ namespace archivist { uuid_balanced() { - if(++balance % 128) + balance++; + if(balance % 128) init_default(); else @@ -76,8 +75,9 @@ namespace archivist struct uuid_fast : public basic_uuid { - uuid_fast() : basic_uuid() + uuid_fast() { + init_default(); } }; @@ -90,3 +90,5 @@ namespace archivist }; } + +#endif//archivist_uuid_h From daaaa2c4565f3bf129fd779f66e54a907553cc3a Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sun, 18 Feb 2018 18:19:43 +0100 Subject: [PATCH 08/10] Updated tests to reflect new api --- src/tests.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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" From 39ccc233f5f3b0b222c97a124db50115d725d13e Mon Sep 17 00:00:00 2001 From: ibroheem Date: Sun, 18 Feb 2018 18:30:00 +0100 Subject: [PATCH 09/10] Fixed slight sluggishness in uuid_balanced and uuid_strong --- include/FastRandom/uuid.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/FastRandom/uuid.h b/include/FastRandom/uuid.h index bce0696..f87dd7d 100644 --- a/include/FastRandom/uuid.h +++ b/include/FastRandom/uuid.h @@ -32,10 +32,9 @@ namespace archivist { state[0] = prng(9998); state[1] = prng(17); - //std::cout << "init_default\n"; } - void init_strong() + inline void init_strong() { uint32_t internal = src(); uint32_t cross = src() | std::chrono::duration_cast< std::chrono::microseconds >( @@ -44,7 +43,6 @@ namespace archivist state[0] = prng(internal); prng_feed(cross); state[1] = prng(cross); - //std::cout << "init_strong\n"; } bool operator==(const basic_uuid& rhs) const From d474d1112b3ee479a30a66fc3b402ebe54c29cd8 Mon Sep 17 00:00:00 2001 From: ibroheem Date: Tue, 29 May 2018 02:51:25 +0100 Subject: [PATCH 10/10] updated tests --- src/FastRandom/Base_PRNG.cpp | 26 -------- src/FastRandom/UUID.cpp | 114 ----------------------------------- 2 files changed, 140 deletions(-) delete mode 100644 src/FastRandom/Base_PRNG.cpp delete mode 100644 src/FastRandom/UUID.cpp 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