Last change
on this file since 560 was
425,
checked in by Jari Häkkinen, 16 years ago
|
Fixed bug in DiscreteUniform?.
Added seeding support for classes derived from Discrete and Continuous
virtual classes.
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Id
|
File size:
1.2 KB
|
Line | |
---|
1 | // $Id: random.cc 425 2005-12-07 14:14:24Z jari $ |
---|
2 | |
---|
3 | #include <c++_tools/random/random.h> |
---|
4 | #include <c++_tools/statistics/Histogram.h> |
---|
5 | |
---|
6 | #include <fstream> |
---|
7 | #include <iostream> |
---|
8 | |
---|
9 | |
---|
10 | namespace theplu { |
---|
11 | namespace random { |
---|
12 | |
---|
13 | |
---|
14 | RNG* RNG::instance_ = NULL; |
---|
15 | |
---|
16 | |
---|
17 | RNG::RNG(void) |
---|
18 | { |
---|
19 | gsl_rng_env_setup(); // support rng/seed changes through environment vars |
---|
20 | rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | RNG::~RNG(void) |
---|
26 | { |
---|
27 | gsl_rng_free(rng_); |
---|
28 | rng_=NULL; |
---|
29 | delete instance_; |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | u_long RNG::seed_from_devurandom(void) |
---|
35 | { |
---|
36 | u_char ulongsize=sizeof(u_long); |
---|
37 | char* buffer=new char[ulongsize]; |
---|
38 | std::ifstream is("/dev/urandom"); |
---|
39 | is.read(buffer,ulongsize); |
---|
40 | is.close(); |
---|
41 | u_long s=0; |
---|
42 | for (u_int i=0; i<ulongsize; i++) { |
---|
43 | u_char ch=buffer[i]; |
---|
44 | s+=ch<<((ulongsize-1-i)*8); |
---|
45 | } |
---|
46 | seed(s); |
---|
47 | return s; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) |
---|
53 | { |
---|
54 | double* p = new double[ hist.nof_bins() ]; |
---|
55 | for (size_t i=0; i<hist.nof_bins(); i++) |
---|
56 | p[ i ] = hist[i]; |
---|
57 | gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); |
---|
58 | delete p; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | DiscreteGeneral::~DiscreteGeneral(void) |
---|
64 | { |
---|
65 | if (gen_) |
---|
66 | gsl_ran_discrete_free( gen_ ); |
---|
67 | gen_ = NULL; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | }} // of namespace random and namespace theplu |
---|
Note: See
TracBrowser
for help on using the repository browser.