Thread: Help with random numbers

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    110

    Help with random numbers

    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;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    random is not random:
    if that code is only ran once on each start up of program then yes it will always return the same, because random is not random. if it is in a continuing run in the program and calling for a random number, or numbers they will change, but if you run that again you may/will start to see the same 'random' number patterns because random is not random. It relies on a mathematical algorithm to produce a 'random' number.street word says, this is why urandom and seed ( which I am not too familiar with) produces a larger pool of random numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random numbers not random?
    By yacek in forum C Programming
    Replies: 6
    Last Post: 10-08-2010, 06:57 PM
  2. Random Numbers
    By vbdave78 in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 08:47 AM
  3. Random Numbers
    By Robster66 in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2009, 10:22 PM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. random numbers
    By Smoose777 in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 12:33 PM

Tags for this Thread