1 | // $Id: random.cc 675 2006-10-10 12:08:45Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/random/random.h" |
---|
25 | #include "yat/statistics/Histogram.h" |
---|
26 | |
---|
27 | #include <fstream> |
---|
28 | #include <iostream> |
---|
29 | |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace random { |
---|
33 | |
---|
34 | |
---|
35 | RNG* RNG::instance_ = NULL; |
---|
36 | |
---|
37 | |
---|
38 | RNG::RNG(void) |
---|
39 | { |
---|
40 | gsl_rng_env_setup(); // support rng/seed changes through environment vars |
---|
41 | rng_ = gsl_rng_alloc(gsl_rng_default); // Memory is allocated here! |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | RNG::~RNG(void) |
---|
47 | { |
---|
48 | gsl_rng_free(rng_); |
---|
49 | rng_=NULL; |
---|
50 | delete instance_; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | u_long RNG::seed_from_devurandom(void) |
---|
56 | { |
---|
57 | u_char ulongsize=sizeof(u_long); |
---|
58 | char* buffer=new char[ulongsize]; |
---|
59 | std::ifstream is("/dev/urandom"); |
---|
60 | is.read(buffer,ulongsize); |
---|
61 | is.close(); |
---|
62 | u_long s=0; |
---|
63 | for (u_int i=0; i<ulongsize; i++) { |
---|
64 | u_char ch=buffer[i]; |
---|
65 | s+=ch<<((ulongsize-1-i)*8); |
---|
66 | } |
---|
67 | seed(s); |
---|
68 | return s; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | int RNG::set_state(const RNG_state& state) |
---|
74 | { |
---|
75 | return gsl_rng_memcpy(rng_, state.rng()); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | RNG_state::RNG_state(const RNG* rng) |
---|
81 | { |
---|
82 | rng_ = gsl_rng_clone(rng->rng()); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | RNG_state::~RNG_state(void) |
---|
88 | { |
---|
89 | gsl_rng_free(rng_); |
---|
90 | rng_=NULL; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist) |
---|
96 | { |
---|
97 | double* p = new double[ hist.nof_bins() ]; |
---|
98 | for (size_t i=0; i<hist.nof_bins(); i++) |
---|
99 | p[ i ] = hist[i]; |
---|
100 | gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p ); |
---|
101 | delete p; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | DiscreteGeneral::~DiscreteGeneral(void) |
---|
107 | { |
---|
108 | if (gen_) |
---|
109 | gsl_ran_discrete_free( gen_ ); |
---|
110 | gen_ = NULL; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | }} // of namespace random and namespace theplu |
---|