#include <iostream>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <math.h>
#include <sys/time.h>

using namespace std;


unsigned long int RandomUnsignedLongInt( const unsigned long int min, const unsigned long int max, gsl_rng* r ) {
  double value = gsl_rng_uniform( r );
  return static_cast<unsigned long int>( floor( value*((1.0 + max - min) + min ) ) );
}


int main( int argc, char** argv ) {
  // getting a global seed from gettimeofday
  struct timeval t_time_value;
  gettimeofday( &t_time_value, NULL );
  unsigned long int un_global_seed = t_time_value.tv_usec;
  cerr << "[INFO] global seed for random number generator is " << un_global_seed << endl;

  // intialising random number generator
  cerr << "[INFO] initialising random number generator \"locally\"..." << endl;
  gsl_rng_env_setup();
  const gsl_rng_type* T = gsl_rng_default;
  gsl_rng* r = gsl_rng_alloc ( T );
  gsl_rng_set( r, un_global_seed );

  // getting the "local" seed
  unsigned long int un_local_seed = RandomUnsignedLongInt( 0, ULONG_MAX, r );
  cerr << "[INFO] getting the \"local\" seed: " << un_local_seed << endl;
  
  // setting the "local" seed
  gsl_rng_set( r, un_local_seed );

  // getting and printing a new random number
  cerr << "[INFO] getting a \"local\" random int in [0:100000]: " << RandomUnsignedLongInt( 0, 100000, r ) << endl;

  return 0;
}
