ExRandom  3.0
Examples using u-rands
Back to Code organization. Forward to Multi-precision floating-point types. Up to Contents.

Here is how to use one of the *_dist classes. These are not C++11 random number distributions. But they offer the flexibility to access the u-rands generated by the algorithms. For example, the two functions to generate normal deviates are unit_normal_dist::generate and unit_normal_dist::value. The skeleton code to create and use these is

#include <iostream>
#include <random>
...
unsigned s = std::random_device()(); // Set seed from random_device
std::mt19937 g(s); // Initialize URNG
// Construct a rand_digit, a u_rand, and a unit_normal_dist
const unsigned b = 2;
long long c = D.count();
N.generate(g, x); // Normal generated as u-rand in x
std::cout << x << "\n"; // Print the u-rand
std::cout << x.value<double>(g); // Extract the double precision value
// Alternatively combine the two steps for the next normal deviate
std::cout << N.value<double>(g);
std::cout << D.count() - c0; // Number of random digits used
...

A complete example is sample_normal.cpp

#include <iostream>
#include <iomanip>
#include <random>
int main() {
std::cout << "Sampling from the normal distribution\n";
unsigned s = std::random_device()(); // Set seed from random_device
std::mt19937 g(s); // Initialize URNG
std::cout << "Seed set to " << s << "\n\n";
{
const unsigned b = 16;
std::cout << "Deviates rounded to floating point with base = " << b << "\n"
<< "columns are: initial u-rand, rounded float, final u-rand\n"
<< std::setprecision(std::numeric_limits<float>::digits10);
for (int i = 0; i < 10; ++i) {
N.generate(g, x);
std::cout << x << " = ";
std::cout << x.value<float>(g) << " = ";
std::cout << x << "\n";
}
std::cout << "Total number of base " << b << " digits used = "
<< D.count() << "\n\n";
}
{
const unsigned b = 10;
int p = 6;
std::cout << "Deviates rounded to fixed point with base = " << b
<< " and precision = " << p << "\n"
<< "columns are: initial u-rand, rounded fixed, final u-rand\n";
for (int i = 0; i < 10; ++i) {
N.generate(g, x);
std::cout << x << " = ";
std::cout << x.print_fixed(g,p) << " = ";
std::cout << x << "\n";
}
std::cout << "Total number of base " << b << " digits used = "
<< D.count() << "\n\n";
}
}

Running this program gives, for example,

Sampling from the normal distribution
Seed set to 237750281

Deviates rounded to floating point with base = 16
columns are: initial u-rand, rounded float, final u-rand
-0... = -0.690407 = -0.b0be828...
+0.1... = 0.0713383 = +0.124339d...
-0... = -0.686044 = -0.afa08bb...
+0.6... = 0.41938 = +0.6b5c7f9...
+0.2... = 0.132118 = +0.21d2840...
-1.3... = -1.2284 = -1.3a7842...
+0.4... = 0.286089 = +0.493d257...
+0... = 0.91659 = +0.eaa5ab4...
+1.dc... = 1.86227 = +1.dcbdbe...
+0... = 0.0933357 = +0.17e4d90...
Total number of base 16 digits used = 162

Deviates rounded to fixed point with base = 10 and precision = 6
columns are: initial u-rand, rounded fixed, final u-rand
+2.9... = +2.906062(+) = +2.9060622...
-0... = -0.824044(-) = -0.8240438...
+1.0... = +1.010208(-) = +1.0102079...
+0.0... = +0.015557(-) = +0.0155568...
+0... = +0.737249(+) = +0.7372493...
+2.5... = +2.511206(+) = +2.5112060...
+1.3... = +1.319720(-) = +1.3197198...
-1.1... = -1.194608(+) = -1.1946084...
+0.84... = +0.846072(-) = +0.8460715...
-0.7... = -0.705696(+) = -0.7056962...
Total number of base 10 digits used = 241

For similar examples using unit_exponential_dist and unit_uniform_dist, see sample_exponential.cpp and sample_normal.cpp.

Using discrete_normal_dist, we can similarly obtain the result directly as an int or we can obtain the partial sample i_rand object with represents a uniform range whose size is a power of the base. See sample_discrete_normal.cpp.

Back to Code organization. Forward to Multi-precision floating-point types. Up to Contents.