mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-29 22:08:45 +08:00
Merge remote-tracking branch 'origin/feature/random_hash' into development
This commit is contained in:
commit
631cd66e19
@ -1,4 +1,4 @@
|
||||
///\file
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
@ -289,6 +289,7 @@ namespace etl
|
||||
//***************************************************************************
|
||||
/// A 32 bit random number generator.
|
||||
/// Uses a linear shift feedback register.
|
||||
/// Polynomial 0x80200003
|
||||
/// https://en.wikipedia.org/wiki/Linear-feedback_shift_register
|
||||
//***************************************************************************
|
||||
class random_lsfr : public random
|
||||
@ -499,6 +500,73 @@ namespace etl
|
||||
|
||||
uint64_t value;
|
||||
};
|
||||
|
||||
#if ETL_8BIT_SUPPORT
|
||||
//***************************************************************************
|
||||
/// A 32 bit random number generator.
|
||||
/// Applies a user supplied 32bit hash to a counter.
|
||||
/// The hash must implement 'void add(uint8_t)' and 'uint8_t value()' member functions.
|
||||
//***************************************************************************
|
||||
template <typename THash>
|
||||
class random_hash : public random
|
||||
{
|
||||
public:
|
||||
|
||||
random_hash()
|
||||
{
|
||||
// An attempt to come up with a unique non-zero seed,
|
||||
// based on the address of the instance.
|
||||
uintptr_t n = reinterpret_cast<uintptr_t>(this);
|
||||
value = static_cast<uint32_t>(n);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Constructor with seed value.
|
||||
///\param seed The new seed value.
|
||||
//***************************************************************************
|
||||
random_hash(uint32_t seed)
|
||||
{
|
||||
initialise(seed);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Initialises the sequence with a new seed value.
|
||||
///\param seed The new seed value.
|
||||
//***************************************************************************
|
||||
void initialise(uint32_t seed)
|
||||
{
|
||||
value = seed;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Get the next random_lsfr number.
|
||||
//***************************************************************************
|
||||
uint32_t operator()()
|
||||
{
|
||||
++value;
|
||||
hash.add(value);
|
||||
return hash.value();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Get the next random_lsfr number in a specified inclusive range.
|
||||
//***************************************************************************
|
||||
uint32_t range(uint32_t low, uint32_t high)
|
||||
{
|
||||
uint32_t r = high - low + 1;
|
||||
uint32_t n = operator()();
|
||||
n %= r;
|
||||
n += low;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
THash hash;
|
||||
uint8_t value;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -38,7 +38,7 @@ SOFTWARE.
|
||||
///\ingroup utilities
|
||||
|
||||
#define ETL_VERSION_MAJOR 14
|
||||
#define ETL_VERSION_MINOR 0
|
||||
#define ETL_VERSION_MINOR 1
|
||||
#define ETL_VERSION_PATCH 0
|
||||
|
||||
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH)
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
===============================================================================
|
||||
14.1.0
|
||||
Added hash based random number generator
|
||||
|
||||
===============================================================================
|
||||
14.0.0
|
||||
The ETL is now 'all header'.
|
||||
|
||||
@ -31,6 +31,7 @@ SOFTWARE.
|
||||
#include <stdint.h>
|
||||
|
||||
#include "etl/random.h"
|
||||
#include "etl/crc32.h"
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
@ -357,5 +358,59 @@ namespace
|
||||
CHECK(n <= high);
|
||||
}
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_random_hash_sequence)
|
||||
{
|
||||
std::vector<uint32_t> out1(10000);
|
||||
etl::random_hash<etl::crc32> r;
|
||||
|
||||
struct generator
|
||||
{
|
||||
generator(etl::random& r_)
|
||||
: r(r_)
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t operator()()
|
||||
{
|
||||
return r();
|
||||
}
|
||||
|
||||
etl::random& r;
|
||||
};
|
||||
|
||||
std::generate(out1.begin(), out1.end(), generator(r));
|
||||
|
||||
std::ofstream file("random_hash.csv");
|
||||
|
||||
if (!file.fail())
|
||||
{
|
||||
for (size_t i = 0; i < out1.size(); i += 2)
|
||||
{
|
||||
file << out1[i] << "," << out1[i + 1] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_random_hash_range)
|
||||
{
|
||||
etl::random_hash<etl::crc32> r;
|
||||
|
||||
uint32_t low = 1234;
|
||||
uint32_t high = 9876;
|
||||
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
{
|
||||
uint32_t n = r.range(low, high);
|
||||
|
||||
CHECK(n >= low);
|
||||
CHECK(n <= high);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
1
test/vs2017/.gitignore
vendored
1
test/vs2017/.gitignore
vendored
@ -4,3 +4,4 @@
|
||||
/random_mwc.csv
|
||||
/random_xorshift.csv
|
||||
/random_pcg.csv
|
||||
/random_hash.csv
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user