Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

random.h

00001 /***************************************************************************
00002                           random.h  -  description
00003                              -------------------
00004     begin                : Mon Jul 24 2000
00005     copyright            : (C) 2000 by Michael Peeters
00006     email                : Michael.Peeters@vub.ac.be
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef RANDOM_H
00019 #define RANDOM_H
00020 
00021 #include "numerictypes.h"
00022 #include <stdexcept>
00023 #include <time.h>
00024 
00025 namespace MODEL{
00026 
00030         class Random {
00031         public: 
00032                 Random(counter initial_seed=0) {seed(initial_seed);}
00033 
00035                 number operator()(void) {number temp; return generate(temp);}
00036 
00038                 virtual number& generate(number& storage) = 0;
00039 
00041                 void seed(integer seed) 
00042                         { 
00043                                 if(seed!=0)
00044                                         _seed=seed;
00045                                 else 
00046                                   _seed=::time(NULL);
00047                         }                         
00049                 counter get_seed(void) {return _seed;}
00050 
00051         protected:
00052                 counter _seed;
00053         };
00054 
00056         class Uniform : public Random
00057                 {
00058                 public:
00059                         Uniform(counter inital_seed=0);
00060                         virtual number& generate(number& storage);
00061                         const counter& seed_update(void);
00062    
00063                 private:
00064                         static const counter IA=16807;
00065                         static const counter IM=2147483647;
00066                         static const number AM=1.0/IM;
00067                         static const counter IQ=127773;
00068                         static const counter IR=2836;
00069                         static const counter tabelsize=32;
00070                         static const counter NDIV = 1+(IM-1)/tabelsize;
00071                         static const number RNMX=1.0-EPS;
00072 
00073                         counter tabel[tabelsize];
00074 
00075                         counter lastseed;
00076                 };
00077 
00081         class Normal {
00082         public:
00083                 Normal(counter seed=0);
00084                 virtual ~Normal();
00085                 virtual number& generate(number& storage);
00086                 number operator()(void) {number temp; return generate(temp);}
00087 
00088         private:
00089                 Uniform* rnd;
00090                 bool regenerate2; //numbers are generated in pairs
00091                 number saved;//the second one
00092         };
00093 
00094 } // end namespace
00095 #endif
00096 
00097 /*********************************************************************
00098 $Id: random.h,v 1.1 2001/05/22 10:54:55 mpeeters Exp $
00099 **********************************************************************
00100 
00101 $Log: random.h,v $
00102 Revision 1.1  2001/05/22 10:54:55  mpeeters
00103 Moved sources and headers for libModel to model/ subdirectory, in an attempt to rationalize the source tree. This should make things "netter".
00104 
00105 Revision 1.3  2000/09/29 13:00:49  mpeeters
00106 Added ChangeLog (using cvs2cl.pl script by Karl Fogel)
00107 Changed makefile to take into account tutorial
00108 Muddled around in random.h
00109 Added tutorial section to the Introduction
00110 
00111 Revision 1.2  2000/09/15 10:26:31  mpeeters
00112 Cleaned out header and added CVS tails to files, removed superfuous
00113 @author comments, inserted dates
00114 
00115 *********************************************************************/

To get the sources or tarballs, please go to SourceForge or you can use the CVS repository.

More Info? Michael Peeters. Also, check our research website: www.alna.vub.ac.be

Last update: June 2002.


Looking for Open Source? Check out SourceForge Logo !