adevs
/home/rotten/adevs-2.6/include/adevs_rand.h
00001 /***************
00002 Copyright (C) 2000-2006 by James Nutaro
00003 
00004 This library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Lesser General Public
00006 License as published by the Free Software Foundation; either
00007 version 2 of the License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 Bugs, comments, and questions can be sent to nutaro@gmail.com
00019 ***************/
00020 #ifndef __adevs_rand_h_
00021 #define __adevs_rand_h_
00022 #include "adevs.h"
00023 #include <cstdlib>
00024 
00025 namespace adevs
00026 {
00027 
00028 typedef enum 
00029 {
00030         NDURATION,
00031         NCOUNT,EMPTY,
00032         NDELAY,
00033         ADDTOQ,
00034         EMPTYQ,
00035         HNCLMS,
00036         HMODE,
00037         PROBVAL,
00038         ERRUNIFORM,
00039         ERRNORMAL,
00040         ERRLOGNORM,
00041         ERRTRIANG,
00042         ERRGAMMA,
00043         ERRBETA,
00044         ERREXPONENT,
00045         ERRERLANG,
00046         ERRHYPGEO,
00047         NULLEV,
00048         NOHISTO,
00049         INITERR,
00050         AMODE,
00051         HFORM,
00052         ERRFILE,
00053         SAMPLE,
00054         FRACTION,
00055         LEVEL,
00056         SCAN,
00057         SUPPRESS,
00058         SEED
00059 } errorType;
00060 
00065 class random_seq 
00066 {
00067         public:
00069                 virtual void set_seed(unsigned long seed) = 0;
00071                 virtual double next_dbl() = 0;
00073                 virtual random_seq* copy() const = 0;
00074                 virtual unsigned long next_long() = 0;
00076                 virtual ~random_seq(){}
00077 };
00078 
00085 class mtrand: public random_seq 
00086 {
00087         public:
00089                 mtrand(unsigned long seed = 1);
00091                 mtrand(const unsigned long* seed_array); 
00093                 mtrand(const mtrand& src);
00095                 const mtrand& operator=(const mtrand& src);
00097                 void set_seed(unsigned long seed);
00099                 random_seq* copy() const;
00101                 unsigned long next_long();
00103                 double next_dbl();
00105                 ~mtrand ();
00106         private:        
00107                 // Wrappers around the original function calls
00108                 void sgenrand(unsigned long);
00109                 void lsgenrand(const unsigned long*);
00110                 unsigned long genrand();
00111                 // The array for the state vector
00112                 unsigned long* mt;
00113                 int mti;
00114                 unsigned long init_seed;
00115 };
00116 
00124 class crand: public random_seq 
00125 {
00126         public:
00128                 crand(){}
00130                 crand(unsigned long seed) { srand (seed); }
00132                 void set_seed(unsigned long seed) { srand (seed); }
00134                 double next_dbl() { return (double)rand()/(double)RAND_MAX; } 
00136                 unsigned long next_long() { return (unsigned long)rand(); }
00137 
00138                 random_seq* copy() const { return new crand (); }
00140                 ~crand(){}
00141 };
00142 
00148 class rv 
00149 {
00150         public:
00152                 rv (unsigned long seed = 1);
00157                 rv(random_seq* rand);
00159                 rv(const rv& src);
00161                 const rv& operator=(const rv& src);
00163                 void set_seed(unsigned long seed);
00165                 unsigned long next_long();
00167                 double triangular(double a, double b, double c);
00169                 double uniform(double a, double b);
00174                 double normal(double m, double s);
00181                 double exponential(double a);
00182                 double hyperexponential(double p,double a,double b);
00183                 double laplace(double a);
00184                 double chisquare(unsigned int n);
00185                 double student(unsigned int n);
00186                 double lognormal(double a,double b);
00187                 double erlang(unsigned int n,double a);
00188                 double gamma(double a,double b);
00189                 double beta(double a,double b);
00190                 double fdistribution(unsigned int n,unsigned int m);
00191                 double poisson(double a);
00192                 double geometric(double p);
00193                 double hypergeometric(unsigned int m,unsigned int n,double p);
00194                 double weibull(double a,double b);
00195                 double binomial(double p,unsigned int n);
00196                 double negativebinomial(double p,unsigned int n);
00197                 double triangular(double a);
00198                 int probability(double p);
00199                 double lngamma(double xx);
00201                 ~rv();
00202         private:        
00203                 random_seq* _impl;
00204                 void err(errorType n);
00205 };
00206 
00207 } // end of namespace
00208 
00209 #endif
00210 
00211