source: trunk/yat/random/random.cc @ 680

Last change on this file since 680 was 680, checked in by Jari Häkkinen, 16 years ago

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1// $Id: random.cc 680 2006-10-11 17:49:03Z 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 "random.h"
25#include "yat/statistics/Histogram.h"
26
27#include <fstream>
28#include <iostream>
29
30namespace theplu {
31namespace yat {
32namespace random {
33
34  RNG* RNG::instance_ = NULL;
35
36
37  RNG::RNG(void)
38  {
39    gsl_rng_env_setup();  // support rng/seed changes through environment vars
40    rng_ = gsl_rng_alloc(gsl_rng_default);  // Memory is allocated here!
41  }
42
43
44  RNG::~RNG(void)
45  {
46    gsl_rng_free(rng_);
47    rng_=NULL;
48    delete instance_;
49  }
50
51 
52  u_long RNG::seed_from_devurandom(void)
53  {
54    u_char ulongsize=sizeof(u_long);
55    char* buffer=new char[ulongsize];
56    std::ifstream is("/dev/urandom");
57    is.read(buffer,ulongsize);
58    is.close();
59    u_long s=0;
60    for (u_int i=0; i<ulongsize; i++) {
61      u_char ch=buffer[i];
62      s+=ch<<((ulongsize-1-i)*8);
63    }
64    seed(s);
65    return s;
66  }
67
68
69  int RNG::set_state(const RNG_state& state)
70  {
71    return gsl_rng_memcpy(rng_, state.rng());
72  }
73
74
75  RNG_state::RNG_state(const RNG* rng)
76  {
77    rng_ = gsl_rng_clone(rng->rng()); 
78  }
79 
80
81  RNG_state::~RNG_state(void)
82  {
83    gsl_rng_free(rng_);
84    rng_=NULL;
85  }
86
87
88  DiscreteGeneral::DiscreteGeneral(const statistics::Histogram& hist)
89  {
90    double* p = new double[ hist.nof_bins() ];
91    for (size_t i=0; i<hist.nof_bins(); i++) 
92      p[ i ] = hist[i];
93    gen_ = gsl_ran_discrete_preproc( hist.nof_bins(), p );
94    delete p;
95  }
96
97
98  DiscreteGeneral::~DiscreteGeneral(void)
99  {
100    if (gen_)
101      gsl_ran_discrete_free( gen_ );
102    gen_ = NULL;
103  }
104
105}}} // of namespace random, yat, and theplu
Note: See TracBrowser for help on using the repository browser.