///////////////////////////////////////////////////////////////////////////// // random2.cc // // SIMLIB version: 2.18 // Date: 2004-01-25 // // Copyright (c) 1991-2004 Petr Peringer // // This library is licensed under GNU Library GPL. See the file COPYING. // // // random number generators (dependent on Random()) // //////////////////////////////////////////////////////////////////////////// // interface // #include "simlib.h" #include "internal.h" #include // exp() floor() log() pow() sqrt() //////////////////////////////////////////////////////////////////////////// // implementation // SIMLIB_IMPLEMENTATION //////////////////////////////////////////////////////////////////////////// // _gam // static double _gam(double AK) { int K,i; double FK,PROD,DG,G,W; FK = K = (int) floor(AK); G = 0.0; if (K>0) { PROD = 1.0; for(i=1; i<=K; i++) PROD *= Random(); G = -log(PROD); } DG = AK-FK; if (DG <= 0.015) return (G); if (DG >=0.935) W=1.0; else { double A,B,X,Y; A = 1.0/DG; B = 1.0/(1.0-DG); do{ X = pow(Random(),A); Y = pow(Random(),B+X); }while (Y>1.0); W = X/Y; } G += W*(-log(Random())); return (G); } //////////////////////////////////////////////////////////////////////////// // Uniform - uniform random number generator // double Uniform(double l, double h) { if( l >= h ) SIMLIB_error(BadUniformParam); return(l+(h-l)*Random()); } //////////////////////////////////////////////////////////////////////////// // Normal(mi,sigma) // mi = mean value // sigma = std deviation? (smerodatna odchylka) ### // double Normal(double mi, double sigma) { int i; double SUM = 0.0; for (i=0; i<12; i++) SUM += Random(); return (SUM-6.0)*sigma + mi; } //////////////////////////////////////////////////////////////////////////// // Weibul // double Weibul(double lambda, double alfa) { double R,W; if (lambda<=0.0 || alfa<=1.0) SIMLIB_error(WeibullError); while ((R=Random()) == 0 || R == 1); W= pow (-1.0/lambda*log(1.0-R), 1.0/alfa); return (W); } //////////////////////////////////////////////////////////////////////////// // Erlang // double Erlang(double alfa, int beta) { double ER = 1.0; int i; if (beta<1) SIMLIB_error(ErlangError); for (i=0; i1) SIMLIB_error(NegBinMError2); ix = i = 0; do{ if (Random() <= p) ix++; i++; }while (i <= m); return (ix); } //////////////////////////////////////////////////////////////////////////// // Beta // double Beta(double th, double fi, double min, double max) { double X; X = _gam(th); X = X/(X+_gam(fi)); X = X*(max-min)+min; return (X); } //////////////////////////////////////////////////////////////////////////// // Triag(mod,min,max) // double Triag(double mod, double min, double max) { double RN,BMA,CMA,TR; RN=Random(); BMA=mod-min; CMA=max-min; if (RN1) SIMLIB_error(HyperGeomError2); IX=0; for (i=1; i<=n; i++) { if (Random() > p) p =m*p/(m-1); else { IX++; p=(m*p-1.0)/(m-1); } m--; } return (IX); } //////////////////////////////////////////////////////////////////////////// // Binom(n,theta) // n = # of experiments (pocet pokusu) // theta = probability // /* int Binom(unsigned n, double theta) { int BNM; double SUM,R,TT,x; if (theta<0 || theta>1) SIMLIB_error(BinomError); SUM = R = pow(1-theta,n); TT = theta/(1-theta); BNM = 0; x = Random(); while(x-SUM > 0) { R *= (n-BNM)/(BNM+1)*TT; SUM += R; BNM++; } return (BNM); } */ //////////////////////////////////////////////////////////////////////////// // end of RANDOM2.CPP ////////////////////////////////////////////////////////////////////////////