mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Merge remote-tracking branch 'origin/development'
# Conflicts: # include/etl/version.h # support/Release notes.txt
This commit is contained in:
parent
fb3d4d78fa
commit
df842eacec
@ -162,6 +162,29 @@ namespace etl
|
||||
uint32_t value1;
|
||||
uint32_t value2;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A 32 bit random number generator.
|
||||
/// Uses a permuted congruential generator calculation.
|
||||
/// https://en.wikipedia.org/wiki/Permuted_congruential_generator
|
||||
//***************************************************************************
|
||||
class random_pcg : public random
|
||||
{
|
||||
public:
|
||||
|
||||
random_pcg();
|
||||
explicit random_pcg(uint32_t seed);
|
||||
void initialise(uint32_t seed);
|
||||
uint32_t operator()();
|
||||
uint32_t range(uint32_t low, uint32_t high);
|
||||
|
||||
private:
|
||||
|
||||
static const uint64_t multiplier = 6364136223846793005ULL;
|
||||
static const uint64_t increment = 1ULL;
|
||||
|
||||
uint64_t value;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -37,12 +37,12 @@ SOFTWARE.
|
||||
/// Definitions of the ETL version
|
||||
///\ingroup utilities
|
||||
|
||||
#define ETL_VERSION "12.0.1"
|
||||
#define ETL_VERSION_W L"12.0.1"
|
||||
#define ETL_VERSION_U16 u"12.0.1"
|
||||
#define ETL_VERSION_U32 U"12.0.1"
|
||||
#define ETL_VERSION "12.1.1"
|
||||
#define ETL_VERSION_W L"12.1.1"
|
||||
#define ETL_VERSION_U16 u"12.1.1"
|
||||
#define ETL_VERSION_U32 U"12.1.1"
|
||||
#define ETL_VERSION_MAJOR 12
|
||||
#define ETL_VERSION_MINOR 0
|
||||
#define ETL_VERSION_MINOR 1
|
||||
#define ETL_VERSION_PATCH 1
|
||||
#define ETL_VERSION_VALUE ((ETL_VERSION_MAJOR * 10000) + (ETL_VERSION_MINOR * 100) + ETL_VERSION_PATCH)
|
||||
|
||||
|
||||
@ -30,6 +30,7 @@ SOFTWARE.
|
||||
|
||||
#include "etl/platform.h"
|
||||
#include "etl/random.h"
|
||||
#include "etl/binary.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
@ -349,4 +350,64 @@ namespace etl
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
// Permuted congruential generator.
|
||||
//***************************************************************************
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
/// Attempts to come up with a unique non-zero seed.
|
||||
//***************************************************************************
|
||||
random_pcg::random_pcg()
|
||||
{
|
||||
// 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<uint64_t>(n);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Constructor with seed value.
|
||||
///\param seed The new seed value.
|
||||
//***************************************************************************
|
||||
random_pcg::random_pcg(uint32_t seed)
|
||||
{
|
||||
initialise(seed);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Initialises the sequence with a new seed value.
|
||||
///\param seed The new seed value.
|
||||
//***************************************************************************
|
||||
void random_pcg::initialise(uint32_t seed)
|
||||
{
|
||||
value = uint64_t(seed) | (uint64_t(seed) << 32);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Get the next random_lsfr number.
|
||||
//***************************************************************************
|
||||
uint32_t random_pcg::operator()()
|
||||
{
|
||||
uint64_t x = value;
|
||||
unsigned count = (unsigned)(value >> 59);
|
||||
|
||||
value = (x * multiplier) + increment;
|
||||
x ^= x >> 18;
|
||||
return etl::rotate_right((uint32_t)(x >> 27), count);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Get the next random_lsfr number in a specified inclusive range.
|
||||
//***************************************************************************
|
||||
uint32_t random_pcg::range(uint32_t low, uint32_t high)
|
||||
{
|
||||
uint32_t r = high - low + 1;
|
||||
uint32_t n = operator()();
|
||||
n %= r;
|
||||
n += low;
|
||||
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
===============================================================================
|
||||
12.1.1
|
||||
Added random_pcg Permuted Congruential Generator
|
||||
|
||||
===============================================================================
|
||||
12.0.1
|
||||
Modified state_chart to accept recursive events.
|
||||
|
||||
@ -253,7 +253,7 @@ namespace
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_random_fast_sequence)
|
||||
TEST(test_random_mwc_sequence)
|
||||
{
|
||||
std::vector<uint32_t> out1(10000);
|
||||
etl::random_mwc r;
|
||||
@ -289,7 +289,7 @@ namespace
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_random_fast_range)
|
||||
TEST(test_random_mwc_range)
|
||||
{
|
||||
etl::random_mwc r;
|
||||
|
||||
@ -304,5 +304,58 @@ namespace
|
||||
CHECK(n <= high);
|
||||
}
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_random_pcg_sequence)
|
||||
{
|
||||
std::vector<uint32_t> out1(10000);
|
||||
etl::random_pcg 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_pcg.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_pcg_range)
|
||||
{
|
||||
etl::random_pcg 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
@ -3,3 +3,4 @@
|
||||
/random_lsfr.csv
|
||||
/random_mwc.csv
|
||||
/random_xorshift.csv
|
||||
/random_pcg.csv
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user