mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-29 22:08:45 +08:00
Merge remote-tracking branch 'origin/feature/random_pcg' into development
This commit is contained in:
commit
06afe8a22a
@ -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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user