Commit 585d1eb7 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Support double random number generation in range [0,1).

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 9e3838ff
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <random>
class Random class Random
{ {
...@@ -41,6 +42,8 @@ private: ...@@ -41,6 +42,8 @@ private:
std::vector<uint64_t> buffer; std::vector<uint64_t> buffer;
size_t buffer_pos = 0; size_t buffer_pos = 0;
std::uniform_real_distribution<double> double_dist;
void die_on_error (const char *func, gcry_error_t error); void die_on_error (const char *func, gcry_error_t error);
public: public:
Random (uint64_t seed, Stream stream); Random (uint64_t seed, Stream stream);
...@@ -54,6 +57,21 @@ public: ...@@ -54,6 +57,21 @@ public:
return buffer[buffer_pos++]; return buffer[buffer_pos++];
} }
uint64_t
min() const
{
return 0;
}
uint64_t
max() const
{
return UINT64_MAX;
}
double
random_double() /* range [0,1) */
{
return double_dist (*this);
}
void refill_buffer(); void refill_buffer();
void seed (uint64_t seed, Stream stream); void seed (uint64_t seed, Stream stream);
......
...@@ -30,6 +30,8 @@ main (int argc, char **argv) ...@@ -30,6 +30,8 @@ main (int argc, char **argv)
uint64_t x = rng(); uint64_t x = rng();
printf ("%016lx\n", x); printf ("%016lx\n", x);
} }
for (size_t i = 0; i < 20; i++)
printf ("%f\n", rng.random_double());
uint64_t s = 0; uint64_t s = 0;
double t_start = get_time(); double t_start = get_time();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment