source: trunk/src/random_singleton.cc @ 13

Last change on this file since 13 was 13, checked in by daniel, 20 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.1 KB
Line 
1#include "random_singleton.h"
2
3using namespace thep_cpp_tools;
4
5// Static members
6//////////////////
7bool random_singleton::instance_ = false;
8random_singleton* random_singleton::singleobj_ = NULL;
9
10
11// Constructors and destructors
12///////////////////////////////
13random_singleton::random_singleton()
14{
15  const gsl_rng_type* T;
16  gsl_rng_env_setup();
17  T = gsl_rng_default;
18  r_ = gsl_rng_alloc( T );  // Memory is allocated here!
19  gen_ = NULL;   // Only to be used in the general purpose distribution
20}
21
22
23random_singleton::~random_singleton()
24{
25  instance_ = false;
26  gsl_rng_free( r_ );
27  r_ = NULL;
28  if( gen_ != 0 ) gsl_ran_discrete_free( gen_ );
29  gen_ = NULL;
30}
31
32
33// Functions (A-Z)
34//////////////////
35void random_singleton::set_seed( const int& seed )
36{
37  if( seed < 0 ) 
38    {
39     gsl_rng_set( r_, time( 0 ) ); 
40    }
41  else
42    {
43     gsl_rng_set( r_, seed );
44    }
45}
46
47
48random_singleton* random_singleton::get_instance( int seed )
49{
50  if( !instance_ ) 
51    {
52     singleobj_ = new random_singleton();
53     singleobj_->set_seed( seed );
54     instance_ = true;
55     return singleobj_;
56    }
57  else
58    {
59     return singleobj_;
60    }
61}
62
63
Note: See TracBrowser for help on using the repository browser.