Hello!
I would be grateful if someone could explain why this code produces identical random numbers (vectors). I am trying to generate a series of uniform and (flat)-dirichlet distributed random vectors of size n. The dirichlet random variables are obtained by first generating exponential random variables and then computing a vector of shares in the sum of n exponential random variables. But the vectors are identical each time. Is this a seed issue?
Thank you for your help
serge
Code:
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <functional>
#include <iostream>
#include <random>
#include <string>
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
int main ( int argc , char* argv[ ] ) {
// DIMENSION ( default 3 )
int n = 3;
if ( argc > 1 ) n = std::stoi ( argv[ 1 ] );
// ITERATIONS ( default 100 )
long total_iterations = 100ll;
if ( argc > 2 ) total_iterations = std::stol ( argv[ 2 ] );
// ROUND OUTPUT TO DECIMAL PLACES ( default 3 )
int display_precision = 3;
if ( argc > 3 ) display_precision = std::stoi ( argv[ 3 ] );
// SET FORMAT FOR DECIMAL OUTPUT
std::cout.unsetf ( std::ios::floatfield );
std::cout.precision ( display_precision ); // number of decimal places
std::cout.setf ( std::ios::fixed , std::ios::floatfield ); // display fixed length
// PSEUDO-RANDOM NUMBER GENERATOR
std::random_device rnd_device;
std::mt19937 mersenne_engine( rnd_device( ) );
std::uniform_real_distribution<double> uniform_dist( 0.0 , 1.0 );
std::exponential_distribution<double> exponential_dist( 1.0 );
auto uniform_generator = std::bind( uniform_dist , mersenne_engine );
auto exponential_generator = std::bind( exponential_dist , mersenne_engine );
// PSEUDO-RANDOM VECTORS
std::vector<double> uniform_vec( n , 0.0f );
std::vector<double> exponential_vec( n , 0.0f );
double sum_exponential_vec = 0.0f;
std::vector<double> dirichlet_vec( n , 1.0f );
for ( int i = 0; i < total_iterations; ++i ) {
generate( begin( uniform_vec ) , end( uniform_vec ) , uniform_generator );
generate( begin( exponential_vec ) , end( exponential_vec ) , exponential_generator );
sum_exponential_vec = std::accumulate( exponential_vec.begin( ) , exponential_vec.end( ) , 0.0f );
transform( exponential_vec.begin( ) , exponential_vec.end( ) , dirichlet_vec.begin( ), std::bind2nd( std::divides<double>( ) , sum_exponential_vec ) );
// PRINT OUTPUT
std::cout << "inputs: " << n << " " << total_iterations << " " << display_precision << std::endl;
std::cout << "uniform: ";
for ( auto j: uniform_vec ) std::cout << j << ' ';
std::cout << std::endl;
std::cout << "exponential: ";
for ( auto j: exponential_vec ) std::cout << j << ' ';
std::cout << "sum: " << sum_exponential_vec << std::endl;
std::cout << "flat dirichlet: ";
for ( auto j: dirichlet_vec ) std::cout << j << ' ';
std::cout << std::endl;
std::cout << std::endl;
}
return 0;
}