#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <random>
int main()
{
int n = 3 ;
std::cout << std::fixed << std::setprecision(2) ;
// legacy: generate ten random numbers within n - 0.5 and n + 0.5
std::srand( std::time(nullptr) ) ;
for( int i = 0 ; i < 10 ; ++i )
{
constdouble r = std::rand() / double(RAND_MAX) ; // random number in [ 0.0, 1.0 ]
constdouble delta = r - 0.5 ; // random number in [ -0.5, +0.5 ]
std::cout << n + delta << ' ' ;
}
std::cout << '\n' ;
// C++11: generate ten random numbers within n - 0.5 and n + 0.5
std::mt19937 rng( std::random_device{}() ) ;
std::uniform_real_distribution<double> distrib( -0.5, +0.5 ) ;
for( int i = 0 ; i < 10 ; ++i )
{
constdouble delta = distrib(rng) ; // random number in [ -0.5, +0.5 ]
std::cout << n + delta << ' ' ;
}
std::cout << '\n' ;
}