mardi 30 décembre 2014

c++1y way to generate random floating point number

I need in random floating point numbers. I want to test implementations of an elementary functions in my JIT-compiler for a custom floating point type. So I do the following:



  • I testing the function (say, exp) for fixed points (say, zero -> one, one -> Euler constant, ln2 -> two)

  • Then I generate full-range floating-point numbers (adjacent to domain of the elementary function) and testing my function on them.


How to generate desired numbers? What I do at the moment is the following:



#include <iostream>
#include <iomanip>
#include <random>
#include <chrono>
#include <limits>

#include <cmath>
#include <cstdlib>

int
main()
{
using G = long double;
G const one = G(1);
std::mt19937_64 random_(static_cast< typename std::mt19937_64::result_type >(std::chrono::high_resolution_clock::now().time_since_epoch().count()));
std::uniform_real_distribution< G > zero_to_one_; // uniform [0;1) ditribution
std::uniform_int_distribution< int > uniform_int_(std::numeric_limits< G >::min_exponent - 1, std::numeric_limits< G >::max_exponent - 1);
std::cout.precision(std::numeric_limits< G >::digits10);
for (std::size_t i = 0; i < 100; ++i) {
std::cout << std::scalbn(one + zero_to_one_(random_), uniform_int_(random_)) << std::endl;
}
return EXIT_SUCCESS;
}

Aucun commentaire:

Enregistrer un commentaire